Add accept method (JSON_VISITOR), remove is_xxx queries, remove to_json feature.
Improve comments
This commit is contained in:
@@ -9,8 +9,8 @@ indexing
|
||||
]"
|
||||
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
|
||||
class
|
||||
JSON_ARRAY
|
||||
@@ -36,9 +36,18 @@ feature -- Access
|
||||
Result := values.i_th (i)
|
||||
end
|
||||
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_json_array' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_array (Current)
|
||||
end
|
||||
|
||||
feature -- Mesurement
|
||||
count:INTEGER is
|
||||
--
|
||||
-- Number of items.
|
||||
do
|
||||
Result:=values.count
|
||||
end
|
||||
@@ -62,28 +71,6 @@ feature -- Change Element
|
||||
end
|
||||
|
||||
feature -- Report
|
||||
to_json:STRING is
|
||||
--Printable json representation
|
||||
-- [] or [elements]
|
||||
local
|
||||
value:JSON_VALUE
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.append("[")
|
||||
from
|
||||
values.start
|
||||
until
|
||||
values.off
|
||||
loop
|
||||
value:=values.item
|
||||
Result.append(value.to_json)
|
||||
values.forth
|
||||
if not values.after then
|
||||
Result.append(",")
|
||||
end
|
||||
end
|
||||
Result.append("]")
|
||||
end
|
||||
|
||||
hash_code:INTEGER is
|
||||
--
|
||||
@@ -100,6 +87,14 @@ feature -- Report
|
||||
Result := Result \\ values.count
|
||||
|
||||
end
|
||||
feature -- Conversion
|
||||
|
||||
array_representation:ARRAYED_LIST[JSON_VALUE] is
|
||||
-- Representation as a sequences of values
|
||||
do
|
||||
Result:=values
|
||||
end
|
||||
|
||||
feature {NONE} --Implementation
|
||||
values:ARRAYED_LIST[JSON_VALUE]
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
indexing
|
||||
description: "JSON Truth values"
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
|
||||
class
|
||||
|
||||
@@ -18,7 +18,7 @@ create
|
||||
feature -- Initialization
|
||||
|
||||
make_boolean (an_item: BOOLEAN) is
|
||||
--
|
||||
--Initialize.
|
||||
do
|
||||
item := an_item
|
||||
end
|
||||
@@ -26,19 +26,24 @@ feature -- Initialization
|
||||
feature -- Access
|
||||
|
||||
item: BOOLEAN
|
||||
-- Content
|
||||
|
||||
to_json: STRING is
|
||||
--
|
||||
do
|
||||
Result := item.out
|
||||
end
|
||||
|
||||
hash_code: INTEGER is
|
||||
--
|
||||
--Hash code value
|
||||
do
|
||||
Result := item.hash_code
|
||||
end
|
||||
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_json_boolean' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_boolean (Current)
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
indexing
|
||||
description: "JSON Null Values"
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
|
||||
class
|
||||
JSON_NULL
|
||||
@@ -10,17 +10,20 @@ class
|
||||
JSON_VALUE
|
||||
|
||||
feature --Access
|
||||
to_json:STRING is
|
||||
--
|
||||
do
|
||||
Result:=null_value
|
||||
end
|
||||
|
||||
hash_code:INTEGER is
|
||||
--
|
||||
do
|
||||
Result:= null_value.hash_code
|
||||
end
|
||||
-- Hash code value
|
||||
do
|
||||
Result:= null_value.hash_code
|
||||
end
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_element_a' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_null (Current)
|
||||
end
|
||||
|
||||
feature {NONE}-- Implementation
|
||||
null_value:STRING is "null"
|
||||
|
||||
@@ -2,8 +2,8 @@ indexing
|
||||
|
||||
description: "JSON Numbers, octal and hexadecimal formats are not used."
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
|
||||
|
||||
class
|
||||
@@ -15,6 +15,8 @@ inherit
|
||||
redefine
|
||||
is_equal
|
||||
end
|
||||
|
||||
|
||||
create
|
||||
|
||||
make_integer,
|
||||
@@ -23,30 +25,39 @@ create
|
||||
feature -- initialization
|
||||
|
||||
make_integer (argument: INTEGER) is
|
||||
-- Initialize an instance of JSON_NUMBER as INTEGER
|
||||
do
|
||||
item:= argument.out
|
||||
internal_hash_code:=argument.hash_code
|
||||
numeric_type:= "INTEGER"
|
||||
numeric_type:= INTEGER_TYPE
|
||||
end
|
||||
|
||||
make_real (argument: REAL) is
|
||||
-- Initialize an instance of JSON_NUMBER as REAL
|
||||
do
|
||||
item:= argument.out
|
||||
internal_hash_code:=argument.hash_code
|
||||
numeric_type:= "REAL"
|
||||
numeric_type:= REAL_TYPE
|
||||
end
|
||||
|
||||
|
||||
feature -- Access
|
||||
|
||||
item: STRING
|
||||
-- Content
|
||||
|
||||
hash_code: INTEGER is
|
||||
--
|
||||
--Hash code value
|
||||
do
|
||||
Result:=internal_hash_code
|
||||
Result:=item.hash_code
|
||||
end
|
||||
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_json_number' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_number (Current)
|
||||
end
|
||||
|
||||
feature -- Status
|
||||
|
||||
@@ -54,23 +65,17 @@ feature -- Status
|
||||
-- Is `other' attached to an object of the same type
|
||||
-- as current object and identical to it?
|
||||
do
|
||||
Result:=item.is_equal (other.to_json)
|
||||
Result:=item.is_equal (other.item)
|
||||
end
|
||||
|
||||
feature -- Conversion
|
||||
|
||||
to_json: STRING is
|
||||
--
|
||||
do
|
||||
Result := item
|
||||
end
|
||||
|
||||
|
||||
feature -- Implementation
|
||||
|
||||
internal_hash_code: INTEGER
|
||||
INTEGER_TYPE: INTEGER is 1
|
||||
REAL_TYPE:INTEGER is 2
|
||||
numeric_type: INTEGER
|
||||
|
||||
numeric_type: STRING -- REAL or INTEGER
|
||||
|
||||
|
||||
invariant
|
||||
|
||||
@@ -13,8 +13,8 @@ An object is an unordered set of name/value pairs
|
||||
|
||||
]"
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ create
|
||||
feature -- Initialization
|
||||
|
||||
make is
|
||||
--
|
||||
-- Initialize
|
||||
do
|
||||
create object.make (10)
|
||||
end
|
||||
@@ -85,32 +85,25 @@ feature -- Access
|
||||
Result:=object.current_keys
|
||||
end
|
||||
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_json_object' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_object (Current)
|
||||
end
|
||||
|
||||
|
||||
feature -- Conversion
|
||||
map_representation: HASH_TABLE[JSON_VALUE,JSON_STRING] is
|
||||
--A representation that maps keys to values
|
||||
do
|
||||
Result:=object
|
||||
end
|
||||
|
||||
feature -- Report
|
||||
|
||||
to_json: STRING is
|
||||
-- Printable json representation
|
||||
-- {} or {member}
|
||||
-- see documentation
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.append ("{")
|
||||
from
|
||||
object.start
|
||||
until
|
||||
object.off
|
||||
loop
|
||||
Result.append (object.item_for_iteration.to_json)
|
||||
Result.append (":")
|
||||
Result.append (object.key_for_iteration.to_json)
|
||||
object.forth
|
||||
if not object.after then
|
||||
Result.append (",")
|
||||
end
|
||||
end
|
||||
Result.append ("}")
|
||||
end
|
||||
|
||||
hash_code: INTEGER is
|
||||
-- Hash code value
|
||||
local
|
||||
|
||||
@@ -7,8 +7,8 @@ indexing
|
||||
]"
|
||||
|
||||
author: "Javier Velilla"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
date: "2008/08/24"
|
||||
revision: "Revision 0.1"
|
||||
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
|
||||
|
||||
|
||||
@@ -43,6 +43,15 @@ feature -- Access
|
||||
item: STRING
|
||||
-- Contents
|
||||
|
||||
feature -- Visitor pattern
|
||||
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_json_string' procedure on `a_visitor'.)
|
||||
do
|
||||
a_visitor.visit_json_string (Current)
|
||||
end
|
||||
|
||||
|
||||
feature -- Comparison
|
||||
|
||||
@@ -50,13 +59,13 @@ feature -- Comparison
|
||||
-- Is JSON_STRING made of same character sequence as `other'
|
||||
-- (possibly with a different capacity)?
|
||||
do
|
||||
Result := Current.to_json.is_equal (other.to_json)
|
||||
Result := item.is_equal (other.item)
|
||||
end
|
||||
|
||||
feature -- Change Element
|
||||
|
||||
append (an_item: STRING)is
|
||||
--
|
||||
-- Add an_item
|
||||
do
|
||||
item.append_string (an_item)
|
||||
end
|
||||
@@ -64,17 +73,8 @@ feature -- Change Element
|
||||
|
||||
feature -- Status report
|
||||
|
||||
to_json: STRING is
|
||||
--
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.append ("%"")
|
||||
Result.append (item)
|
||||
Result.append ("%"")
|
||||
end
|
||||
|
||||
hash_code: INTEGER is
|
||||
--
|
||||
-- Hash code value
|
||||
do
|
||||
Result := item.hash_code
|
||||
end
|
||||
|
||||
@@ -24,117 +24,13 @@ inherit
|
||||
|
||||
HASHABLE
|
||||
|
||||
feature -- Access
|
||||
feature -- Visitor pattern
|
||||
|
||||
to_json:STRING is
|
||||
-- Generate the JSON String for this value object
|
||||
-- return a correct JSON-STRING
|
||||
accept (a_visitor: JSON_VISITOR) is
|
||||
-- Accept `a_visitor'.
|
||||
-- (Call `visit_*' procedure on `a_visitor'.)
|
||||
require
|
||||
a_visitor_not_void: a_visitor /= Void
|
||||
deferred
|
||||
end
|
||||
|
||||
feature -- Status Report
|
||||
|
||||
is_json_array:BOOLEAN is
|
||||
-- Does `Current' represent a JSON_ARRAY?
|
||||
do
|
||||
if generating_type.is_equal ("JSON_ARRAY") then
|
||||
Result := true
|
||||
end
|
||||
end
|
||||
|
||||
is_json_string:BOOLEAN is
|
||||
-- Does `Current' represent a JSON_STRING?
|
||||
do
|
||||
if generating_type.is_equal ("JSON_STRING") then
|
||||
Result := true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
is_json_object:BOOLEAN is
|
||||
-- Does `Current' represent a JSON_OBJECT?
|
||||
do
|
||||
|
||||
if generating_type.is_equal ("JSON_OBJECT") then
|
||||
Result := true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
is_json_number:BOOLEAN is
|
||||
-- Does 'Current' represent a JSON_NUMBER?
|
||||
do
|
||||
if generating_type.is_equal ("JSON_NUMBER") then
|
||||
Result := true
|
||||
end
|
||||
end
|
||||
|
||||
is_json_boolean:BOOLEAN is
|
||||
-- Does 'Current' represent a JSON_BOOLEAN?
|
||||
do
|
||||
if generating_type.is_equal ("JSON_BOOLEAN") then
|
||||
Result := true
|
||||
end
|
||||
end
|
||||
|
||||
is_json_null:BOOLEAN is
|
||||
-- Does 'Current' represent a JSON_NULL?
|
||||
do
|
||||
if generating_type.is_equal ("JSON_NULL") then
|
||||
Result := true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
feature -- Conversion
|
||||
|
||||
to_json_array:JSON_ARRAY is
|
||||
-- Convert `Current' as a JSON_ARRAY.
|
||||
require
|
||||
is_a_json_array:is_json_array
|
||||
do
|
||||
Result ?= Current
|
||||
end
|
||||
|
||||
to_json_string:JSON_STRING is
|
||||
-- Convert `Current' as a JSON_STRING.
|
||||
require
|
||||
is_a_json_string: is_json_string
|
||||
do
|
||||
Result ?= Current
|
||||
end
|
||||
|
||||
to_json_object:JSON_OBJECT is
|
||||
-- Convert 'Current' as a JSON_OBJECT
|
||||
require
|
||||
is_a_json_object: is_json_object
|
||||
do
|
||||
Result?=Current
|
||||
end
|
||||
|
||||
to_json_number:JSON_NUMBER is
|
||||
-- Convert 'Current' as a JSON_NUMBER
|
||||
require
|
||||
is_a_json_number:is_json_number
|
||||
do
|
||||
Result ?= Current
|
||||
end
|
||||
|
||||
to_json_boolean:JSON_BOOLEAN is
|
||||
-- Convert 'Current' as a JSON_BOOLEAN
|
||||
require
|
||||
is_a_json_boolean:is_json_boolean
|
||||
do
|
||||
Result ?= Current
|
||||
end
|
||||
|
||||
to_json_null:JSON_NULL is
|
||||
-- Convert 'Current' as a JSON_NULL
|
||||
require
|
||||
is_a_json_null:is_json_null
|
||||
do
|
||||
Result ?= Current
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user