From a0e570226caeb66407187e33ede761677b33f5cc Mon Sep 17 00:00:00 2001 From: "paul.cohen" Date: Mon, 8 Mar 2010 22:45:41 +0000 Subject: [PATCH] Ported r75 (all JSON value classes) from POC_CONVERTERS_FACTORY branch to trunk --- library/kernel/json_array.e | 177 +++++++++++---------- library/kernel/json_boolean.e | 69 +++++---- library/kernel/json_null.e | 53 ++++--- library/kernel/json_number.e | 118 +++++++------- library/kernel/json_object.e | 282 ++++++++++++++++++---------------- library/kernel/json_string.e | 127 ++++++++------- library/kernel/json_value.e | 79 +++++----- 7 files changed, 500 insertions(+), 405 deletions(-) diff --git a/library/kernel/json_array.e b/library/kernel/json_array.e index bf762885..b71bc48d 100644 --- a/library/kernel/json_array.e +++ b/library/kernel/json_array.e @@ -1,122 +1,141 @@ indexing - description: "[ - JSON_ARRAY represent an array in JSON. - An array in JSON is an ordered set of names. - Examples - array - [] - [elements] - ]" + description: "[ + JSON_ARRAY represent an array in JSON. + An array in JSON is an ordered set of names. + Examples + array + [] + [elements] + ]" - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" class - JSON_ARRAY + JSON_ARRAY inherit - JSON_VALUE + JSON_VALUE - DEBUG_OUTPUT + DEBUG_OUTPUT create - make_array + make_array feature {NONE} -- Initialization - make_array is - -- Initialize JSON Array - do - create values.make (10) - end + make_array is + -- Initialize JSON Array + do + create values.make (10) + end feature -- Access - i_th alias "[]" (i: INTEGER): JSON_VALUE is - -- Item at `i'-th position - require - is_valid_index: valid_index (i) - do - Result := values.i_th (i) - end + i_th alias "[]" (i: INTEGER): JSON_VALUE is + -- Item at `i'-th position + require + is_valid_index: valid_index (i) + do + Result := values.i_th (i) + 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 - 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 + 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 + count: INTEGER is + -- Number of items. + do + Result := values.count + end feature -- Status report - valid_index (i: INTEGER): BOOLEAN is - -- Is `i' a valid index? - do - Result := (1 <= i) and (i <= count) - end + valid_index (i: INTEGER): BOOLEAN is + -- Is `i' a valid index? + do + Result := (1 <= i) and (i <= count) + end feature -- Change Element - add (value: JSON_VALUE) is - require - value_not_null: value /= void - do - values.extend(value) - ensure - has_new_value: old values.count + 1 = values.count and - values.has (value) - end + add (value: JSON_VALUE) is + require + value_not_null: value /= void + do + values.extend(value) + ensure + has_new_value: old values.count + 1 = values.count and + values.has (value) + end feature -- Report - hash_code: INTEGER is - -- Hash code value - do - from - values.start - Result := values.item.hash_code - until - values.off - loop - Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code - values.forth - end - Result := Result \\ values.count - end + hash_code: INTEGER is + -- Hash code value + do + from + values.start + Result := values.item.hash_code + until + values.off + loop + Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code + values.forth + end + Result := Result \\ values.count + end feature -- Conversion - array_representation: ARRAYED_LIST [JSON_VALUE] is - -- Representation as a sequences of values - do - Result := values - end + array_representation: ARRAYED_LIST [JSON_VALUE] is + -- Representation as a sequences of values + do + Result := values + end feature -- Status report - debug_output: STRING - -- String that should be displayed in debugger to represent `Current'. - do - Result := count.out - end + debug_output: STRING + -- String that should be displayed in debugger to represent `Current'. + do + Result := count.out + end feature {NONE} -- Implementation - values: ARRAYED_LIST [JSON_VALUE] - -- Value container + values: ARRAYED_LIST [JSON_VALUE] + -- Value container invariant - value_not_void: values /= Void + value_not_void: values /= Void end diff --git a/library/kernel/json_boolean.e b/library/kernel/json_boolean.e index d89be8da..86b48dbc 100644 --- a/library/kernel/json_boolean.e +++ b/library/kernel/json_boolean.e @@ -1,52 +1,61 @@ indexing - description: "JSON Truth values" - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" + description: "JSON Truth values" + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" class - JSON_BOOLEAN + JSON_BOOLEAN inherit - JSON_VALUE + JSON_VALUE create - make_boolean + make_boolean feature {NONE} -- Initialization - make_boolean (an_item: BOOLEAN) is - --Initialize. - do - item := an_item - end + make_boolean (an_item: BOOLEAN) is + --Initialize. + do + item := an_item + end feature -- Access - item: BOOLEAN - -- Content + item: BOOLEAN + -- Content - hash_code: INTEGER is - -- Hash code value - do - Result := item.hash_code - end + hash_code: INTEGER is + -- Hash code value + do + Result := item.hash_code + end + representation: STRING is + do + if item then + Result := "true" + else + Result := "false" + end + 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 + 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 feature -- Status report - debug_output: STRING - -- String that should be displayed in debugger to represent `Current'. - do - Result := item.out - end + debug_output: STRING + -- String that should be displayed in debugger to represent `Current'. + do + Result := item.out + end end diff --git a/library/kernel/json_null.e b/library/kernel/json_null.e index ca61e1c0..d7609b37 100644 --- a/library/kernel/json_null.e +++ b/library/kernel/json_null.e @@ -1,42 +1,47 @@ indexing - description: "JSON Null Values" - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" + description: "JSON Null Values" + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" class - JSON_NULL + JSON_NULL inherit - JSON_VALUE + JSON_VALUE feature --Access - hash_code: INTEGER is - -- Hash code value - do - Result := null_value.hash_code - end + hash_code: INTEGER is + -- Hash code value + do + Result := null_value.hash_code + end + representation: STRING is + do + Result := "null" + 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 + 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 -- Status report - debug_output: STRING - -- String that should be displayed in debugger to represent `Current'. - do - Result := null_value - end + debug_output: STRING + -- String that should be displayed in debugger to represent `Current'. + do + Result := null_value + end -feature {NONE}-- Implementation +feature {NONE}-- Implementation - null_value: STRING is "null" + null_value: STRING is "null" end diff --git a/library/kernel/json_number.e b/library/kernel/json_number.e index e76cc85f..2f939684 100644 --- a/library/kernel/json_number.e +++ b/library/kernel/json_number.e @@ -1,85 +1,99 @@ indexing - description: "JSON Numbers, octal and hexadecimal formats are not used." - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" - license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" + description: "JSON Numbers, octal and hexadecimal formats are not used." + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" + license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" class - JSON_NUMBER + JSON_NUMBER inherit - JSON_VALUE - redefine - is_equal - end + JSON_VALUE + redefine + is_equal + end create - make_integer, - make_real + make_integer, + make_natural, + make_real feature {NONE} -- initialization - make_integer (an_argument: INTEGER) is - -- Initialize an instance of JSON_NUMBER as INTEGER - do - item := an_argument.out - numeric_type := INTEGER_TYPE - end + make_integer (an_argument: INTEGER_64) is + -- Initialize an instance of JSON_NUMBER from the integer value of `an_argument'. + do + item := an_argument.out + numeric_type := INTEGER_TYPE + end - make_real (an_argument: DOUBLE) is - -- Initialize an instance of JSON_NUMBER as DOUBLE - do - item := an_argument.out - numeric_type := DOUBLE_TYPE - end + make_natural (an_argument: NATURAL_64) is + -- Initialize an instance of JSON_NUMBER from the unsigned integer value of `an_argument'. + do + item := an_argument.out + numeric_type := NATURAL_TYPE + 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 - item: STRING - -- Content + item: STRING + -- Content - hash_code: INTEGER is - --Hash code value - do - Result := item.hash_code - end + hash_code: INTEGER is + --Hash code value + do + Result := item.hash_code + end + representation: STRING is + do + Result := item + 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 + 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 - is_equal (other: like Current): BOOLEAN is - -- Is `other' attached to an object of the same type - -- as current object and identical to it? - do - Result := item.is_equal (other.item) - end + is_equal (other: like Current): BOOLEAN is + -- Is `other' attached to an object of the same type + -- as current object and identical to it? + do + Result := item.is_equal (other.item) + end feature -- Status report - debug_output: STRING - -- String that should be displayed in debugger to represent `Current'. - do - Result := item - end + debug_output: STRING + -- String that should be displayed in debugger to represent `Current'. + do + Result := item + end feature -- Implementation - INTEGER_TYPE: INTEGER is 1 - DOUBLE_TYPE: INTEGER is 2 + INTEGER_TYPE: INTEGER is 1 + DOUBLE_TYPE: INTEGER is 2 + NATURAL_TYPE: INTEGER is 3 - numeric_type: INTEGER + numeric_type: INTEGER invariant - item_not_void: item /= Void + item_not_void: item /= Void end diff --git a/library/kernel/json_object.e b/library/kernel/json_object.e index de5d0349..d91b1823 100644 --- a/library/kernel/json_object.e +++ b/library/kernel/json_object.e @@ -1,130 +1,152 @@ -indexing - - description: "[ - An JSON_OBJECT represent an object in JSON. - An object is an unordered set of name/value pairs - - Examples: - - object - {} - {"key","value"} - - ]" - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" - license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" - -class - JSON_OBJECT - -inherit - JSON_VALUE - -create - make - -feature {NONE} -- Initialization - - make is - -- Initialize - do - create object.make (10) - end - -feature -- Change Element - - put (value: ?JSON_VALUE; key: JSON_STRING) is - -- Assuming there is no item of key `key', - -- insert `value' with `key'. - require - key_not_present: not has_key (key) - local - l_value: ?JSON_VALUE - do - l_value := value - if l_value = Void then - create {JSON_NULL} l_value - end - object.extend (l_value, key) - end - -feature -- Access - - has_key (key: JSON_STRING): BOOLEAN is - -- has the JSON_OBJECT contains a specific key 'key'. - do - Result := object.has (key) - end - - has_item (value: JSON_VALUE): BOOLEAN is - -- has the JSON_OBJECT contain a specfic item 'value' - do - Result := object.has_item (value) - end - - item (key: JSON_STRING): ?JSON_VALUE is - -- the json_value associated with a key. - do - Result := object.item (key) - end - - current_keys: ARRAY [JSON_STRING] is - -- array containing actually used keys - do - 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 - - hash_code: INTEGER is - -- Hash code value - do - from - object.start - Result := object.item_for_iteration.hash_code - until - object.off - loop - Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code - 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 +indexing + + description: "[ + An JSON_OBJECT represent an object in JSON. + An object is an unordered set of name/value pairs + + Examples: + + object + {} + {"key","value"} + + ]" + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" + license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" + +class + JSON_OBJECT + +inherit + JSON_VALUE + +create + make + +feature {NONE} -- Initialization + + make is + -- Initialize + do + create object.make (10) + end + +feature -- Change Element + + put (value: ?JSON_VALUE; key: JSON_STRING) is + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + require + key_not_present: not has_key (key) + local + l_value: ?JSON_VALUE + do + l_value := value + if l_value = Void then + create {JSON_NULL} l_value + end + object.extend (l_value, key) + end + +feature -- Access + + has_key (key: JSON_STRING): BOOLEAN is + -- has the JSON_OBJECT contains a specific key 'key'. + do + Result := object.has (key) + end + + has_item (value: JSON_VALUE): BOOLEAN is + -- has the JSON_OBJECT contain a specfic item 'value' + do + Result := object.has_item (value) + end + + item (key: JSON_STRING): ?JSON_VALUE is + -- the json_value associated with a key. + do + Result := object.item (key) + end + + current_keys: ARRAY [JSON_STRING] is + -- array containing actually used keys + do + Result := object.current_keys + end + + representation: STRING is + local + t: HASH_TABLE [JSON_VALUE, JSON_STRING] + do + Result := "{" + from + t := map_representation + t.start + until + t.after + loop + Result.append (t.key_for_iteration.representation) + Result.append (":") + Result.append (t.item_for_iteration.representation) + t.forth + if not t.after then + Result.append_character (',') + end + end + Result.append_character ('}') + 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 + + hash_code: INTEGER is + -- Hash code value + do + from + object.start + Result := object.item_for_iteration.hash_code + until + object.off + loop + Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code + 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 diff --git a/library/kernel/json_string.e b/library/kernel/json_string.e index d7a2b0a0..39afdb0c 100644 --- a/library/kernel/json_string.e +++ b/library/kernel/json_string.e @@ -1,89 +1,108 @@ indexing - description:"[ - A JSON_STRING represent a string in JSON. - A string is a collection of zero or more Unicodes characters, wrapped in double - quotes, using blackslash espaces. - ]" + description:"[ + A JSON_STRING represent a string in JSON. + A string is a collection of zero or more Unicodes characters, wrapped in double + quotes, using blackslash espaces. + ]" - author: "Javier Velilla" - date: "2008/08/24" - revision: "Revision 0.1" - license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" + author: "Javier Velilla" + date: "2008/08/24" + revision: "Revision 0.1" + license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" class - JSON_STRING + JSON_STRING inherit - JSON_VALUE - redefine - is_equal - end + JSON_VALUE + redefine + is_equal + end create - make_json + make_json feature {NONE} -- Initialization - make_json (an_item: STRING) is - -- Initialize. - require - item_not_void: an_item /= Void - do - item := an_item - end + make_json (an_item: STRING) is + -- Initialize. + require + item_not_void: an_item /= Void + do + item := escaped_json_string (an_item) + end feature -- Access - item: STRING - -- Contents + item: STRING + -- Contents + representation: STRING is + do + Result := "%"" + Result.append (item) + Result.append_character ('%"') + end + 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 + 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 - is_equal (other: like Current): BOOLEAN is - -- Is JSON_STRING made of same character sequence as `other' - -- (possibly with a different capacity)? - do - Result := item.is_equal (other.item) - end + is_equal (other: like Current): BOOLEAN is + -- Is JSON_STRING made of same character sequence as `other' + -- (possibly with a different capacity)? + do + Result := item.is_equal (other.item) + end feature -- Change Element - append (a_string: STRING)is - -- Add an_item - require - a_string_not_void: a_string /= Void - do - item.append_string (a_string) - end + append (a_string: STRING)is + -- Add an_item + require + a_string_not_void: a_string /= Void + do + item.append_string (a_string) + end feature -- Status report - hash_code: INTEGER is - -- Hash code value - do - Result := item.hash_code - end + hash_code: INTEGER is + -- Hash code value + do + Result := item.hash_code + end feature -- Status report - debug_output: STRING - -- String that should be displayed in debugger to represent `Current'. - do - Result := item - end + debug_output: STRING + -- String that should be displayed in debugger to represent `Current'. + do + Result := item + 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 - value_not_void: item /= Void + value_not_void: item /= Void end diff --git a/library/kernel/json_value.e b/library/kernel/json_value.e index 8443e392..63aa7bbe 100644 --- a/library/kernel/json_value.e +++ b/library/kernel/json_value.e @@ -1,36 +1,43 @@ -indexing - description:"[ - JSON_VALUE represent a value in JSON. - A value can be - * a string in double quotes - * a number - * boolean value(true, false ) - * null - * an object - * an array - ]" - author: "Javier Velilla" - date: "2008/05/19" - revision: "Revision 0.1" - license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" - - -deferred class - JSON_VALUE - -inherit - HASHABLE - - DEBUG_OUTPUT - -feature -- Visitor pattern - - 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 - -end +indexing + description:"[ + JSON_VALUE represent a value in JSON. + A value can be + * a string in double quotes + * a number + * boolean value(true, false ) + * null + * an object + * an array + ]" + author: "Javier Velilla" + date: "2008/05/19" + revision: "Revision 0.1" + license:"MIT (see http://www.opensource.org/licenses/mit-license.php)" + + +deferred class + JSON_VALUE + +inherit + HASHABLE + + DEBUG_OUTPUT + +feature -- Access + + representation: STRING is + -- UTF-8 encoded Unicode string representation of Current + deferred + end + +feature -- Visitor pattern + + 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 + +end