Enhanced interface of JSON_ARRAY and JSON_OBJECT

Added JSON_ITERATOR
This commit is contained in:
2013-08-05 12:07:07 +02:00
parent 3e976768ac
commit 8e743f3253
3 changed files with 118 additions and 10 deletions

View File

@@ -0,0 +1,60 @@
note
description:
"JSON Iterator"
pattern: "Iterator visitor"
author: "Jocelyn Fiat"
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
date: "2013/08/01"
revision: "Revision 0.1"
deferred class
JSON_ITERATOR
inherit
JSON_VISITOR
feature -- Visitor Pattern
visit_json_array (a_json_array: JSON_ARRAY)
-- Visit `a_json_array'.
do
across
a_json_array as c
loop
c.item.accept (Current)
end
end
visit_json_boolean (a_json_boolean: JSON_BOOLEAN)
-- Visit `a_json_boolean'.
do
end
visit_json_null (a_json_null: JSON_NULL)
-- Visit `a_json_null'.
do
end
visit_json_number (a_json_number: JSON_NUMBER)
-- Visit `a_json_number'.
do
end
visit_json_object (a_json_object: JSON_OBJECT)
-- Visit `a_json_object'.
do
across
a_json_object as c
loop
c.item.accept (Current)
end
end
visit_json_string (a_json_string: JSON_STRING)
-- Visit `a_json_string'.
do
end
end

View File

@@ -18,7 +18,7 @@ class
inherit inherit
JSON_VALUE JSON_VALUE
DEBUG_OUTPUT ITERABLE [JSON_VALUE]
create create
make_array make_array
@@ -69,6 +69,14 @@ feature -- Visitor pattern
a_visitor.visit_json_array (Current) a_visitor.visit_json_array (Current)
end end
feature -- Access
new_cursor: ITERATION_CURSOR [JSON_VALUE]
-- Fresh cursor associated with current structure
do
Result := values.new_cursor
end
feature -- Mesurement feature -- Mesurement
count: INTEGER count: INTEGER
@@ -87,16 +95,42 @@ feature -- Status report
feature -- Change Element feature -- Change Element
add (value: JSON_VALUE) put_front (v: JSON_VALUE)
require require
value_not_null: value /= void v_not_void: v /= Void
do do
values.extend (value) values.put_front (v)
ensure ensure
has_new_value: old values.count + 1 = values.count and has_new_value: old values.count + 1 = values.count and
values.has (value) values.first = v
end end
add, extend (v: JSON_VALUE)
require
v_not_void: v /= Void
do
values.extend (v)
ensure
has_new_value: old values.count + 1 = values.count and
values.has (v)
end
prune_all (v: JSON_VALUE)
-- Remove all occurrences of `v'.
require
v_not_void: v /= Void
do
values.prune_all (v)
ensure
not_has_new_value: not values.has (v)
end
wipe_out
-- Remove all items.
do
values.wipe_out
end
feature -- Report feature -- Report
hash_code: INTEGER hash_code: INTEGER
@@ -118,6 +152,7 @@ feature -- Conversion
array_representation: ARRAYED_LIST [JSON_VALUE] array_representation: ARRAYED_LIST [JSON_VALUE]
-- Representation as a sequences of values -- Representation as a sequences of values
-- be careful, modifying the return object may have impact on the original JSON_ARRAY object
do do
Result := values Result := values
end end
@@ -127,7 +162,7 @@ feature -- Status report
debug_output: STRING debug_output: STRING
-- String that should be displayed in debugger to represent `Current'. -- String that should be displayed in debugger to represent `Current'.
do do
Result := count.out Result := count.out + " item(s)"
end end
feature {NONE} -- Implementation feature {NONE} -- Implementation

View File

@@ -65,6 +65,18 @@ feature -- Change Element
object.force (l_value, key) object.force (l_value, key)
end end
remove (key: JSON_STRING)
-- Remove item indexed by `key' if any.
do
object.remove (key)
end
wipe_out
-- Reset all items to default values; reset status.
do
object.wipe_out
end
feature -- Access feature -- Access
has_key (key: JSON_STRING): BOOLEAN has_key (key: JSON_STRING): BOOLEAN
@@ -95,7 +107,8 @@ feature -- Access
local local
t: HASH_TABLE [JSON_VALUE, JSON_STRING] t: HASH_TABLE [JSON_VALUE, JSON_STRING]
do do
Result := "{" create Result.make (2)
Result.append_character ('{')
from from
t := map_representation t := map_representation
t.start t.start
@@ -103,7 +116,7 @@ feature -- Access
t.after t.after
loop loop
Result.append (t.key_for_iteration.representation) Result.append (t.key_for_iteration.representation)
Result.append (":") Result.append_character (':')
Result.append (t.item_for_iteration.representation) Result.append (t.item_for_iteration.representation)
t.forth t.forth
if not t.after then if not t.after then
@@ -177,7 +190,7 @@ feature -- Status report
debug_output: STRING debug_output: STRING
-- String that should be displayed in debugger to represent `Current'. -- String that should be displayed in debugger to represent `Current'.
do do
Result := object.count.out Result := count.out + " item(s)"
end end
feature {NONE} -- Implementation feature {NONE} -- Implementation
@@ -186,6 +199,6 @@ feature {NONE} -- Implementation
-- Value container -- Value container
invariant invariant
object_not_null: object /= Void object_not_void: object /= Void
end end