Reformatted code to Gobo standard.
This commit is contained in:
@@ -51,9 +51,7 @@ feature -- Status report
|
|||||||
feature -- Change Element
|
feature -- Change Element
|
||||||
add(value:JSON_VALUE) is
|
add(value:JSON_VALUE) is
|
||||||
require
|
require
|
||||||
not_null:value /= void
|
not_null:value /= void
|
||||||
local
|
|
||||||
l_json_value:JSON_VALUE
|
|
||||||
do
|
do
|
||||||
values.extend(value)
|
values.extend(value)
|
||||||
ensure
|
ensure
|
||||||
@@ -87,16 +85,16 @@ feature -- Report
|
|||||||
hash_code:INTEGER is
|
hash_code:INTEGER is
|
||||||
--
|
--
|
||||||
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 {NONE} --Implementation
|
feature {NONE} --Implementation
|
||||||
|
|||||||
@@ -5,36 +5,40 @@ indexing
|
|||||||
revision: "$Revision$"
|
revision: "$Revision$"
|
||||||
|
|
||||||
class
|
class
|
||||||
|
|
||||||
JSON_BOOLEAN
|
JSON_BOOLEAN
|
||||||
inherit
|
|
||||||
JSON_VALUE
|
inherit
|
||||||
|
JSON_VALUE
|
||||||
|
|
||||||
create
|
create
|
||||||
|
|
||||||
make_boolean
|
make_boolean
|
||||||
|
|
||||||
feature -- Initialization
|
feature -- Initialization
|
||||||
|
|
||||||
make_boolean(a_value:BOOLEAN) is
|
make_boolean (an_item: BOOLEAN) is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
value:=a_value
|
item := an_item
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
|
|
||||||
to_json:STRING is
|
item: BOOLEAN
|
||||||
|
|
||||||
|
to_json: STRING is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
Result:=value.out
|
Result := item.out
|
||||||
end
|
end
|
||||||
|
|
||||||
hash_code:INTEGER is
|
hash_code: INTEGER is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
Result:=value.hash_code
|
Result := item.hash_code
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
value:BOOLEAN
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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: "$Date$"
|
date: "$Date$"
|
||||||
@@ -7,54 +8,73 @@ indexing
|
|||||||
|
|
||||||
class
|
class
|
||||||
JSON_NUMBER
|
JSON_NUMBER
|
||||||
inherit
|
|
||||||
JSON_VALUE
|
inherit
|
||||||
rename is_equal as is_equal_json_value
|
|
||||||
end
|
JSON_VALUE
|
||||||
|
rename
|
||||||
|
is_equal as is_equal_json_value
|
||||||
|
end
|
||||||
create
|
create
|
||||||
|
|
||||||
make_integer,
|
make_integer,
|
||||||
make_real
|
make_real
|
||||||
|
|
||||||
feature -- initialization
|
feature -- initialization
|
||||||
make_integer(argument:INTEGER) is
|
|
||||||
do
|
|
||||||
value:= argument.out
|
|
||||||
internal_hash_code:=argument.hash_code
|
|
||||||
numeric_type:="INTEGER"
|
|
||||||
end
|
|
||||||
|
|
||||||
make_real(argument:REAL) is
|
make_integer (argument: INTEGER) is
|
||||||
do
|
do
|
||||||
value:= argument.out
|
item:= argument.out
|
||||||
internal_hash_code:=argument.hash_code
|
internal_hash_code:=argument.hash_code
|
||||||
numeric_type:="REAL"
|
numeric_type:= "INTEGER"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
make_real (argument: REAL) is
|
||||||
|
do
|
||||||
|
item:= argument.out
|
||||||
|
internal_hash_code:=argument.hash_code
|
||||||
|
numeric_type:= "REAL"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
to_json:STRING is
|
|
||||||
--
|
|
||||||
do
|
|
||||||
Result:=value
|
|
||||||
end
|
|
||||||
|
|
||||||
hash_code:INTEGER is
|
item: STRING
|
||||||
|
|
||||||
|
hash_code: INTEGER is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
Result:=internal_hash_code
|
Result:=internal_hash_code
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
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:=value.is_equal(other.to_json)
|
Result:=item.is_equal (other.to_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Conversion
|
||||||
|
|
||||||
|
to_json: STRING is
|
||||||
|
--
|
||||||
|
do
|
||||||
|
Result := item
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Implementation
|
feature -- Implementation
|
||||||
value:STRING
|
|
||||||
internal_hash_code:INTEGER
|
internal_hash_code: INTEGER
|
||||||
numeric_type:STRING -- REAL or INTEGER
|
|
||||||
|
numeric_type: STRING -- REAL or INTEGER
|
||||||
|
|
||||||
|
|
||||||
|
invariant
|
||||||
|
|
||||||
|
item_not_void: item /= Void
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
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:
|
||||||
|
|
||||||
@@ -10,70 +11,75 @@ indexing
|
|||||||
{"key","value"}
|
{"key","value"}
|
||||||
|
|
||||||
|
|
||||||
]"
|
]"
|
||||||
author: "Javier Velilla"
|
author: "Javier Velilla"
|
||||||
date: "$Date$"
|
date: "$Date$"
|
||||||
revision: "$Revision$"
|
revision: "$Revision$"
|
||||||
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 -- Initialization
|
feature -- Initialization
|
||||||
|
|
||||||
make is
|
make is
|
||||||
--
|
--
|
||||||
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
|
||||||
not_present: not has_key (key)
|
not_present: not has_key (key)
|
||||||
local
|
local
|
||||||
l_json_null:JSON_NULL
|
l_json_null: JSON_NULL
|
||||||
l_value:JSON_VALUE
|
l_value: JSON_VALUE
|
||||||
do
|
do
|
||||||
l_value:=value
|
l_value:=value
|
||||||
if value = void then
|
if value = void then
|
||||||
create l_json_null
|
create l_json_null
|
||||||
l_value:=l_json_null
|
l_value:=l_json_null
|
||||||
end
|
|
||||||
object.extend(l_value,key)
|
|
||||||
end
|
end
|
||||||
|
object.extend (l_value,key)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
has_key(key:JSON_STRING):BOOLEAN is
|
|
||||||
-- has the JSON_OBJECT contains a specific key 'key'.
|
has_key (key: JSON_STRING):BOOLEAN is
|
||||||
|
-- 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
|
||||||
@@ -81,32 +87,31 @@ feature -- Access
|
|||||||
|
|
||||||
|
|
||||||
feature -- Report
|
feature -- Report
|
||||||
to_json:STRING is
|
|
||||||
|
to_json: STRING is
|
||||||
-- Printable json representation
|
-- Printable json representation
|
||||||
-- {} or {member}
|
-- {} or {member}
|
||||||
-- see documentation
|
-- see documentation
|
||||||
do
|
do
|
||||||
create Result.make_empty
|
create Result.make_empty
|
||||||
Result.append ("{")
|
Result.append ("{")
|
||||||
from
|
from
|
||||||
object.start
|
object.start
|
||||||
until
|
until
|
||||||
object.off
|
object.off
|
||||||
loop
|
loop
|
||||||
Result.append (object.item_for_iteration.to_json)
|
Result.append (object.item_for_iteration.to_json)
|
||||||
Result.append (":")
|
Result.append (":")
|
||||||
Result.append (object.key_for_iteration.to_json)
|
Result.append (object.key_for_iteration.to_json)
|
||||||
object.forth
|
object.forth
|
||||||
if not object.after then
|
if not object.after then
|
||||||
Result.append (",")
|
Result.append (",")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
Result.append ("}")
|
|
||||||
end
|
end
|
||||||
|
Result.append ("}")
|
||||||
|
end
|
||||||
|
|
||||||
|
hash_code: INTEGER is
|
||||||
|
|
||||||
hash_code: INTEGER is
|
|
||||||
-- Hash code value
|
-- Hash code value
|
||||||
local
|
local
|
||||||
do
|
do
|
||||||
@@ -119,11 +124,13 @@ feature -- Report
|
|||||||
Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code
|
Result := ((Result \\ 8388593) |<< 8) + object.item_for_iteration.hash_code
|
||||||
object.forth
|
object.forth
|
||||||
end
|
end
|
||||||
-- Ensure it is a positive value.
|
-- Ensure it is a positive value.
|
||||||
Result := Result.hash_code
|
Result := Result.hash_code
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
feature {NONE} -- Implementation
|
||||||
object:HASH_TABLE[JSON_VALUE,JSON_STRING]
|
|
||||||
|
object: HASH_TABLE[JSON_VALUE,JSON_STRING]
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,68 +1,86 @@
|
|||||||
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: "$Date$"
|
date: "$Date$"
|
||||||
revision: "$Revision$"
|
revision: "$Revision$"
|
||||||
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
|
license:"MIT (see http://www.opensource.org/licenses/mit-license.php)"
|
||||||
|
|
||||||
class
|
|
||||||
JSON_STRING
|
|
||||||
inherit
|
|
||||||
JSON_VALUE
|
|
||||||
redefine
|
|
||||||
is_equal
|
|
||||||
end
|
|
||||||
|
|
||||||
create
|
class
|
||||||
make_json
|
|
||||||
|
JSON_STRING
|
||||||
|
|
||||||
|
inherit
|
||||||
|
|
||||||
|
JSON_VALUE
|
||||||
|
redefine
|
||||||
|
is_equal
|
||||||
|
end
|
||||||
|
|
||||||
|
create
|
||||||
|
|
||||||
|
make_json
|
||||||
|
|
||||||
feature -- Initialization
|
feature -- Initialization
|
||||||
make_json(value:STRING) is
|
|
||||||
--
|
make_json (an_item: STRING) is
|
||||||
do
|
-- Initialize.
|
||||||
create buffer.make(256)
|
require
|
||||||
buffer.append (value)
|
item_not_void: an_item /= Void
|
||||||
end
|
do
|
||||||
|
item := an_item
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
|
|
||||||
|
item: STRING
|
||||||
|
-- Contents
|
||||||
|
|
||||||
|
|
||||||
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:= Current.to_json.is_equal (other.to_json)
|
Result := Current.to_json.is_equal (other.to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Change Element
|
feature -- Change Element
|
||||||
append(value:STRING)is
|
|
||||||
|
append (an_item: STRING)is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
buffer.append (value)
|
item.append_string (an_item)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Status report
|
feature -- Status report
|
||||||
to_json:STRING is
|
|
||||||
--
|
|
||||||
do
|
|
||||||
create Result.make_empty
|
|
||||||
Result.append ("%"")
|
|
||||||
Result.append (buffer)
|
|
||||||
Result.append ("%"")
|
|
||||||
end
|
|
||||||
|
|
||||||
hash_code:INTEGER is
|
to_json: STRING is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
Result:= buffer.hash_code + buffer.count
|
create Result.make_empty
|
||||||
end
|
Result.append ("%"")
|
||||||
|
Result.append (item)
|
||||||
|
Result.append ("%"")
|
||||||
|
end
|
||||||
|
|
||||||
|
hash_code: INTEGER is
|
||||||
|
--
|
||||||
|
do
|
||||||
|
Result := item.hash_code
|
||||||
|
end
|
||||||
|
|
||||||
|
invariant
|
||||||
|
|
||||||
|
value_not_void: item /= Void
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
|
||||||
buffer:STRING
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,392 +1,397 @@
|
|||||||
indexing
|
indexing
|
||||||
description: "Objects that ..."
|
|
||||||
|
description: "Parse serialized JSON data"
|
||||||
author: ""
|
author: ""
|
||||||
date: "$Date$"
|
date: "$Date$"
|
||||||
revision: "$Revision$"
|
revision: "$Revision$"
|
||||||
|
|
||||||
class
|
class
|
||||||
|
|
||||||
JSON_PARSER
|
JSON_PARSER
|
||||||
inherit
|
inherit
|
||||||
JSON_READER
|
JSON_READER
|
||||||
JSON_TOKENS
|
JSON_TOKENS
|
||||||
|
|
||||||
create
|
create
|
||||||
|
|
||||||
make_parser
|
make_parser
|
||||||
|
|
||||||
|
feature {NONE} -- Initialize
|
||||||
|
|
||||||
|
make_parser (a_json: STRING) is
|
||||||
|
-- Initialize.
|
||||||
|
require
|
||||||
|
json_not_empty: a_json /= Void and then not a_json.is_empty
|
||||||
|
do
|
||||||
|
make (a_json)
|
||||||
|
is_parsed := True
|
||||||
|
create current_errors.make_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
is_parsed:BOOLEAN
|
|
||||||
|
|
||||||
current_errors:STRING
|
is_parsed: BOOLEAN
|
||||||
|
|
||||||
make_parser(a_json:STRING) is
|
current_errors: STRING
|
||||||
|
|
||||||
|
|
||||||
|
feature -- Commands
|
||||||
|
|
||||||
|
parse_json: JSON_VALUE is
|
||||||
--
|
--
|
||||||
do
|
do
|
||||||
make(a_json)
|
Result := parse
|
||||||
is_parsed:=true
|
if extra_elements then
|
||||||
create current_errors.make_empty
|
is_parsed := false
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
parse_json:JSON_VALUE is
|
parse: JSON_VALUE is
|
||||||
--
|
|
||||||
do
|
|
||||||
Result:=parse
|
|
||||||
if extra_elements then
|
|
||||||
is_parsed:=false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
parse:JSON_VALUE is
|
|
||||||
--
|
--
|
||||||
local
|
local
|
||||||
c:CHARACTER
|
c: CHARACTER
|
||||||
do
|
do
|
||||||
if is_parsed then
|
if is_parsed then
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
c:=actual
|
c := actual
|
||||||
if c.is_equal (j_object_open) then
|
if c.is_equal (j_object_open) then
|
||||||
Result:=parse_object
|
Result := parse_object
|
||||||
elseif c.is_equal (j_string) then
|
elseif c.is_equal (j_string) then
|
||||||
Result:=parse_string
|
Result := parse_string
|
||||||
elseif c.is_equal (j_array_open) then
|
elseif c.is_equal (j_array_open) then
|
||||||
Result:=parse_array
|
Result := parse_array
|
||||||
elseif c.is_digit or c.is_equal (j_minus) then
|
elseif c.is_digit or c.is_equal (j_minus) then
|
||||||
Result:=parse_number
|
Result := parse_number
|
||||||
elseif is_null then
|
elseif is_null then
|
||||||
--
|
--
|
||||||
Result:=create {JSON_NULL}
|
Result := create {JSON_NULL}
|
||||||
next;next;next;
|
next;next;next
|
||||||
elseif is_true then
|
elseif is_true then
|
||||||
Result:=create {JSON_BOOLEAN}.make_boolean (true)
|
Result := create {JSON_BOOLEAN}.make_boolean (true)
|
||||||
next;next;next;
|
next;next;next
|
||||||
elseif is_false then
|
elseif is_false then
|
||||||
Result:=create {JSON_BOOLEAN}.make_boolean (false)
|
Result := create {JSON_BOOLEAN}.make_boolean (false)
|
||||||
next;next;next;next;
|
next;next;next;next
|
||||||
else
|
else
|
||||||
is_parsed:=false
|
is_parsed := false
|
||||||
current_errors.append("JSON is not well formed in parse")
|
current_errors.append ("JSON is not well formed in parse")
|
||||||
Result:=void
|
Result := void
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
parse_object:JSON_OBJECT is
|
parse_object: JSON_OBJECT is
|
||||||
-- object
|
-- object
|
||||||
-- {}
|
-- {}
|
||||||
-- {"key" : "value" [,]}
|
-- {"key" : "value" [,]}
|
||||||
|
|
||||||
local
|
local
|
||||||
has_more:BOOLEAN
|
has_more: BOOLEAN
|
||||||
l_json_string:JSON_STRING
|
l_json_string: JSON_STRING
|
||||||
l_value:JSON_VALUE
|
l_value: JSON_VALUE
|
||||||
do
|
do
|
||||||
create Result.make
|
create Result.make
|
||||||
-- check if is an empty object {}
|
-- check if is an empty object {}
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
if actual.is_equal (j_object_close) then
|
if actual.is_equal (j_object_close) then
|
||||||
--is an empty object
|
--is an empty object
|
||||||
else
|
else
|
||||||
-- a complex object {"key" : "value"}
|
-- a complex object {"key" : "value"}
|
||||||
previous
|
previous
|
||||||
from has_more:=true until not has_more
|
from has_more := true until not has_more
|
||||||
loop
|
loop
|
||||||
|
next
|
||||||
|
skip_withe_spaces
|
||||||
|
l_json_string := parse_string
|
||||||
|
next
|
||||||
|
skip_withe_spaces
|
||||||
|
if actual.is_equal (':') then
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
l_json_string:=parse_string
|
else
|
||||||
next
|
is_parsed := false
|
||||||
skip_withe_spaces
|
current_errors.append ("%N Input string is a not well formed JSON, expected: : found: " + actual.out +"%N")
|
||||||
if actual.is_equal (':') then
|
has_more := false
|
||||||
next
|
end
|
||||||
skip_withe_spaces
|
|
||||||
else
|
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("%N Input string is a not well formed JSON, expected: : found:" + actual.out +"%N")
|
|
||||||
has_more:=false
|
|
||||||
end
|
|
||||||
|
|
||||||
l_value:=parse
|
l_value := parse
|
||||||
if is_parsed then
|
if is_parsed then
|
||||||
Result.put (l_value,l_json_string)
|
Result.put (l_value,l_json_string)
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
if actual.is_equal (j_object_close) then
|
if actual.is_equal (j_object_close) then
|
||||||
has_more:=false
|
has_more := false
|
||||||
elseif not actual.is_equal (',') then
|
elseif not actual.is_equal (',') then
|
||||||
has_more:=false
|
has_more := false
|
||||||
is_parsed:=false
|
is_parsed := false
|
||||||
current_errors.append("JSON Object sintactically malformed expected , found:[" + actual.out + "] %N")
|
current_errors.append ("JSON Object sintactically malformed expected , found: [" + actual.out + "] %N")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
has_more:=false
|
has_more := false
|
||||||
-- explain the error
|
-- explain the error
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
parse_string:JSON_STRING is
|
parse_string: JSON_STRING is
|
||||||
--
|
--
|
||||||
local
|
local
|
||||||
has_more:BOOLEAN
|
has_more: BOOLEAN
|
||||||
l_json_string:STRING
|
l_json_string: STRING
|
||||||
l_unicode:STRING
|
l_unicode: STRING
|
||||||
do
|
do
|
||||||
create l_json_string.make_empty
|
create l_json_string.make_empty
|
||||||
if actual.is_equal (j_string) then
|
if actual.is_equal (j_string) then
|
||||||
from
|
from
|
||||||
has_more:=true
|
has_more := true
|
||||||
until not has_more
|
until not has_more
|
||||||
|
|
||||||
loop
|
loop
|
||||||
|
next
|
||||||
|
if actual.is_equal (j_string) then
|
||||||
|
has_more := false
|
||||||
|
elseif actual.is_equal ('%H') then
|
||||||
next
|
next
|
||||||
if actual.is_equal (j_string) then
|
if actual.is_equal ('u') then
|
||||||
has_more:=false
|
create l_unicode.make_from_string ("\u")
|
||||||
elseif actual.is_equal ('%H') then
|
l_unicode.append (read_unicode)
|
||||||
next
|
if is_a_valid_unicode (l_unicode) then
|
||||||
if actual.is_equal ('u') then
|
l_json_string.append (l_unicode)
|
||||||
create l_unicode.make_from_string ("\u")
|
|
||||||
l_unicode.append (read_unicode)
|
|
||||||
if is_a_valid_unicode(l_unicode) then
|
|
||||||
l_json_string.append (l_unicode)
|
|
||||||
else
|
|
||||||
has_more:=false
|
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("Input String is not well formed JSON, expected a Unicode value, found [" + actual.out + " ] %N")
|
|
||||||
end
|
|
||||||
elseif (not special_characters.has (actual) and not special_controls.has (actual)) or actual.is_equal ('%N') then
|
|
||||||
has_more:=false
|
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("Input String is not well formed JSON, found [" + actual.out + " ] %N")
|
|
||||||
|
|
||||||
else
|
else
|
||||||
l_json_string.append ("\")
|
has_more := false
|
||||||
l_json_string.append (actual.out)
|
is_parsed := false
|
||||||
|
current_errors.append ("Input String is not well formed JSON, expected a Unicode value, found [" + actual.out + " ] %N")
|
||||||
end
|
end
|
||||||
|
elseif (not special_characters.has (actual) and not special_controls.has (actual)) or actual.is_equal ('%N') then
|
||||||
|
has_more := false
|
||||||
|
is_parsed := false
|
||||||
|
current_errors.append ("Input String is not well formed JSON, found [" + actual.out + " ] %N")
|
||||||
|
|
||||||
else
|
else
|
||||||
if special_characters.has (actual) and not actual.is_equal ('/') then
|
l_json_string.append ("\")
|
||||||
has_more:=false
|
l_json_string.append (actual.out)
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("Input String is not well formed JSON, found [" + actual.out + " ] %N")
|
end
|
||||||
else
|
|
||||||
l_json_string.append (actual.out)
|
else
|
||||||
end
|
if special_characters.has (actual) and not actual.is_equal ('/') then
|
||||||
|
has_more := false
|
||||||
|
is_parsed := false
|
||||||
|
current_errors.append ("Input String is not well formed JSON, found [" + actual.out + " ] %N")
|
||||||
|
else
|
||||||
|
l_json_string.append (actual.out)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
create Result.make_json (l_json_string)
|
end
|
||||||
else
|
create Result.make_json (l_json_string)
|
||||||
Result := void
|
else
|
||||||
end
|
Result := void
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
parse_array:JSON_ARRAY is
|
parse_array: JSON_ARRAY is
|
||||||
-- array
|
-- array
|
||||||
-- []
|
-- []
|
||||||
-- [elements [,]]
|
-- [elements [,]]
|
||||||
local
|
local
|
||||||
flag:BOOLEAN
|
flag: BOOLEAN
|
||||||
l_value:JSON_VALUE
|
l_value: JSON_VALUE
|
||||||
do
|
do
|
||||||
create Result.make_array
|
create Result.make_array
|
||||||
--check if is an empty array []
|
--check if is an empty array []
|
||||||
next
|
next
|
||||||
skip_withe_spaces
|
skip_withe_spaces
|
||||||
if actual.is_equal (j_array_close) then
|
if actual.is_equal (j_array_close) then
|
||||||
--is an empty array
|
--is an empty array
|
||||||
else
|
else
|
||||||
previous
|
previous
|
||||||
from
|
from
|
||||||
flag:=true
|
flag := true
|
||||||
until
|
until
|
||||||
not flag
|
not flag
|
||||||
loop
|
|
||||||
next
|
|
||||||
skip_withe_spaces
|
|
||||||
l_value := parse
|
|
||||||
if is_parsed then
|
|
||||||
Result.add (l_value)
|
|
||||||
next
|
|
||||||
skip_withe_spaces
|
|
||||||
if not actual.is_equal (j_array_close) and not actual.is_equal (',')then
|
|
||||||
flag:=false
|
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("Array is not well formed JSON, found [" + actual.out + " ] %N")
|
|
||||||
elseif actual.is_equal (j_array_close) then
|
|
||||||
flag:= false
|
|
||||||
end
|
|
||||||
else
|
|
||||||
flag:=false
|
|
||||||
current_errors.append("Array is not well formed JSON, found [" + actual.out + " ] %N")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
parse_number:JSON_NUMBER is
|
|
||||||
--
|
|
||||||
local
|
|
||||||
sb:STRING
|
|
||||||
flag:BOOLEAN
|
|
||||||
is_integer:BOOLEAN
|
|
||||||
do
|
|
||||||
create sb.make_empty
|
|
||||||
sb.append (actual.out)
|
|
||||||
|
|
||||||
from
|
|
||||||
flag:=true
|
|
||||||
until not flag
|
|
||||||
loop
|
loop
|
||||||
next
|
next
|
||||||
if not has_next or close_tokens.has (actual) or actual.is_equal (',')
|
skip_withe_spaces
|
||||||
or actual.is_equal ('%N') or actual.is_equal ('%R') then
|
l_value := parse
|
||||||
flag:=false
|
if is_parsed then
|
||||||
previous
|
Result.add (l_value)
|
||||||
else
|
|
||||||
sb.append (actual.out)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_a_valid_number(sb) then
|
|
||||||
if sb.is_integer then
|
|
||||||
create Result.make_integer (sb.to_integer)
|
|
||||||
is_integer:=true;
|
|
||||||
elseif sb.is_double and not is_integer then
|
|
||||||
create Result.make_real (sb.to_double)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
is_parsed:=false
|
|
||||||
current_errors.append("Expected a number, found: [ " + sb + " ]")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
is_null:BOOLEAN is
|
|
||||||
--
|
|
||||||
local
|
|
||||||
l_null:STRING
|
|
||||||
l_string:STRING
|
|
||||||
do
|
|
||||||
l_null:="null"
|
|
||||||
l_string:=json_substring (index,index + l_null.count - 1)
|
|
||||||
if l_string.is_equal (l_null) then
|
|
||||||
Result := true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
is_false:BOOLEAN is
|
|
||||||
--
|
|
||||||
local
|
|
||||||
l_false:STRING
|
|
||||||
l_string:STRING
|
|
||||||
do
|
|
||||||
l_false:="false"
|
|
||||||
l_string:=json_substring (index,index + l_false.count - 1)
|
|
||||||
if l_string.is_equal (l_false) then
|
|
||||||
Result := true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
is_true:BOOLEAN is
|
|
||||||
--
|
|
||||||
local
|
|
||||||
l_true:STRING
|
|
||||||
l_string:STRING
|
|
||||||
do
|
|
||||||
l_true:="true"
|
|
||||||
l_string:=json_substring (index,index + l_true.count - 1)
|
|
||||||
if l_string.is_equal (l_true) then
|
|
||||||
Result := true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
read_unicode:STRING is
|
|
||||||
--
|
|
||||||
local
|
|
||||||
i:INTEGER
|
|
||||||
do
|
|
||||||
create Result.make_empty
|
|
||||||
from
|
|
||||||
i:=1
|
|
||||||
until i > 4 or not has_next
|
|
||||||
|
|
||||||
loop
|
|
||||||
next
|
|
||||||
Result.append(actual.out)
|
|
||||||
i:= i + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
feature {NONE}
|
|
||||||
|
|
||||||
is_a_valid_number(a_number:STRING):BOOLEAN is
|
|
||||||
-- is 'a_number' a valid number based on this regular expression
|
|
||||||
-- "-?(?:0|[1-9]\d+)(?:\.\d+)?(?:[eE][+-]?\d+)?\b"?
|
|
||||||
local
|
|
||||||
case_mapping: RX_CASE_MAPPING
|
|
||||||
word_set: RX_CHARACTER_SET
|
|
||||||
regexp: RX_PCRE_REGULAR_EXPRESSION
|
|
||||||
number_regex:STRING
|
|
||||||
l_number:STRING
|
|
||||||
do
|
|
||||||
create regexp.make
|
|
||||||
create word_set.make_empty
|
|
||||||
word_set.add_string ("0123456789.eE+-")
|
|
||||||
regexp.set_word_set (word_set)
|
|
||||||
number_regex:="-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\b"
|
|
||||||
regexp.compile(number_regex)
|
|
||||||
if regexp.matches (a_number) then
|
|
||||||
a_number.right_adjust
|
|
||||||
if a_number.is_equal(regexp.captured_substring (0)) then
|
|
||||||
Result := true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
is_a_valid_unicode(a_unicode:STRING):BOOLEAN is
|
|
||||||
-- is 'a_unicode' a valid unicode based on this regular expression
|
|
||||||
-- "\\u[0-9a-fA-F]{4}"
|
|
||||||
local
|
|
||||||
case_mapping: RX_CASE_MAPPING
|
|
||||||
word_set: RX_CHARACTER_SET
|
|
||||||
regexp: RX_PCRE_REGULAR_EXPRESSION
|
|
||||||
unicode_regex:STRING
|
|
||||||
do
|
|
||||||
create regexp.make
|
|
||||||
unicode_regex:="\\u[0-9a-fA-F]{4}"
|
|
||||||
regexp.compile(unicode_regex)
|
|
||||||
if regexp.matches (a_unicode) then
|
|
||||||
Result := true
|
|
||||||
check
|
|
||||||
is_valid: a_unicode.is_equal(regexp.captured_substring (0))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
extra_elements:BOOLEAN is
|
|
||||||
--has more elements?
|
|
||||||
local
|
|
||||||
l_string:STRING
|
|
||||||
do
|
|
||||||
|
|
||||||
if has_next then
|
|
||||||
next
|
|
||||||
end
|
|
||||||
from
|
|
||||||
until not actual.is_space or not actual.is_equal ('%R') or
|
|
||||||
not actual.is_equal ('%U') or not actual.is_equal ('%T')
|
|
||||||
or not actual.is_equal ('%N') or not has_next
|
|
||||||
loop
|
|
||||||
next
|
next
|
||||||
|
skip_withe_spaces
|
||||||
|
if not actual.is_equal (j_array_close) and not actual.is_equal (',')then
|
||||||
|
flag := false
|
||||||
|
is_parsed := false
|
||||||
|
current_errors.append ("Array is not well formed JSON, found [" + actual.out + " ] %N")
|
||||||
|
elseif actual.is_equal (j_array_close) then
|
||||||
|
flag := false
|
||||||
|
end
|
||||||
|
else
|
||||||
|
flag := false
|
||||||
|
current_errors.append ("Array is not well formed JSON, found [" + actual.out + " ] %N")
|
||||||
end
|
end
|
||||||
|
|
||||||
if has_next then
|
|
||||||
Result:=True
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
parse_number: JSON_NUMBER is
|
||||||
|
--
|
||||||
|
local
|
||||||
|
sb: STRING
|
||||||
|
flag: BOOLEAN
|
||||||
|
is_integer: BOOLEAN
|
||||||
|
do
|
||||||
|
create sb.make_empty
|
||||||
|
sb.append (actual.out)
|
||||||
|
|
||||||
|
from
|
||||||
|
flag := true
|
||||||
|
until not flag
|
||||||
|
loop
|
||||||
|
next
|
||||||
|
if not has_next or close_tokens.has (actual) or actual.is_equal (',')
|
||||||
|
or actual.is_equal ('%N') or actual.is_equal ('%R') then
|
||||||
|
flag := false
|
||||||
|
previous
|
||||||
|
else
|
||||||
|
sb.append (actual.out)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_a_valid_number (sb) then
|
||||||
|
if sb.is_integer then
|
||||||
|
create Result.make_integer (sb.to_integer)
|
||||||
|
is_integer := true
|
||||||
|
elseif sb.is_double and not is_integer then
|
||||||
|
create Result.make_real (sb.to_double)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
is_parsed := false
|
||||||
|
current_errors.append ("Expected a number, found: [ " + sb + " ]")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
is_null: BOOLEAN is
|
||||||
|
--
|
||||||
|
local
|
||||||
|
l_null: STRING
|
||||||
|
l_string: STRING
|
||||||
|
do
|
||||||
|
l_null := "null"
|
||||||
|
l_string := json_substring (index,index + l_null.count - 1)
|
||||||
|
if l_string.is_equal (l_null) then
|
||||||
|
Result := true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
is_false: BOOLEAN is
|
||||||
|
--
|
||||||
|
local
|
||||||
|
l_false: STRING
|
||||||
|
l_string: STRING
|
||||||
|
do
|
||||||
|
l_false := "false"
|
||||||
|
l_string := json_substring (index,index + l_false.count - 1)
|
||||||
|
if l_string.is_equal (l_false) then
|
||||||
|
Result := true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
is_true: BOOLEAN is
|
||||||
|
--
|
||||||
|
local
|
||||||
|
l_true: STRING
|
||||||
|
l_string: STRING
|
||||||
|
do
|
||||||
|
l_true := "true"
|
||||||
|
l_string := json_substring (index,index + l_true.count - 1)
|
||||||
|
if l_string.is_equal (l_true) then
|
||||||
|
Result := true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
read_unicode: STRING is
|
||||||
|
--
|
||||||
|
local
|
||||||
|
i: INTEGER
|
||||||
|
do
|
||||||
|
create Result.make_empty
|
||||||
|
from
|
||||||
|
i := 1
|
||||||
|
until i > 4 or not has_next
|
||||||
|
|
||||||
|
loop
|
||||||
|
next
|
||||||
|
Result.append (actual.out)
|
||||||
|
i := i + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
feature {NONE} -- Implementation
|
||||||
|
|
||||||
|
is_a_valid_number (a_number: STRING): BOOLEAN is
|
||||||
|
-- is 'a_number' a valid number based on this regular expression
|
||||||
|
-- "-?(?: 0|[1-9]\d+)(?: \.\d+)?(?: [eE][+-]?\d+)?\b"?
|
||||||
|
local
|
||||||
|
word_set: RX_CHARACTER_SET
|
||||||
|
regexp: RX_PCRE_REGULAR_EXPRESSION
|
||||||
|
number_regex: STRING
|
||||||
|
do
|
||||||
|
create regexp.make
|
||||||
|
create word_set.make_empty
|
||||||
|
word_set.add_string ("0123456789.eE+-")
|
||||||
|
regexp.set_word_set (word_set)
|
||||||
|
number_regex := "-?(?: 0|[1-9]\d*)(?: \.\d+)?(?: [eE][+-]?\d+)?\b"
|
||||||
|
regexp.compile (number_regex)
|
||||||
|
if regexp.matches (a_number) then
|
||||||
|
a_number.right_adjust
|
||||||
|
if a_number.is_equal (regexp.captured_substring (0)) then
|
||||||
|
Result := true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
is_a_valid_unicode (a_unicode: STRING): BOOLEAN is
|
||||||
|
-- is 'a_unicode' a valid unicode based on this regular expression
|
||||||
|
-- "\\u[0-9a-fA-F]{4}"
|
||||||
|
local
|
||||||
|
regexp: RX_PCRE_REGULAR_EXPRESSION
|
||||||
|
unicode_regex: STRING
|
||||||
|
do
|
||||||
|
create regexp.make
|
||||||
|
unicode_regex := "\\u[0-9a-fA-F]{4}"
|
||||||
|
regexp.compile (unicode_regex)
|
||||||
|
if regexp.matches (a_unicode) then
|
||||||
|
Result := true
|
||||||
|
check
|
||||||
|
is_valid: a_unicode.is_equal (regexp.captured_substring (0))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
extra_elements: BOOLEAN is
|
||||||
|
--has more elements?
|
||||||
|
do
|
||||||
|
|
||||||
|
if has_next then
|
||||||
|
next
|
||||||
|
end
|
||||||
|
from
|
||||||
|
until
|
||||||
|
not actual.is_space or not actual.is_equal ('%R') or
|
||||||
|
not actual.is_equal ('%U') or not actual.is_equal ('%T')
|
||||||
|
or not actual.is_equal ('%N') or not has_next
|
||||||
|
loop
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
if has_next then
|
||||||
|
Result := True
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user