Updated eJSON to use Eiffel 6.8 version.

Basically the changes are:

Replace ? by detachable
indexing by note
removing `is' from features, and in some places replaced by =
In the ecf now we need to include every gobo library, because the gobo.ecf, exclude libraries that are needed.
TODO: the test-suite is not void-safety.
This commit is contained in:
jvelilla
2011-07-07 12:03:25 +00:00
parent 94c5c90eaa
commit f4c472cb9f
26 changed files with 1009 additions and 929 deletions

View File

@@ -1,4 +1,4 @@
indexing
note
description: "Objects that ..."
author: ""
date: "$Date$"
@@ -9,11 +9,11 @@ class
feature -- Access
read_json_from (a_path: STRING): ?STRING is
read_json_from (a_path: STRING): detachable STRING
local
l_file: PLAIN_TEXT_FILE
template_content: STRING
l_last_string: ?STRING
l_last_string: detachable STRING
do
create l_file.make (a_path)
-- We perform several checks until we make a real attempt to open the file.

View File

@@ -1,4 +1,4 @@
indexing
note
description:
"JSON Visitor"
@@ -14,42 +14,42 @@ deferred class
feature -- Visitor Pattern
visit_json_array (a_json_array: JSON_ARRAY) is
visit_json_array (a_json_array: JSON_ARRAY)
-- Visit `a_json_array'.
require
a_json_array_not_void: a_json_array /= Void
deferred
end
visit_json_boolean (a_json_boolean: JSON_BOOLEAN) is
visit_json_boolean (a_json_boolean: JSON_BOOLEAN)
-- Visit `a_json_boolean'.
require
a_json_boolean_not_void: a_json_boolean /= Void
deferred
end
visit_json_null (a_json_null: JSON_NULL) is
visit_json_null (a_json_null: JSON_NULL)
-- Visit `a_json_null'.
require
a_json_null_not_void: a_json_null /= Void
deferred
end
visit_json_number (a_json_number: JSON_NUMBER) is
visit_json_number (a_json_number: JSON_NUMBER)
-- Visit `a_json_number'.
require
a_json_number_not_void: a_json_number /= Void
deferred
end
visit_json_object (a_json_object: JSON_OBJECT) is
visit_json_object (a_json_object: JSON_OBJECT)
-- Visit `a_json_object'.
require
a_json_object_not_void: a_json_object /= Void
deferred
end
visit_json_string (a_json_string: JSON_STRING) is
visit_json_string (a_json_string: JSON_STRING)
-- Visit `a_json_string'.
require
a_json_string_not_void: a_json_string /= Void

View File

@@ -1,4 +1,4 @@
indexing
note
description: "PRINT_JSON_VISITOR Generates the JSON-String for a JSON_VALUE"
author: "jvelilla"
date: "2008/08/24"
@@ -14,7 +14,7 @@ create make
feature -- Initialization
make is
make
-- Create a new instance
do
create to_json.make_empty
@@ -27,7 +27,7 @@ feature -- Access
feature -- Visitor Pattern
visit_json_array (a_json_array: JSON_ARRAY) is
visit_json_array (a_json_array: JSON_ARRAY)
-- Visit `a_json_array'.
local
value: JSON_VALUE
@@ -50,25 +50,25 @@ feature -- Visitor Pattern
to_json.append ("]")
end
visit_json_boolean (a_json_boolean: JSON_BOOLEAN) is
visit_json_boolean (a_json_boolean: JSON_BOOLEAN)
-- Visit `a_json_boolean'.
do
to_json.append (a_json_boolean.item.out)
end
visit_json_null (a_json_null: JSON_NULL) is
visit_json_null (a_json_null: JSON_NULL)
-- Visit `a_json_null'.
do
to_json.append ("null")
end
visit_json_number (a_json_number: JSON_NUMBER) is
visit_json_number (a_json_number: JSON_NUMBER)
-- Visit `a_json_number'.
do
to_json.append (a_json_number.item)
end
visit_json_object (a_json_object: JSON_OBJECT) is
visit_json_object (a_json_object: JSON_OBJECT)
-- Visit `a_json_object'.
local
l_pairs: HASH_TABLE[JSON_VALUE,JSON_STRING]
@@ -91,7 +91,7 @@ feature -- Visitor Pattern
to_json.append ("}")
end
visit_json_string (a_json_string: JSON_STRING) is
visit_json_string (a_json_string: JSON_STRING)
-- Visit `a_json_string'.
do
to_json.append ("%"")

View File

@@ -1,81 +1,81 @@
indexing
description: "A JSON converter for DS_HASH_TABLE [ANY, HASHABLE]"
author: "Paul Cohen"
date: "$Date: $"
revision: "$Revision: $"
file: "$HeadURL: $"
class JSON_DS_HASH_TABLE_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make is
do
create object.make (0)
end
feature -- Access
value: JSON_OBJECT
object: DS_HASH_TABLE [ANY, HASHABLE]
feature -- Conversion
from_json (j: like value): like object is
local
keys: ARRAY [JSON_STRING]
i: INTEGER
h: HASHABLE
a: ANY
do
keys := j.current_keys
create Result.make (keys.count)
from
i := 1
until
i > keys.count
loop
h ?= json.object (keys [i], void)
check h /= Void end
a := json.object (j.item (keys [i]), Void)
Result.put (a, h)
i := i + 1
end
end
to_json (o: like object): like value is
local
c: DS_HASH_TABLE_CURSOR [ANY, HASHABLE]
js: JSON_STRING
jv: JSON_VALUE
failed: BOOLEAN
do
create Result.make
from
c := o.new_cursor
c.start
until
c.after
loop
create js.make_json (c.key.out)
jv := json.value (c.item)
if jv /= Void then
Result.put (jv, js)
else
failed := True
end
c.forth
end
if failed then
Result := Void
end
end
end -- class JSON_DS_HASH_TABLE_CONVERTER
note
description: "A JSON converter for DS_HASH_TABLE [ANY, HASHABLE]"
author: "Paul Cohen"
date: "$Date: $"
revision: "$Revision: $"
file: "$HeadURL: $"
class JSON_DS_HASH_TABLE_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make
do
create object.make (0)
end
feature -- Access
value: JSON_OBJECT
object: DS_HASH_TABLE [ANY, HASHABLE]
feature -- Conversion
from_json (j: like value): detachable like object
local
keys: ARRAY [JSON_STRING]
i: INTEGER
h: HASHABLE
a: ANY
do
keys := j.current_keys
create Result.make (keys.count)
from
i := 1
until
i > keys.count
loop
h ?= json.object (keys [i], void)
check h /= Void end
a := json.object (j.item (keys [i]), Void)
Result.put (a, h)
i := i + 1
end
end
to_json (o: like object): like value
local
c: DS_HASH_TABLE_CURSOR [ANY, HASHABLE]
js: JSON_STRING
jv: JSON_VALUE
failed: BOOLEAN
do
create Result.make
from
c := o.new_cursor
c.start
until
c.after
loop
create js.make_json (c.key.out)
jv := json.value (c.item)
if jv /= Void then
Result.put (jv, js)
else
failed := True
end
c.forth
end
if failed then
Result := Void
end
end
end -- class JSON_DS_HASH_TABLE_CONVERTER

View File

@@ -1,62 +1,62 @@
indexing
description: "A JSON converter for DS_LINKED_LIST [ANY]"
author: "Paul Cohen"
date: "$Date: $"
revision: "$Revision: $"
file: "$HeadURL: $"
class JSON_DS_LINKED_LIST_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make is
do
create object.make
end
feature -- Access
value: JSON_ARRAY
object: DS_LINKED_LIST [ANY]
feature -- Conversion
from_json (j: like value): like object is
local
i: INTEGER
do
create Result.make
from
i := 1
until
i > j.count
loop
Result.put_last (json.object (j [i], Void))
i := i + 1
end
end
to_json (o: like object): like value is
local
c: DS_LIST_CURSOR [ANY]
do
create Result.make_array
from
c := o.new_cursor
c.start
until
c.after
loop
Result.add (json.value (c.item))
c.forth
end
end
end -- class JSON_DS_LINKED_LIST_CONVERTER
note
description: "A JSON converter for DS_LINKED_LIST [ANY]"
author: "Paul Cohen"
date: "$Date: $"
revision: "$Revision: $"
file: "$HeadURL: $"
class JSON_DS_LINKED_LIST_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make
do
create object.make
end
feature -- Access
value: JSON_ARRAY
object: DS_LINKED_LIST [ANY]
feature -- Conversion
from_json (j: like value): detachable like object
local
i: INTEGER
do
create Result.make
from
i := 1
until
i > j.count
loop
Result.put_last (json.object (j [i], Void))
i := i + 1
end
end
to_json (o: like object): like value
local
c: DS_LIST_CURSOR [ANY]
do
create Result.make_array
from
c := o.new_cursor
c.start
until
c.after
loop
Result.add (json.value (c.item))
c.forth
end
end
end -- class JSON_DS_LINKED_LIST_CONVERTER

View File

@@ -1,4 +1,4 @@
indexing
note
description: "[
Shared factory class for creating JSON objects. Maps JSON
objects to Gobo DS_HASH_TABLEs and JSON arrays to Gobo
@@ -15,7 +15,7 @@ class SHARED_GOBO_EJSON
feature
json: EJSON is
json: EJSON
-- A shared EJSON instance with default converters for
-- DS_LINKED_LIST [ANY] and DS_HASH_TABLE [ANY, HASHABLE]
local

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-5-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-5-0 http://www.eiffel.com/developers/xml/configuration-1-5-0.xsd" name="json" uuid="4E21C3BD-7951-4C6E-A673-431E762D7414" library_target="json">
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-8-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-8-0 http://www.eiffel.com/developers/xml/configuration-1-8-0.xsd" name="json" uuid="4E21C3BD-7951-4C6E-A673-431E762D7414" library_target="json">
<target name="json">
<root all_classes="true"/>
<option trace="false" profile="false" debug="false" warning="true" full_class_checking="true" is_attached_by_default="true" void_safety="all" syntax="obsolete" namespace="EJSON.Library">
<option trace="false" profile="false" debug="false" warning="true" full_class_checking="true" is_attached_by_default="true" void_safety="all" syntax="standard" namespace="EJSON.Library">
<assertions/>
<warning name="export_class_missing" enabled="false"/>
<warning name="old_verbatim_strings" enabled="false"/>
@@ -13,16 +13,15 @@
<cluster name="json" location=".\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
<exclude>/.svn$</exclude>
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
</file_rule>
<file_rule>
<exclude>^/kernel$</exclude>
<exclude>^/gobo$</exclude>
<exclude>^/kernel$</exclude>
<exclude>^/extras$</exclude>
</file_rule>
<cluster name="kernel" location=".\kernel\" recursive="true"/>
<cluster name="gobo" location=".\gobo\" recursive="true"/>
<cluster name="extras" location=".\extras\" recursive="true"/>
</cluster>
</target>

View File

@@ -1,30 +1,42 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-3-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-3-0 http://www.eiffel.com/developers/xml/configuration-1-3-0.xsd" name="json" uuid="4E21C3BD-7951-4C6E-A673-431E762D7414" library_target="json">
<target name="json">
<root all_classes="true"/>
<option trace="false" profile="false" debug="false" warning="true" full_class_checking="true" namespace="EJSON.Library">
<assertions/>
<warning name="export_class_missing" enabled="false"/>
<warning name="old_verbatim_strings" enabled="false"/>
<warning name="syntax" enabled="false"/>
<warning name="vjrv" enabled="false"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base.ecf" readonly="true"/>
<library name="gobo_ise" location="$ISE_LIBRARY\library\gobo\gobo.ecf" readonly="true"/>
<cluster name="json" location=".\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
<exclude>/.svn$</exclude>
<exclude>/CVS$</exclude>
</file_rule>
<file_rule>
<exclude>^/kernel$</exclude>
<exclude>^/gobo$</exclude>
<exclude>^/extras$</exclude>
</file_rule>
<cluster name="kernel" location=".\kernel\" recursive="true"/>
<cluster name="gobo" location=".\gobo\" recursive="true"/>
<cluster name="extras" location=".\extras\" recursive="true"/>
</cluster>
</target>
</system>
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-8-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-8-0 http://www.eiffel.com/developers/xml/configuration-1-8-0.xsd" name="json" uuid="4E21C3BD-7951-4C6E-A673-431E762D7414" library_target="json">
<target name="json">
<root all_classes="true"/>
<option trace="false" profile="false" debug="false" warning="true" full_class_checking="true" void_safety="none" syntax="standard" namespace="EJSON.Library">
<assertions/>
<warning name="export_class_missing" enabled="false"/>
<warning name="old_verbatim_strings" enabled="false"/>
<warning name="syntax" enabled="false"/>
<warning name="vjrv" enabled="false"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base.ecf" readonly="true"/>
<library name="gobo_argument" location="$ISE_LIBRARY\library\gobo\gobo_argument.ecf"/>
<library name="gobo_kernel" location="$ISE_LIBRARY\library\gobo\gobo_kernel.ecf"/>
<library name="gobo_lexical" location="$ISE_LIBRARY\library\gobo\gobo_lexical.ecf"/>
<library name="gobo_math" location="$ISE_LIBRARY\library\gobo\gobo_math.ecf"/>
<library name="gobo_parse" location="$ISE_LIBRARY\library\gobo\gobo_parse.ecf"/>
<library name="gobo_pattern" location="$ISE_LIBRARY\library\gobo\gobo_pattern.ecf"/>
<library name="gobo_regexp" location="$ISE_LIBRARY\library\gobo\gobo_regexp.ecf"/>
<library name="gobo_string" location="$ISE_LIBRARY\library\gobo\gobo_string.ecf"/>
<library name="gobo_structure" location="$ISE_LIBRARY\library\gobo\gobo_structure.ecf"/>
<library name="gobo_time" location="$ISE_LIBRARY\library\gobo\gobo_time.ecf"/>
<library name="gobo_tools" location="$ISE_LIBRARY\library\gobo\gobo_tools.ecf"/>
<library name="gobo_utility" location="$ISE_LIBRARY\library\gobo\gobo_utility.ecf"/>
<library name="gobo_xml" location="$ISE_LIBRARY\library\gobo\gobo_xml.ecf"/>
<cluster name="json" location=".\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
</file_rule>
<file_rule>
<exclude>^/gobo$</exclude>
<exclude>^/kernel$</exclude>
<exclude>^/extras$</exclude>
</file_rule>
<cluster name="kernel" location=".\kernel\" recursive="true"/>
<cluster name="gobo" location=".\gobo\" recursive="true"/>
<cluster name="extras" location=".\extras\" recursive="true"/>
</cluster>
</target>
</system>

View File

@@ -1,4 +1,4 @@
indexing
note
description: "A JSON converter"
author: "Paul Cohen"
date: "$Date: $"
@@ -7,35 +7,30 @@ indexing
deferred class JSON_CONVERTER
inherit
inherit
SHARED_EJSON
feature -- Access
value: JSON_VALUE is
-- JSON value
deferred
end
object: ANY is
object: ANY
-- Eiffel object
deferred
end
feature -- Conversion
from_json (j: like value): like object is
-- Convert from JSON value. Returns Void if unable to convert
from_json (j: attached like to_json): detachable like object
-- Convert from JSON value.
-- Returns Void if unable to convert
deferred
end
to_json (o: like object): like value is
to_json (o: like object): detachable JSON_VALUE
-- Convert to JSON value
deferred
end
invariant
has_eiffel_object: object /= Void -- An empty object must be created at creation time!
end -- class JSON_CONVERTER
end

View File

@@ -1,4 +1,4 @@
indexing
note
description: "A JSON converter for HASH_TABLE [ANY, HASHABLE]"
author: "Paul Cohen"
date: "$Date$"
@@ -9,31 +9,30 @@ class JSON_HASH_TABLE_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make is
make
do
create object.make (0)
end
feature -- Access
value: JSON_OBJECT
object: HASH_TABLE [ANY, HASHABLE]
feature -- Conversion
from_json (j: like value): like object is
from_json (j: attached like to_json): like object
local
keys: ARRAY [JSON_STRING]
i: INTEGER
h: HASHABLE
a: ANY
h: detachable HASHABLE
jv: detachable JSON_VALUE
a: detachable ANY
do
keys := j.current_keys
create Result.make (keys.count)
@@ -44,36 +43,46 @@ feature -- Conversion
loop
h ?= json.object (keys [i], void)
check h /= Void end
a := json.object (j.item (keys [i]), Void)
Result.put (a, h)
jv := j.item (keys [i])
if jv /= Void then
a := json.object (jv, Void)
if a /= Void then
Result.put (a, h)
else
check a_attached: a /= Void end
end
else
check j_has_item: False end
end
i := i + 1
end
end
to_json (o: like object): like value is
to_json (o: like object): detachable JSON_OBJECT
local
c: HASH_TABLE_ITERATION_CURSOR [ANY, HASHABLE]
js: JSON_STRING
jv: JSON_VALUE
jv: detachable JSON_VALUE
failed: BOOLEAN
do
create Result.make
from
o.start
c := o.new_cursor
until
o.after
c.after
loop
create js.make_json (o.key_for_iteration.out)
jv := json.value (o.item_for_iteration)
create js.make_json (c.key.out)
jv := json.value (c.item)
if jv /= Void then
Result.put (jv, js)
else
failed := True
end
o.forth
end
c.forth
end
if failed then
Result := Void
end
end
end -- class JSON_HASH_TABLE_CONVERTER
end -- class JSON_HASH_TABLE_CONVERTER

View File

@@ -1,4 +1,4 @@
indexing
note
description: "A JSON converter for LINKED_LIST [ANY]"
author: "Paul Cohen"
date: "$Date$"
@@ -9,26 +9,24 @@ class JSON_LINKED_LIST_CONVERTER
inherit
JSON_CONVERTER
create
make
feature {NONE} -- Initialization
make is
make
do
create object.make
end
feature -- Access
value: JSON_ARRAY
object: LINKED_LIST [ANY]
object: LINKED_LIST [detachable ANY]
feature -- Conversion
from_json (j: like value): like object is
from_json (j: like to_json): detachable like object
local
i: INTEGER
do
@@ -42,20 +40,24 @@ feature -- Conversion
i := i + 1
end
end
to_json (o: like object): like value is
to_json (o: like object): JSON_ARRAY
local
c: LINKED_LIST_CURSOR [ANY]
c: ITERATION_CURSOR [detachable ANY]
do
create Result.make_array
from
o.start
c := o.new_cursor
until
o.after
c.after
loop
Result.add (json.value (o.item))
o.forth
if attached json.value (c.item) as v then
Result.add (v)
else
check attached_value: False end
end
c.forth
end
end
end -- class JSON_LINKED_LIST_CONVERTER
end -- class JSON_LINKED_LIST_CONVERTER

View File

@@ -1,4 +1,4 @@
indexing
note
description: "Core factory class for creating JSON objects and corresponding Eiffel objects."
author: "Paul Cohen"
date: "$Date: $"
@@ -8,98 +8,73 @@ indexing
class EJSON
inherit
{NONE} KL_EXCEPTIONS
{NONE} EXCEPTIONS
feature -- Access
value (an_object: ?ANY): JSON_VALUE is
-- JSON value from Eiffel object. Raises an "eJSON exception" if
value (an_object: detachable ANY): detachable JSON_VALUE
-- JSON value from Eiffel object. Raises an "eJSON exception" if
-- unable to convert value.
local
b: BOOLEAN
i: INTEGER
i8: INTEGER_8
i16: INTEGER_16
i32: INTEGER_32
i64: INTEGER_64
n8: NATURAL_8
n16: NATURAL_16
n32: NATURAL_32
n64: NATURAL_64
r32: REAL_32
r64: REAL_64
a: ARRAY [ANY]
c: CHARACTER
s8: STRING_8
ucs: UC_STRING
ja: JSON_ARRAY
jc: JSON_CONVERTER
do
-- Try to convert from basic Eiffel types. Note that we check with
-- `conforms_to' since the client may have subclassed the base class
-- that these basic types are derived from.
if an_object = Void then
create {JSON_NULL} Result
elseif an_object.conforms_to (a_boolean) then
b ?= an_object
elseif attached {BOOLEAN} an_object as b then
create {JSON_BOOLEAN} Result.make_boolean (b)
elseif an_object.conforms_to (an_integer_8) then
i8 ?= an_object
elseif attached {INTEGER_8} an_object as i8 then
create {JSON_NUMBER} Result.make_integer (i8)
elseif an_object.conforms_to (an_integer_16) then
i16 ?= an_object
elseif attached {INTEGER_16} an_object as i16 then
create {JSON_NUMBER} Result.make_integer (i16)
elseif an_object.conforms_to (an_integer_32) then
i32 ?= an_object
elseif attached {INTEGER_32} an_object as i32 then
create {JSON_NUMBER} Result.make_integer (i32)
elseif an_object.conforms_to (an_integer_64) then
i64 ?= an_object
elseif attached {INTEGER_64} an_object as i64 then
create {JSON_NUMBER} Result.make_integer (i64)
elseif an_object.conforms_to (a_natural_8) then
n8 ?= an_object
elseif attached {NATURAL_8} an_object as n8 then
create {JSON_NUMBER} Result.make_natural (n8)
elseif an_object.conforms_to (a_natural_16) then
n16 ?= an_object
elseif attached {NATURAL_16} an_object as n16 then
create {JSON_NUMBER} Result.make_natural (n16)
elseif an_object.conforms_to (a_natural_32) then
n32 ?= an_object
elseif attached {NATURAL_32} an_object as n32 then
create {JSON_NUMBER} Result.make_natural (n32)
elseif an_object.conforms_to (a_natural_64) then
n64 ?= an_object
elseif attached {NATURAL_64} an_object as n64 then
create {JSON_NUMBER} Result.make_natural (n64)
elseif an_object.conforms_to (a_real_32) then
r32 ?= an_object
elseif attached {REAL_32} an_object as r32 then
create {JSON_NUMBER} Result.make_real (r32)
elseif an_object.conforms_to (a_real_64) then
r64 ?= an_object
elseif attached {REAL_64} an_object as r64 then
create {JSON_NUMBER} Result.make_real (r64)
elseif an_object.conforms_to (an_array) then
a ?= an_object
elseif attached {ARRAY [detachable ANY]} an_object as a then
create ja.make_array
from
i := a.lower
until
i > a.upper
loop
ja.add (value (a @ i))
if attached value (a @ i) as v then
ja.add (v)
else
check value_attached: False end
end
i := i + 1
end
Result := ja
elseif an_object.conforms_to (a_character) then
c ?= an_object
create {JSON_STRING} Result.make_json (c.out)
elseif an_object.conforms_to (a_uc_string) then
ucs ?= an_object
create {JSON_STRING} Result.make_json (ucs.to_utf8)
elseif an_object.conforms_to (a_string_8) then
s8 ?= an_object
elseif attached {CHARACTER_8} an_object as c8 then
create {JSON_STRING} Result.make_json (c8.out)
elseif attached {CHARACTER_32} an_object as c32 then
create {JSON_STRING} Result.make_json (c32.out)
elseif attached {STRING_8} an_object as s8 then
create {JSON_STRING} Result.make_json (s8)
elseif attached {STRING_32} an_object as s32 then
create {JSON_STRING} Result.make_json (s32.as_string_8) -- FIXME: need correct convertion/encoding here ...
end
if Result = Void then
-- Now check the converters
jc := converter_for (an_object)
if jc /= Void then
if an_object /= Void and then attached converter_for (an_object) as jc then
Result := jc.to_json (an_object)
else
raise (exception_failed_to_convert_to_json (an_object))
@@ -107,118 +82,108 @@ feature -- Access
end
end
object (a_value: JSON_VALUE; base_class: ?STRING): ANY is
object (a_value: detachable JSON_VALUE; base_class: detachable STRING): detachable ANY
-- Eiffel object from JSON value. If `base_class' /= Void an eiffel
-- object based on `base_class' will be returned. Raises an "eJSON
-- exception" if unable to convert value.
require
a_value_not_void: a_value /= Void
local
jc: JSON_CONVERTER
jb: JSON_BOOLEAN
jn: JSON_NUMBER
js: JSON_STRING
ja: JSON_ARRAY
jo: JSON_OBJECT
i: INTEGER
ll: LINKED_LIST [ANY]
t: HASH_TABLE [ANY, UC_STRING]
ll: LINKED_LIST [detachable ANY]
t: HASH_TABLE [detachable ANY, STRING_GENERAL]
keys: ARRAY [JSON_STRING]
ucs: UC_STRING
s32: STRING_32
s: detachable STRING_GENERAL
do
if base_class = Void then
if a_value.generator.is_equal ("JSON_NULL") then
Result := Void
elseif a_value.generator.is_equal ("JSON_BOOLEAN") then
jb ?= a_value
check jb /= Void end
Result := jb.item
elseif a_value.generator.is_equal ("JSON_NUMBER") then
jn ?= a_value
check jn /= Void end
if jn.item.is_integer_8 then
Result := jn.item.to_integer_8
elseif jn.item.is_integer_16 then
Result := jn.item.to_integer_16
elseif jn.item.is_integer_32 then
Result := jn.item.to_integer_32
elseif jn.item.is_integer_64 then
Result := jn.item.to_integer_64
elseif jn.item.is_natural_64 then
Result := jn.item.to_natural_64
elseif jn.item.is_double then
Result := jn.item.to_double
end
elseif a_value.generator.is_equal ("JSON_STRING") then
js ?= a_value
check js /= Void end
create ucs.make_from_string (js.item)
Result := ucs
elseif a_value.generator.is_equal ("JSON_ARRAY") then
ja ?= a_value
check ja /= Void end
from
create ll.make ()
i := 1
until
i > ja.count
loop
ll.extend (object (ja [i], Void))
i := i + 1
end
Result := ll
elseif a_value.generator.is_equal ("JSON_OBJECT") then
jo ?= a_value
check jo /= Void end
keys := jo.current_keys
create t.make (keys.count)
from
i := keys.lower
until
i > keys.upper
loop
ucs ?= object (keys [i], Void)
check ucs /= Void end
t.put (object (jo.item (keys [i]), Void), ucs)
i := i + 1
end
Result := t
end
else
if converters.has (base_class) then
jc := converters @ base_class
Result := jc.from_json (a_value)
else
raise (exception_failed_to_convert_to_eiffel (a_value, base_class))
end
end
if a_value = Void then
Result := Void
else
if base_class = Void then
if a_value = Void then
Result := Void
elseif attached {JSON_NULL} a_value then
Result := Void
elseif attached {JSON_BOOLEAN} a_value as jb then
Result := jb.item
elseif attached {JSON_NUMBER} a_value as jn then
if jn.item.is_integer_8 then
Result := jn.item.to_integer_8
elseif jn.item.is_integer_16 then
Result := jn.item.to_integer_16
elseif jn.item.is_integer_32 then
Result := jn.item.to_integer_32
elseif jn.item.is_integer_64 then
Result := jn.item.to_integer_64
elseif jn.item.is_natural_64 then
Result := jn.item.to_natural_64
elseif jn.item.is_double then
Result := jn.item.to_double
end
elseif attached {JSON_STRING} a_value as js then
create s32.make_from_string (js.item)
Result := s32
elseif attached {JSON_ARRAY} a_value as ja then
from
create ll.make
i := 1
until
i > ja.count
loop
ll.extend (object (ja [i], Void))
i := i + 1
end
Result := ll
elseif attached {JSON_OBJECT} a_value as jo then
keys := jo.current_keys
create t.make (keys.count)
from
i := keys.lower
until
i > keys.upper
loop
s ?= object (keys [i], Void)
check s /= Void end
t.put (object (jo.item (keys [i]), Void), s)
i := i + 1
end
Result := t
end
else
if converters.has_key (base_class) and then attached converters.found_item as jc then
Result := jc.from_json (a_value)
else
raise (exception_failed_to_convert_to_eiffel (a_value, base_class))
end
end
end
end
object_from_json (json: STRING; base_class: ?STRING): ANY is
-- Eiffel object from JSON representation. If `base_class' /= Void an
-- Eiffel object based on `base_class' will be returned. Raises an
object_from_json (json: STRING; base_class: detachable STRING): detachable ANY
-- Eiffel object from JSON representation. If `base_class' /= Void an
-- Eiffel object based on `base_class' will be returned. Raises an
-- "eJSON exception" if unable to convert value.
require
json_not_void: json /= Void
local
jv: JSON_VALUE
jv: detachable JSON_VALUE
do
json_parser.set_representation (json)
jv := json_parser.parse
Result := object (jv, base_class)
if jv /= Void then
Result := object (jv, base_class)
end
end
converter_for (an_object: ANY): JSON_CONVERTER is
converter_for (an_object: ANY): detachable JSON_CONVERTER
-- Converter for objects. Returns Void if none found.
require
an_object_not_void: an_object /= Void
do
if converters.has (an_object.generator) then
Result := converters @ an_object.generator
if converters.has_key (an_object.generator) then
Result := converters.found_item
end
end
json_reference (s: STRING): JSON_OBJECT is
json_reference (s: STRING): JSON_OBJECT
-- A JSON (Dojo style) reference object using `s' as the
-- reference value. The caller is responsable for ensuring
-- the validity of `s' as a json reference.
@@ -232,21 +197,20 @@ feature -- Access
create js_value.make_json (s)
Result.put (js_value, js_key)
end
json_references (l: DS_LIST [STRING]): JSON_ARRAY is
-- A JSON array of JSON (Dojo style) reference objects using the
-- strings in `l' as reference values. The caller is responsable
-- for ensuring the validity of all strings in `l' as json
json_references (l: LIST [STRING]): JSON_ARRAY
-- A JSON array of JSON (Dojo style) reference objects using the
-- strings in `l' as reference values. The caller is responsable
-- for ensuring the validity of all strings in `l' as json
-- references.
require
l_not_void: l /= Void
local
c: DS_LIST_CURSOR [STRING]
c: ITERATION_CURSOR [STRING]
do
create Result.make_array
from
c := l.new_cursor
c.start
until
c.after
loop
@@ -254,10 +218,10 @@ feature -- Access
c.forth
end
end
feature -- Change
add_converter (jc: JSON_CONVERTER) is
add_converter (jc: JSON_CONVERTER)
-- Add the converter `jc'.
require
jc_not_void: jc /= Void
@@ -269,7 +233,7 @@ feature -- Change
feature {NONE} -- Implementation
converters: DS_HASH_TABLE [JSON_CONVERTER, STRING] is
converters: HASH_TABLE [JSON_CONVERTER, STRING]
-- Converters hashed by generator (base class)
once
create Result.make (10)
@@ -277,9 +241,9 @@ feature {NONE} -- Implementation
feature {NONE} -- Implementation (Exceptions)
exception_prefix: STRING is "eJSON exception: "
exception_failed_to_convert_to_eiffel (a_value: JSON_VALUE; base_class: ?STRING): STRING is
exception_prefix: STRING = "eJSON exception: "
exception_failed_to_convert_to_eiffel (a_value: JSON_VALUE; base_class: detachable STRING): STRING
-- Exception message for failing to convert a JSON_VALUE to an instance of `a'.
do
Result := exception_prefix + "Failed to convert JSON_VALUE to an Eiffel object: " + a_value.generator
@@ -288,15 +252,18 @@ feature {NONE} -- Implementation (Exceptions)
end
end
exception_failed_to_convert_to_json (an_object: ?ANY): STRING is
exception_failed_to_convert_to_json (an_object: detachable ANY): STRING
-- Exception message for failing to convert `a' to a JSON_VALUE.
do
Result := exception_prefix + "Failed to convert Eiffel object to a JSON_VALUE: " + an_object.generator
Result := exception_prefix + "Failed to convert Eiffel object to a JSON_VALUE"
if an_object /= Void then
Result := ": " + an_object.generator
end
end
feature {NONE} -- Implementation (JSON parser)
json_parser: JSON_PARSER is
json_parser: JSON_PARSER
once
create Result.make_parser ("")
end
@@ -304,7 +271,7 @@ feature {NONE} -- Implementation (JSON parser)
feature {NONE} -- Implementation (Basic Eiffel objects)
a_boolean: BOOLEAN
an_integer_8: INTEGER_8
an_integer_16: INTEGER_16
@@ -320,26 +287,26 @@ feature {NONE} -- Implementation (Basic Eiffel objects)
a_natural_32: NATURAL_32
a_natural_64: NATURAL_64
a_real_32: REAL_32
a_real_64: REAL_64
an_array: ARRAY [ANY] is
an_array: ARRAY [ANY]
once
Result := <<>>
end
a_character: CHARACTER
a_string_8: STRING_8 is
a_string_8: STRING_8
once
Result := ""
end
a_uc_string: UC_STRING is
a_string_32: STRING_32
once
create Result.make_from_string ("")
Result := {STRING_32} ""
end
end -- class EJSON
end -- class EJSON

View File

@@ -1,4 +1,4 @@
indexing
note
description: "[
JSON_ARRAY represent an array in JSON.
An array in JSON is an ordered set of names.
@@ -25,7 +25,7 @@ create
feature {NONE} -- Initialization
make_array is
make_array
-- Initialize JSON Array
do
create values.make (10)
@@ -33,7 +33,7 @@ feature {NONE} -- Initialization
feature -- Access
i_th alias "[]" (i: INTEGER): JSON_VALUE is
i_th alias "[]" (i: INTEGER): JSON_VALUE
-- Item at `i'-th position
require
is_valid_index: valid_index (i)
@@ -41,7 +41,7 @@ feature -- Access
Result := values.i_th (i)
end
representation: STRING is
representation: STRING
local
i: INTEGER
do
@@ -59,10 +59,10 @@ feature -- Access
end
Result.append_character (']')
end
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_json_array' procedure on `a_visitor'.)
do
@@ -71,7 +71,7 @@ feature -- Visitor pattern
feature -- Mesurement
count: INTEGER is
count: INTEGER
-- Number of items.
do
Result := values.count
@@ -79,7 +79,7 @@ feature -- Mesurement
feature -- Status report
valid_index (i: INTEGER): BOOLEAN is
valid_index (i: INTEGER): BOOLEAN
-- Is `i' a valid index?
do
Result := (1 <= i) and (i <= count)
@@ -87,11 +87,11 @@ feature -- Status report
feature -- Change Element
add (value: JSON_VALUE) is
add (value: JSON_VALUE)
require
value_not_null: value /= void
do
values.extend(value)
values.extend (value)
ensure
has_new_value: old values.count + 1 = values.count and
values.has (value)
@@ -99,7 +99,7 @@ feature -- Change Element
feature -- Report
hash_code: INTEGER is
hash_code: INTEGER
-- Hash code value
do
from
@@ -116,7 +116,7 @@ feature -- Report
feature -- Conversion
array_representation: ARRAYED_LIST [JSON_VALUE] is
array_representation: ARRAYED_LIST [JSON_VALUE]
-- Representation as a sequences of values
do
Result := values

View File

@@ -1,4 +1,4 @@
indexing
note
description: "JSON Truth values"
author: "Javier Velilla"
date: "2008/08/24"
@@ -15,7 +15,7 @@ create
feature {NONE} -- Initialization
make_boolean (an_item: BOOLEAN) is
make_boolean (an_item: BOOLEAN)
--Initialize.
do
item := an_item
@@ -26,13 +26,13 @@ feature -- Access
item: BOOLEAN
-- Content
hash_code: INTEGER is
hash_code: INTEGER
-- Hash code value
do
Result := item.hash_code
end
representation: STRING is
representation: STRING
do
if item then
Result := "true"
@@ -43,7 +43,7 @@ feature -- Access
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_json_boolean' procedure on `a_visitor'.)
do

View File

@@ -1,4 +1,4 @@
indexing
note
description: "JSON Null Values"
author: "Javier Velilla"
date: "2008/08/24"
@@ -12,20 +12,20 @@ inherit
feature --Access
hash_code: INTEGER is
hash_code: INTEGER
-- Hash code value
do
Result := null_value.hash_code
end
representation: STRING is
representation: STRING
do
Result := "null"
end
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_element_a' procedure on `a_visitor'.)
do
@@ -42,6 +42,6 @@ feature -- Status report
feature {NONE}-- Implementation
null_value: STRING is "null"
null_value: STRING = "null"
end

View File

@@ -1,4 +1,4 @@
indexing
note
description: "JSON Numbers, octal and hexadecimal formats are not used."
author: "Javier Velilla"
@@ -22,21 +22,21 @@ create
feature {NONE} -- initialization
make_integer (an_argument: INTEGER_64) is
make_integer (an_argument: INTEGER_64)
-- Initialize an instance of JSON_NUMBER from the integer value of `an_argument'.
do
item := an_argument.out
numeric_type := INTEGER_TYPE
end
make_natural (an_argument: NATURAL_64) is
make_natural (an_argument: NATURAL_64)
-- 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
make_real (an_argument: DOUBLE)
-- Initialize an instance of JSON_NUMBER from the floating point value of `an_argument'.
do
item := an_argument.out
@@ -48,20 +48,20 @@ feature -- Access
item: STRING
-- Content
hash_code: INTEGER is
hash_code: INTEGER
--Hash code value
do
Result := item.hash_code
end
representation: STRING is
representation: STRING
do
Result := item
end
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_json_number' procedure on `a_visitor'.)
do
@@ -70,7 +70,7 @@ feature -- Visitor pattern
feature -- Status
is_equal (other: like Current): BOOLEAN is
is_equal (other: like Current): BOOLEAN
-- Is `other' attached to an object of the same type
-- as current object and identical to it?
do
@@ -87,9 +87,9 @@ feature -- Status report
feature -- Implementation
INTEGER_TYPE: INTEGER is 1
DOUBLE_TYPE: INTEGER is 2
NATURAL_TYPE: INTEGER is 3
INTEGER_TYPE: INTEGER = 1
DOUBLE_TYPE: INTEGER = 2
NATURAL_TYPE: INTEGER = 3
numeric_type: INTEGER

View File

@@ -1,4 +1,4 @@
indexing
note
description: "[
An JSON_OBJECT represent an object in JSON.
@@ -27,7 +27,7 @@ create
feature {NONE} -- Initialization
make is
make
-- Initialize
do
create object.make (10)
@@ -35,13 +35,13 @@ feature {NONE} -- Initialization
feature -- Change Element
put (value: ?JSON_VALUE; key: JSON_STRING) is
put (value: detachable JSON_VALUE; key: JSON_STRING)
-- 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
l_value: detachable JSON_VALUE
do
l_value := value
if l_value = Void then
@@ -52,31 +52,31 @@ feature -- Change Element
feature -- Access
has_key (key: JSON_STRING): BOOLEAN is
has_key (key: JSON_STRING): BOOLEAN
-- has the JSON_OBJECT contains a specific key 'key'.
do
Result := object.has (key)
end
has_item (value: JSON_VALUE): BOOLEAN is
has_item (value: JSON_VALUE): BOOLEAN
-- has the JSON_OBJECT contain a specfic item 'value'
do
Result := object.has_item (value)
end
item (key: JSON_STRING): ?JSON_VALUE is
item (key: JSON_STRING): detachable JSON_VALUE
-- the json_value associated with a key.
do
Result := object.item (key)
end
current_keys: ARRAY [JSON_STRING] is
current_keys: ARRAY [JSON_STRING]
-- array containing actually used keys
do
Result := object.current_keys
end
representation: STRING is
representation: STRING
local
t: HASH_TABLE [JSON_VALUE, JSON_STRING]
do
@@ -100,7 +100,7 @@ feature -- Access
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_json_object' procedure on `a_visitor'.)
do
@@ -109,7 +109,7 @@ feature -- Visitor pattern
feature -- Conversion
map_representation: HASH_TABLE [JSON_VALUE, JSON_STRING] is
map_representation: HASH_TABLE [JSON_VALUE, JSON_STRING]
--A representation that maps keys to values
do
Result := object
@@ -117,7 +117,7 @@ feature -- Conversion
feature -- Report
hash_code: INTEGER is
hash_code: INTEGER
-- Hash code value
do
from

View File

@@ -1,6 +1,6 @@
indexing
note
description:"[
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.
@@ -26,7 +26,7 @@ create
feature {NONE} -- Initialization
make_json (an_item: STRING) is
make_json (an_item: STRING)
-- Initialize.
require
item_not_void: an_item /= Void
@@ -39,7 +39,7 @@ feature -- Access
item: STRING
-- Contents
representation: STRING is
representation: STRING
do
Result := "%""
Result.append (item)
@@ -48,7 +48,7 @@ feature -- Access
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_json_string' procedure on `a_visitor'.)
do
@@ -57,7 +57,7 @@ feature -- Visitor pattern
feature -- Comparison
is_equal (other: like Current): BOOLEAN is
is_equal (other: like Current): BOOLEAN
-- Is JSON_STRING made of same character sequence as `other'
-- (possibly with a different capacity)?
do
@@ -66,7 +66,7 @@ feature -- Comparison
feature -- Change Element
append (a_string: STRING)is
append (a_string: STRING)
-- Add an_item
require
a_string_not_void: a_string /= Void
@@ -76,7 +76,7 @@ feature -- Change Element
feature -- Status report
hash_code: INTEGER is
hash_code: INTEGER
-- Hash code value
do
Result := item.hash_code
@@ -92,7 +92,7 @@ feature -- Status report
feature {NONE} -- Implementation
escaped_json_string (s: STRING): STRING is
escaped_json_string (s: STRING): STRING
-- JSON string with '"' and '\' characters escaped
require
s_not_void: s /= Void

View File

@@ -1,4 +1,4 @@
indexing
note
description:"[
JSON_VALUE represent a value in JSON.
A value can be
@@ -25,14 +25,14 @@ inherit
feature -- Access
representation: STRING is
representation: STRING
-- UTF-8 encoded Unicode string representation of Current
deferred
end
feature -- Visitor pattern
accept (a_visitor: JSON_VISITOR) is
accept (a_visitor: JSON_VISITOR)
-- Accept `a_visitor'.
-- (Call `visit_*' procedure on `a_visitor'.)
require

View File

@@ -1,4 +1,4 @@
indexing
note
description: "Parse serialized JSON data"
author: "jvelilla"
@@ -17,7 +17,7 @@ create
feature {NONE} -- Initialize
make_parser (a_json: STRING) is
make_parser (a_json: STRING)
-- Initialize.
require
json_not_empty: a_json /= Void and then not a_json.is_empty
@@ -51,7 +51,7 @@ feature -- Status report
feature -- Element change
report_error (e: STRING) is
report_error (e: STRING)
-- Report error `e'
require
e_not_void: e /= Void
@@ -61,7 +61,7 @@ feature -- Element change
feature -- Commands
parse_json: ?JSON_VALUE is
parse_json: detachable JSON_VALUE
-- Parse JSON data `representation'
-- start ::= object | array
do
@@ -76,7 +76,7 @@ feature -- Commands
end
end
parse: ?JSON_VALUE is
parse: detachable JSON_VALUE
-- Parse JSON data `representation'
local
c: CHARACTER
@@ -121,14 +121,14 @@ feature -- Commands
is_parsed_implies_result_not_void: is_parsed implies Result /= Void
end
parse_object: JSON_OBJECT is
parse_object: JSON_OBJECT
-- object
-- {}
-- {"key" : "value" [,]}
local
has_more: BOOLEAN
l_json_string: ?JSON_STRING
l_value: ?JSON_VALUE
l_json_string: detachable JSON_STRING
l_value: detachable JSON_VALUE
do
create Result.make
-- check if is an empty object {}
@@ -174,7 +174,7 @@ feature -- Commands
end
end
parse_string: ?JSON_STRING is
parse_string: detachable JSON_STRING
-- Parsed string
local
has_more: BOOLEAN
@@ -200,7 +200,7 @@ feature -- Commands
create l_unicode.make_from_string ("\u")
l_unicode.append (read_unicode)
c := actual
if is_a_valid_unicode (l_unicode) then
if is_valid_unicode (l_unicode) then
l_json_string.append (l_unicode)
else
has_more := False
@@ -231,13 +231,13 @@ feature -- Commands
end
end
parse_array: JSON_ARRAY is
parse_array: JSON_ARRAY
-- array
-- []
-- [elements [,]]
local
flag: BOOLEAN
l_value: ?JSON_VALUE
l_value: detachable JSON_VALUE
c: like actual
do
create Result.make_array
@@ -276,7 +276,7 @@ feature -- Commands
end
end
parse_number: ?JSON_NUMBER is
parse_number: detachable JSON_NUMBER
-- Parsed number
local
sb: STRING
@@ -304,7 +304,7 @@ feature -- Commands
end
end
if is_a_valid_number (sb) then
if is_valid_number (sb) then
if sb.is_integer then
create Result.make_integer (sb.to_integer)
is_integer := True
@@ -317,7 +317,7 @@ feature -- Commands
end
end
is_null: BOOLEAN is
is_null: BOOLEAN
-- Word at index represents null?
local
l_null: STRING
@@ -330,7 +330,7 @@ feature -- Commands
end
end
is_false: BOOLEAN is
is_false: BOOLEAN
-- Word at index represents false?
local
l_false: STRING
@@ -343,7 +343,7 @@ feature -- Commands
end
end
is_true: BOOLEAN is
is_true: BOOLEAN
-- Word at index represents true?
local
l_true: STRING
@@ -356,7 +356,7 @@ feature -- Commands
end
end
read_unicode: STRING is
read_unicode: STRING
-- Read unicode and return value
local
i: INTEGER
@@ -375,27 +375,83 @@ feature -- Commands
feature {NONE} -- Implementation
is_a_valid_number (a_number: STRING): BOOLEAN is
is_valid_number (a_number: STRING): BOOLEAN
-- 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
a_number.right_adjust
a_number.left_adjust
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
Result := a_number.is_equal (regexp.captured_substring (0))
end
end
is_a_valid_unicode (a_unicode: STRING): BOOLEAN is
local
s: detachable STRING
c: CHARACTER
i,n: INTEGER
do
create s.make_empty
n := a_number.count
if n = 0 then
Result := False
else
Result := True
i := 1
--| "-?"
c := a_number[i]
if c = '-' then
s.extend (c); i := i + 1; c := a_number[i]
end
--| "0|[1-9]\d*
if c.is_digit then
if c = '0' then
--| "0"
s.extend (c); i := i + 1; c := a_number[i]
else
--| "[1-9]"
s.extend (c); i := i + 1; c := a_number[i]
--| "\d*"
from until i > n or not c.is_digit loop
s.extend (c); i := i + 1; c := a_number[i]
end
end
end
end
if Result then
--| "(\.\d+)?"
if c = '.' then
--| "\.\d+" = "\.\d\d*"
s.extend (c); i := i + 1; c := a_number[i]
if c.is_digit then
from until i > n or not c.is_digit loop
s.extend (c); i := i + 1; c := a_number[i]
end
else
Result := False --| expecting digit
end
end
end
if Result then --| "(?:[eE][+-]?\d+)?\b"
if c = 'e' or c = 'E' then
--| "[eE][+-]?\d+"
s.extend (c); i := i + 1; c := a_number[i]
if c = '+' or c = '-' then
s.extend (c); i := i + 1; c := a_number[i]
end
if c.is_digit then
from until i > n or not c.is_digit loop
s.extend (c); i := i + 1; c := a_number[i]
end
else
Result := False --| expecting digit
end
end
end
if Result then --| "\b"
from until i > n or not c.is_space loop
s.extend (c); i := i + 1; c := a_number[i]
end
Result := i > n
if Result then
Result := s.same_string (a_number)
end
end
end
is_valid_unicode (a_unicode: STRING): BOOLEAN
-- is 'a_unicode' a valid unicode based on this regular expression
-- "\\u[0-9a-fA-F]{4}"
local
@@ -423,7 +479,7 @@ feature {NONE} -- Implementation
end
end
extra_elements: BOOLEAN is
extra_elements: BOOLEAN
-- has more elements?
local
c: like actual
@@ -449,11 +505,11 @@ feature {NONE} -- Implementation
feature {NONE} -- Constants
false_id: STRING is "false"
false_id: STRING = "false"
true_id: STRING is "true"
true_id: STRING = "true"
null_id: STRING is "null"
null_id: STRING = "null"
end

View File

@@ -1,4 +1,4 @@
indexing
note
description: "Objects that ..."
author: "jvelilla"
date: "2008/08/24"
@@ -12,7 +12,7 @@ create
feature {NONE} -- Initialization
make (a_json: STRING) is
make (a_json: STRING)
-- Initialize Reader
do
set_representation (a_json)
@@ -20,7 +20,7 @@ feature {NONE} -- Initialization
feature -- Commands
set_representation (a_json: STRING) is
set_representation (a_json: STRING)
-- Set `representation'.
do
a_json.left_adjust
@@ -29,7 +29,7 @@ feature -- Commands
index := 1
end
read: CHARACTER is
read: CHARACTER
-- Read character
do
if not representation.is_empty then
@@ -37,7 +37,7 @@ feature -- Commands
end
end
next is
next
-- Move to next index
require
has_more_elements: has_next
@@ -47,7 +47,7 @@ feature -- Commands
incremented: old index + 1 = index
end
previous is
previous
-- Move to previous index
require
not_is_first: has_previous
@@ -57,7 +57,7 @@ feature -- Commands
incremented: old index - 1 = index
end
skip_white_spaces is
skip_white_spaces
-- Remove white spaces
local
c: like actual
@@ -72,7 +72,7 @@ feature -- Commands
end
end
json_substring (start_index, end_index: INTEGER_32): STRING is
json_substring (start_index, end_index: INTEGER_32): STRING
-- JSON representation between `start_index' and `end_index'
do
Result := representation.substring (start_index, end_index)
@@ -80,13 +80,13 @@ feature -- Commands
feature -- Status report
has_next: BOOLEAN is
has_next: BOOLEAN
-- Has a next character?
do
Result := index <= representation.count
end
has_previous: BOOLEAN is
has_previous: BOOLEAN
-- Has a previous character?
do
Result := index >= 1
@@ -99,7 +99,7 @@ feature -- Access
feature {NONE} -- Implementation
actual: CHARACTER is
actual: CHARACTER
-- Current character or '%U' if none
do
if index > representation.count then

View File

@@ -1,4 +1,4 @@
indexing
note
description: ""
author: "jvelilla"
date: "2008/08/24"
@@ -9,19 +9,19 @@ class
feature -- Access
j_OBJECT_OPEN: CHARACTER is '{'
j_ARRAY_OPEN: CHARACTER is '['
j_OBJECT_CLOSE: CHARACTER is '}'
j_ARRAY_CLOSE: CHARACTER is ']'
j_OBJECT_OPEN: CHARACTER = '{'
j_ARRAY_OPEN: CHARACTER = '['
j_OBJECT_CLOSE: CHARACTER = '}'
j_ARRAY_CLOSE: CHARACTER = ']'
j_STRING: CHARACTER is '"'
j_PLUS: CHARACTER is '+'
j_MINUS: CHARACTER is '-'
j_DOT: CHARACTER is '.'
j_STRING: CHARACTER = '"'
j_PLUS: CHARACTER = '+'
j_MINUS: CHARACTER = '-'
j_DOT: CHARACTER = '.'
feature -- Status report
is_open_token (c: CHARACTER): BOOLEAN is
is_open_token (c: CHARACTER): BOOLEAN
-- Characters which open a type
do
inspect c
@@ -32,7 +32,7 @@ feature -- Status report
end
end
is_close_token (c: CHARACTER): BOOLEAN is
is_close_token (c: CHARACTER): BOOLEAN
-- Characters which close a type
do
inspect c
@@ -43,7 +43,7 @@ feature -- Status report
end
end
is_special_character (c: CHARACTER): BOOLEAN is
is_special_character (c: CHARACTER): BOOLEAN
-- Control Characters
-- %F Form feed
-- %H backslasH
@@ -62,7 +62,7 @@ feature -- Status report
end
end
is_special_control (c: CHARACTER): BOOLEAN is
is_special_control (c: CHARACTER): BOOLEAN
--Control Characters
-- \b\f\n\r\t
do

View File

@@ -1,4 +1,4 @@
indexing
note
description: "[
Shared factory class for creating JSON objects. Maps JSON
objects to ELKS HASH_TABLEs and JSON arrays to ELKS
@@ -15,7 +15,7 @@ class SHARED_EJSON
feature
json: EJSON is
json: EJSON
-- A shared EJSON instance with default converters for
-- DS_LINKED_LIST [ANY] and DS_HASH_TABLE [ANY, HASHABLE]
local