Enhanced interface of JSON_ARRAY and JSON_OBJECT
Added JSON_ITERATOR
This commit is contained in:
60
library/extras/visitor/json_iterator.e
Normal file
60
library/extras/visitor/json_iterator.e
Normal 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
|
||||
@@ -18,7 +18,7 @@ class
|
||||
inherit
|
||||
JSON_VALUE
|
||||
|
||||
DEBUG_OUTPUT
|
||||
ITERABLE [JSON_VALUE]
|
||||
|
||||
create
|
||||
make_array
|
||||
@@ -69,6 +69,14 @@ feature -- Visitor pattern
|
||||
a_visitor.visit_json_array (Current)
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
new_cursor: ITERATION_CURSOR [JSON_VALUE]
|
||||
-- Fresh cursor associated with current structure
|
||||
do
|
||||
Result := values.new_cursor
|
||||
end
|
||||
|
||||
feature -- Mesurement
|
||||
|
||||
count: INTEGER
|
||||
@@ -87,16 +95,42 @@ feature -- Status report
|
||||
|
||||
feature -- Change Element
|
||||
|
||||
add (value: JSON_VALUE)
|
||||
put_front (v: JSON_VALUE)
|
||||
require
|
||||
value_not_null: value /= void
|
||||
v_not_void: v /= Void
|
||||
do
|
||||
values.extend (value)
|
||||
values.put_front (v)
|
||||
ensure
|
||||
has_new_value: old values.count + 1 = values.count and
|
||||
values.has (value)
|
||||
values.first = v
|
||||
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
|
||||
|
||||
hash_code: INTEGER
|
||||
@@ -118,6 +152,7 @@ feature -- Conversion
|
||||
|
||||
array_representation: ARRAYED_LIST [JSON_VALUE]
|
||||
-- Representation as a sequences of values
|
||||
-- be careful, modifying the return object may have impact on the original JSON_ARRAY object
|
||||
do
|
||||
Result := values
|
||||
end
|
||||
@@ -127,7 +162,7 @@ feature -- Status report
|
||||
debug_output: STRING
|
||||
-- String that should be displayed in debugger to represent `Current'.
|
||||
do
|
||||
Result := count.out
|
||||
Result := count.out + " item(s)"
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
@@ -65,6 +65,18 @@ feature -- Change Element
|
||||
object.force (l_value, key)
|
||||
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
|
||||
|
||||
has_key (key: JSON_STRING): BOOLEAN
|
||||
@@ -95,7 +107,8 @@ feature -- Access
|
||||
local
|
||||
t: HASH_TABLE [JSON_VALUE, JSON_STRING]
|
||||
do
|
||||
Result := "{"
|
||||
create Result.make (2)
|
||||
Result.append_character ('{')
|
||||
from
|
||||
t := map_representation
|
||||
t.start
|
||||
@@ -103,7 +116,7 @@ feature -- Access
|
||||
t.after
|
||||
loop
|
||||
Result.append (t.key_for_iteration.representation)
|
||||
Result.append (":")
|
||||
Result.append_character (':')
|
||||
Result.append (t.item_for_iteration.representation)
|
||||
t.forth
|
||||
if not t.after then
|
||||
@@ -177,7 +190,7 @@ feature -- Status report
|
||||
debug_output: STRING
|
||||
-- String that should be displayed in debugger to represent `Current'.
|
||||
do
|
||||
Result := object.count.out
|
||||
Result := count.out + " item(s)"
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
@@ -186,6 +199,6 @@ feature {NONE} -- Implementation
|
||||
-- Value container
|
||||
|
||||
invariant
|
||||
object_not_null: object /= Void
|
||||
object_not_void: object /= Void
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user