Ported r75 (all JSON value classes) from POC_CONVERTERS_FACTORY branch to trunk

This commit is contained in:
paul.cohen
2010-03-08 22:45:41 +00:00
parent 2710cae1da
commit a0e570226c
7 changed files with 500 additions and 405 deletions

View File

@@ -1,122 +1,141 @@
indexing indexing
description: "[ description: "[
JSON_ARRAY represent an array in JSON. JSON_ARRAY represent an array in JSON.
An array in JSON is an ordered set of names. An array in JSON is an ordered set of names.
Examples Examples
array array
[] []
[elements] [elements]
]" ]"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
class class
JSON_ARRAY JSON_ARRAY
inherit inherit
JSON_VALUE JSON_VALUE
DEBUG_OUTPUT DEBUG_OUTPUT
create create
make_array make_array
feature {NONE} -- Initialization feature {NONE} -- Initialization
make_array is make_array is
-- Initialize JSON Array -- Initialize JSON Array
do do
create values.make (10) create values.make (10)
end end
feature -- Access feature -- Access
i_th alias "[]" (i: INTEGER): JSON_VALUE is i_th alias "[]" (i: INTEGER): JSON_VALUE is
-- Item at `i'-th position -- Item at `i'-th position
require require
is_valid_index: valid_index (i) is_valid_index: valid_index (i)
do do
Result := values.i_th (i) Result := values.i_th (i)
end end
representation: STRING is
local
i: INTEGER
do
Result := "["
from
i := 1
until
i > count
loop
Result.append (i_th (i).representation)
i := i + 1
if i <= count then
Result.append_character (',')
end
end
Result.append_character (']')
end
feature -- Visitor pattern feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is accept (a_visitor: JSON_VISITOR) is
-- Accept `a_visitor'. -- Accept `a_visitor'.
-- (Call `visit_json_array' procedure on `a_visitor'.) -- (Call `visit_json_array' procedure on `a_visitor'.)
do do
a_visitor.visit_json_array (Current) a_visitor.visit_json_array (Current)
end end
feature -- Mesurement feature -- Mesurement
count: INTEGER is count: INTEGER is
-- Number of items. -- Number of items.
do do
Result := values.count Result := values.count
end end
feature -- Status report feature -- Status report
valid_index (i: INTEGER): BOOLEAN is valid_index (i: INTEGER): BOOLEAN is
-- Is `i' a valid index? -- Is `i' a valid index?
do do
Result := (1 <= i) and (i <= count) Result := (1 <= i) and (i <= count)
end end
feature -- Change Element feature -- Change Element
add (value: JSON_VALUE) is add (value: JSON_VALUE) is
require require
value_not_null: value /= void value_not_null: value /= void
do do
values.extend(value) values.extend(value)
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.has (value)
end end
feature -- Report feature -- Report
hash_code: INTEGER is hash_code: INTEGER is
-- Hash code value -- Hash code value
do do
from from
values.start values.start
Result := values.item.hash_code Result := values.item.hash_code
until until
values.off values.off
loop loop
Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code
values.forth values.forth
end end
Result := Result \\ values.count Result := Result \\ values.count
end end
feature -- Conversion feature -- Conversion
array_representation: ARRAYED_LIST [JSON_VALUE] is array_representation: ARRAYED_LIST [JSON_VALUE] is
-- Representation as a sequences of values -- Representation as a sequences of values
do do
Result := values Result := values
end end
feature -- Status report 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
end end
feature {NONE} -- Implementation feature {NONE} -- Implementation
values: ARRAYED_LIST [JSON_VALUE] values: ARRAYED_LIST [JSON_VALUE]
-- Value container -- Value container
invariant invariant
value_not_void: values /= Void value_not_void: values /= Void
end end

View File

@@ -1,52 +1,61 @@
indexing indexing
description: "JSON Truth values" description: "JSON Truth values"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
class class
JSON_BOOLEAN JSON_BOOLEAN
inherit inherit
JSON_VALUE JSON_VALUE
create create
make_boolean make_boolean
feature {NONE} -- Initialization feature {NONE} -- Initialization
make_boolean (an_item: BOOLEAN) is make_boolean (an_item: BOOLEAN) is
--Initialize. --Initialize.
do do
item := an_item item := an_item
end end
feature -- Access feature -- Access
item: BOOLEAN item: BOOLEAN
-- Content -- Content
hash_code: INTEGER is hash_code: INTEGER is
-- Hash code value -- Hash code value
do do
Result := item.hash_code Result := item.hash_code
end end
representation: STRING is
do
if item then
Result := "true"
else
Result := "false"
end
end
feature -- Visitor pattern feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is accept (a_visitor: JSON_VISITOR) is
-- Accept `a_visitor'. -- Accept `a_visitor'.
-- (Call `visit_json_boolean' procedure on `a_visitor'.) -- (Call `visit_json_boolean' procedure on `a_visitor'.)
do do
a_visitor.visit_json_boolean (Current) a_visitor.visit_json_boolean (Current)
end end
feature -- Status report 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 := item.out Result := item.out
end end
end end

View File

@@ -1,42 +1,47 @@
indexing indexing
description: "JSON Null Values" description: "JSON Null Values"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
class class
JSON_NULL JSON_NULL
inherit inherit
JSON_VALUE JSON_VALUE
feature --Access feature --Access
hash_code: INTEGER is hash_code: INTEGER is
-- Hash code value -- Hash code value
do do
Result := null_value.hash_code Result := null_value.hash_code
end end
representation: STRING is
do
Result := "null"
end
feature -- Visitor pattern feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is accept (a_visitor: JSON_VISITOR) is
-- Accept `a_visitor'. -- Accept `a_visitor'.
-- (Call `visit_element_a' procedure on `a_visitor'.) -- (Call `visit_element_a' procedure on `a_visitor'.)
do do
a_visitor.visit_json_null (Current) a_visitor.visit_json_null (Current)
end end
feature -- Status report 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 := null_value Result := null_value
end end
feature {NONE}-- Implementation feature {NONE}-- Implementation
null_value: STRING is "null" null_value: STRING is "null"
end end

View File

@@ -1,85 +1,99 @@
indexing indexing
description: "JSON Numbers, octal and hexadecimal formats are not used." description: "JSON Numbers, octal and hexadecimal formats are not used."
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
class class
JSON_NUMBER JSON_NUMBER
inherit inherit
JSON_VALUE JSON_VALUE
redefine redefine
is_equal is_equal
end end
create create
make_integer, make_integer,
make_real make_natural,
make_real
feature {NONE} -- initialization feature {NONE} -- initialization
make_integer (an_argument: INTEGER) is make_integer (an_argument: INTEGER_64) is
-- Initialize an instance of JSON_NUMBER as INTEGER -- Initialize an instance of JSON_NUMBER from the integer value of `an_argument'.
do do
item := an_argument.out item := an_argument.out
numeric_type := INTEGER_TYPE numeric_type := INTEGER_TYPE
end end
make_real (an_argument: DOUBLE) is make_natural (an_argument: NATURAL_64) is
-- Initialize an instance of JSON_NUMBER as DOUBLE -- Initialize an instance of JSON_NUMBER from the unsigned integer value of `an_argument'.
do do
item := an_argument.out item := an_argument.out
numeric_type := DOUBLE_TYPE numeric_type := NATURAL_TYPE
end end
make_real (an_argument: DOUBLE) is
-- Initialize an instance of JSON_NUMBER from the floating point value of `an_argument'.
do
item := an_argument.out
numeric_type := DOUBLE_TYPE
end
feature -- Access feature -- Access
item: STRING item: STRING
-- Content -- Content
hash_code: INTEGER is hash_code: INTEGER is
--Hash code value --Hash code value
do do
Result := item.hash_code Result := item.hash_code
end end
representation: STRING is
do
Result := item
end
feature -- Visitor pattern feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is accept (a_visitor: JSON_VISITOR) is
-- Accept `a_visitor'. -- Accept `a_visitor'.
-- (Call `visit_json_number' procedure on `a_visitor'.) -- (Call `visit_json_number' procedure on `a_visitor'.)
do do
a_visitor.visit_json_number (Current) a_visitor.visit_json_number (Current)
end end
feature -- Status feature -- Status
is_equal (other: like Current): BOOLEAN is is_equal (other: like Current): BOOLEAN is
-- Is `other' attached to an object of the same type -- Is `other' attached to an object of the same type
-- as current object and identical to it? -- as current object and identical to it?
do do
Result := item.is_equal (other.item) Result := item.is_equal (other.item)
end end
feature -- Status report 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 := item Result := item
end end
feature -- Implementation feature -- Implementation
INTEGER_TYPE: INTEGER is 1 INTEGER_TYPE: INTEGER is 1
DOUBLE_TYPE: INTEGER is 2 DOUBLE_TYPE: INTEGER is 2
NATURAL_TYPE: INTEGER is 3
numeric_type: INTEGER numeric_type: INTEGER
invariant invariant
item_not_void: item /= Void item_not_void: item /= Void
end end

View File

@@ -1,130 +1,152 @@
indexing indexing
description: "[ description: "[
An JSON_OBJECT represent an object in JSON. An JSON_OBJECT represent an object in JSON.
An object is an unordered set of name/value pairs An object is an unordered set of name/value pairs
Examples: Examples:
object object
{} {}
{"key","value"} {"key","value"}
]" ]"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
class class
JSON_OBJECT JSON_OBJECT
inherit inherit
JSON_VALUE JSON_VALUE
create create
make make
feature {NONE} -- Initialization feature {NONE} -- Initialization
make is make is
-- Initialize -- Initialize
do do
create object.make (10) create object.make (10)
end end
feature -- Change Element feature -- Change Element
put (value: ?JSON_VALUE; key: JSON_STRING) is put (value: ?JSON_VALUE; key: JSON_STRING) is
-- Assuming there is no item of key `key', -- Assuming there is no item of key `key',
-- insert `value' with `key'. -- insert `value' with `key'.
require require
key_not_present: not has_key (key) key_not_present: not has_key (key)
local local
l_value: ?JSON_VALUE l_value: ?JSON_VALUE
do do
l_value := value l_value := value
if l_value = Void then if l_value = Void then
create {JSON_NULL} l_value create {JSON_NULL} l_value
end end
object.extend (l_value, key) object.extend (l_value, key)
end end
feature -- Access feature -- Access
has_key (key: JSON_STRING): BOOLEAN is has_key (key: JSON_STRING): BOOLEAN is
-- has the JSON_OBJECT contains a specific key 'key'. -- has the JSON_OBJECT contains a specific key 'key'.
do do
Result := object.has (key) Result := object.has (key)
end end
has_item (value: JSON_VALUE): BOOLEAN is has_item (value: JSON_VALUE): BOOLEAN is
-- has the JSON_OBJECT contain a specfic item 'value' -- has the JSON_OBJECT contain a specfic item 'value'
do do
Result := object.has_item (value) Result := object.has_item (value)
end end
item (key: JSON_STRING): ?JSON_VALUE is item (key: JSON_STRING): ?JSON_VALUE is
-- the json_value associated with a key. -- the json_value associated with a key.
do do
Result := object.item (key) Result := object.item (key)
end end
current_keys: ARRAY [JSON_STRING] is current_keys: ARRAY [JSON_STRING] is
-- array containing actually used keys -- array containing actually used keys
do do
Result := object.current_keys Result := object.current_keys
end end
feature -- Visitor pattern representation: STRING is
local
accept (a_visitor: JSON_VISITOR) is t: HASH_TABLE [JSON_VALUE, JSON_STRING]
-- Accept `a_visitor'. do
-- (Call `visit_json_object' procedure on `a_visitor'.) Result := "{"
do from
a_visitor.visit_json_object (Current) t := map_representation
end t.start
until
feature -- Conversion t.after
loop
map_representation: HASH_TABLE [JSON_VALUE, JSON_STRING] is Result.append (t.key_for_iteration.representation)
--A representation that maps keys to values Result.append (":")
do Result.append (t.item_for_iteration.representation)
Result := object t.forth
end if not t.after then
Result.append_character (',')
feature -- Report end
end
hash_code: INTEGER is Result.append_character ('}')
-- Hash code value end
do
from feature -- Visitor pattern
object.start
Result := object.item_for_iteration.hash_code accept (a_visitor: JSON_VISITOR) is
until -- Accept `a_visitor'.
object.off -- (Call `visit_json_object' procedure on `a_visitor'.)
loop do
Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code a_visitor.visit_json_object (Current)
object.forth end
end
-- Ensure it is a positive value. feature -- Conversion
Result := Result.hash_code
end map_representation: HASH_TABLE [JSON_VALUE, JSON_STRING] is
--A representation that maps keys to values
feature -- Status report do
Result := object
debug_output: STRING end
-- String that should be displayed in debugger to represent `Current'.
do feature -- Report
Result := object.count.out
end hash_code: INTEGER is
-- Hash code value
feature {NONE} -- Implementation do
from
object: HASH_TABLE [JSON_VALUE, JSON_STRING] object.start
-- Value container Result := object.item_for_iteration.hash_code
until
invariant object.off
object_not_null: object /= Void loop
Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code
end object.forth
end
-- Ensure it is a positive value.
Result := Result.hash_code
end
feature -- Status report
debug_output: STRING
-- String that should be displayed in debugger to represent `Current'.
do
Result := object.count.out
end
feature {NONE} -- Implementation
object: HASH_TABLE [JSON_VALUE, JSON_STRING]
-- Value container
invariant
object_not_null: object /= Void
end

View File

@@ -1,89 +1,108 @@
indexing indexing
description:"[ description:"[
A JSON_STRING represent a string in JSON. A JSON_STRING represent a string in JSON.
A string is a collection of zero or more Unicodes characters, wrapped in double A string is a collection of zero or more Unicodes characters, wrapped in double
quotes, using blackslash espaces. quotes, using blackslash espaces.
]" ]"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/08/24" date: "2008/08/24"
revision: "Revision 0.1" revision: "Revision 0.1"
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
class class
JSON_STRING JSON_STRING
inherit inherit
JSON_VALUE JSON_VALUE
redefine redefine
is_equal is_equal
end end
create create
make_json make_json
feature {NONE} -- Initialization feature {NONE} -- Initialization
make_json (an_item: STRING) is make_json (an_item: STRING) is
-- Initialize. -- Initialize.
require require
item_not_void: an_item /= Void item_not_void: an_item /= Void
do do
item := an_item item := escaped_json_string (an_item)
end end
feature -- Access feature -- Access
item: STRING item: STRING
-- Contents -- Contents
representation: STRING is
do
Result := "%""
Result.append (item)
Result.append_character ('%"')
end
feature -- Visitor pattern feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is accept (a_visitor: JSON_VISITOR) is
-- Accept `a_visitor'. -- Accept `a_visitor'.
-- (Call `visit_json_string' procedure on `a_visitor'.) -- (Call `visit_json_string' procedure on `a_visitor'.)
do do
a_visitor.visit_json_string (Current) a_visitor.visit_json_string (Current)
end end
feature -- Comparison feature -- Comparison
is_equal (other: like Current): BOOLEAN is is_equal (other: like Current): BOOLEAN is
-- Is JSON_STRING made of same character sequence as `other' -- Is JSON_STRING made of same character sequence as `other'
-- (possibly with a different capacity)? -- (possibly with a different capacity)?
do do
Result := item.is_equal (other.item) Result := item.is_equal (other.item)
end end
feature -- Change Element feature -- Change Element
append (a_string: STRING)is append (a_string: STRING)is
-- Add an_item -- Add an_item
require require
a_string_not_void: a_string /= Void a_string_not_void: a_string /= Void
do do
item.append_string (a_string) item.append_string (a_string)
end end
feature -- Status report feature -- Status report
hash_code: INTEGER is hash_code: INTEGER is
-- Hash code value -- Hash code value
do do
Result := item.hash_code Result := item.hash_code
end end
feature -- Status report 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 := item Result := item
end end
feature {NONE} -- Implementation
escaped_json_string (s: STRING): STRING is
-- JSON string with '"' and '\' characters escaped
require
s_not_void: s /= Void
do
Result := s.twin
Result.replace_substring_all ("\", "\\")
Result.replace_substring_all ("%"", "\%"")
end
invariant invariant
value_not_void: item /= Void value_not_void: item /= Void
end end

View File

@@ -1,36 +1,43 @@
indexing indexing
description:"[ description:"[
JSON_VALUE represent a value in JSON. JSON_VALUE represent a value in JSON.
A value can be A value can be
* a string in double quotes * a string in double quotes
* a number * a number
* boolean value(true, false ) * boolean value(true, false )
* null * null
* an object * an object
* an array * an array
]" ]"
author: "Javier Velilla" author: "Javier Velilla"
date: "2008/05/19" date: "2008/05/19"
revision: "Revision 0.1" revision: "Revision 0.1"
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
deferred class deferred class
JSON_VALUE JSON_VALUE
inherit inherit
HASHABLE HASHABLE
DEBUG_OUTPUT DEBUG_OUTPUT
feature -- Visitor pattern feature -- Access
accept (a_visitor: JSON_VISITOR) is representation: STRING is
-- Accept `a_visitor'. -- UTF-8 encoded Unicode string representation of Current
-- (Call `visit_*' procedure on `a_visitor'.) deferred
require end
a_visitor_not_void: a_visitor /= Void
deferred feature -- Visitor pattern
end
accept (a_visitor: JSON_VISITOR) is
end -- Accept `a_visitor'.
-- (Call `visit_*' procedure on `a_visitor'.)
require
a_visitor_not_void: a_visitor /= Void
deferred
end
end