Merge branch 'master' into fix_hash_table_converter
This commit is contained in:
175
library/extras/visitor/json_pretty_string_visitor.e
Normal file
175
library/extras/visitor/json_pretty_string_visitor.e
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
note
|
||||||
|
description: "JSON_PRETTY_STRING_VISITOR Generates the JSON-String for a JSON_VALUE"
|
||||||
|
revision: "0.1"
|
||||||
|
|
||||||
|
class
|
||||||
|
JSON_PRETTY_STRING_VISITOR
|
||||||
|
|
||||||
|
inherit
|
||||||
|
JSON_VISITOR
|
||||||
|
|
||||||
|
create
|
||||||
|
make,
|
||||||
|
make_custom
|
||||||
|
|
||||||
|
feature -- Initialization
|
||||||
|
|
||||||
|
make (a_output: like output)
|
||||||
|
-- Create a new instance
|
||||||
|
do
|
||||||
|
make_custom (a_output, 1, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
make_custom (a_output: like output; a_object_count_inlining, a_array_count_inlining: INTEGER)
|
||||||
|
-- Create a new instance
|
||||||
|
do
|
||||||
|
output := a_output
|
||||||
|
create indentation.make_empty
|
||||||
|
indentation_step := "%T"
|
||||||
|
|
||||||
|
object_count_inlining := a_object_count_inlining
|
||||||
|
array_count_inlining := a_array_count_inlining
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Access
|
||||||
|
|
||||||
|
output: STRING_32
|
||||||
|
-- JSON representation
|
||||||
|
|
||||||
|
indentation: like output
|
||||||
|
|
||||||
|
indentation_step: like indentation
|
||||||
|
|
||||||
|
line_number: INTEGER
|
||||||
|
|
||||||
|
indent
|
||||||
|
do
|
||||||
|
indentation.append (indentation_step)
|
||||||
|
end
|
||||||
|
|
||||||
|
exdent
|
||||||
|
do
|
||||||
|
indentation.remove_tail (indentation_step.count)
|
||||||
|
end
|
||||||
|
|
||||||
|
new_line
|
||||||
|
do
|
||||||
|
output.append ("%N")
|
||||||
|
output.append (indentation)
|
||||||
|
line_number := line_number + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
object_count_inlining: INTEGER
|
||||||
|
array_count_inlining: INTEGER
|
||||||
|
|
||||||
|
feature -- Visitor Pattern
|
||||||
|
|
||||||
|
visit_json_array (a_json_array: JSON_ARRAY)
|
||||||
|
-- Visit `a_json_array'.
|
||||||
|
local
|
||||||
|
value: JSON_VALUE
|
||||||
|
l_json_array: ARRAYED_LIST [JSON_VALUE]
|
||||||
|
l_line: like line_number
|
||||||
|
l_multiple_lines: BOOLEAN
|
||||||
|
do
|
||||||
|
l_json_array := a_json_array.array_representation
|
||||||
|
l_multiple_lines := l_json_array.count >= array_count_inlining or across l_json_array as p some attached {JSON_OBJECT} p.item or attached {JSON_ARRAY} p.item end
|
||||||
|
output.append ("[")
|
||||||
|
l_line := line_number
|
||||||
|
indent
|
||||||
|
from
|
||||||
|
l_json_array.start
|
||||||
|
until
|
||||||
|
l_json_array.off
|
||||||
|
loop
|
||||||
|
if
|
||||||
|
line_number > l_line or
|
||||||
|
l_multiple_lines
|
||||||
|
then
|
||||||
|
new_line
|
||||||
|
end
|
||||||
|
value := l_json_array.item
|
||||||
|
value.accept (Current)
|
||||||
|
l_json_array.forth
|
||||||
|
if not l_json_array.after then
|
||||||
|
output.append (", ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
exdent
|
||||||
|
if
|
||||||
|
line_number > l_line or
|
||||||
|
l_json_array.count >= array_count_inlining
|
||||||
|
then
|
||||||
|
new_line
|
||||||
|
end
|
||||||
|
output.append ("]")
|
||||||
|
end
|
||||||
|
|
||||||
|
visit_json_boolean (a_json_boolean: JSON_BOOLEAN)
|
||||||
|
-- Visit `a_json_boolean'.
|
||||||
|
do
|
||||||
|
output.append (a_json_boolean.item.out)
|
||||||
|
end
|
||||||
|
|
||||||
|
visit_json_null (a_json_null: JSON_NULL)
|
||||||
|
-- Visit `a_json_null'.
|
||||||
|
do
|
||||||
|
output.append ("null")
|
||||||
|
end
|
||||||
|
|
||||||
|
visit_json_number (a_json_number: JSON_NUMBER)
|
||||||
|
-- Visit `a_json_number'.
|
||||||
|
do
|
||||||
|
output.append (a_json_number.item)
|
||||||
|
end
|
||||||
|
|
||||||
|
visit_json_object (a_json_object: JSON_OBJECT)
|
||||||
|
-- Visit `a_json_object'.
|
||||||
|
local
|
||||||
|
l_pairs: HASH_TABLE [JSON_VALUE, JSON_STRING]
|
||||||
|
l_line: like line_number
|
||||||
|
l_multiple_lines: BOOLEAN
|
||||||
|
do
|
||||||
|
l_pairs := a_json_object.map_representation
|
||||||
|
l_multiple_lines := l_pairs.count >= object_count_inlining or across l_pairs as p some attached {JSON_OBJECT} p.item or attached {JSON_ARRAY} p.item end
|
||||||
|
output.append ("{")
|
||||||
|
l_line := line_number
|
||||||
|
indent
|
||||||
|
from
|
||||||
|
l_pairs.start
|
||||||
|
until
|
||||||
|
l_pairs.off
|
||||||
|
loop
|
||||||
|
if
|
||||||
|
line_number > l_line or
|
||||||
|
l_multiple_lines
|
||||||
|
then
|
||||||
|
new_line
|
||||||
|
end
|
||||||
|
l_pairs.key_for_iteration.accept (Current)
|
||||||
|
output.append (": ")
|
||||||
|
l_pairs.item_for_iteration.accept (Current)
|
||||||
|
l_pairs.forth
|
||||||
|
if not l_pairs.after then
|
||||||
|
output.append (", ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
exdent
|
||||||
|
if
|
||||||
|
line_number > l_line or
|
||||||
|
l_pairs.count >= object_count_inlining
|
||||||
|
then
|
||||||
|
new_line
|
||||||
|
end
|
||||||
|
output.append ("}")
|
||||||
|
end
|
||||||
|
|
||||||
|
visit_json_string (a_json_string: JSON_STRING)
|
||||||
|
-- Visit `a_json_string'.
|
||||||
|
do
|
||||||
|
output.append ("%"")
|
||||||
|
output.append (a_json_string.item)
|
||||||
|
output.append ("%"")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
30
library/kernel/converters/json_arrayed_list_converter.e
Normal file
30
library/kernel/converters/json_arrayed_list_converter.e
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
note
|
||||||
|
description: "A JSON converter for ARRAYED_LIST [ANY]"
|
||||||
|
author: "Paul Cohen"
|
||||||
|
date: "$Date$"
|
||||||
|
revision: "$Revision$"
|
||||||
|
file: "$HeadURL: $"
|
||||||
|
|
||||||
|
class JSON_ARRAYED_LIST_CONVERTER
|
||||||
|
|
||||||
|
inherit
|
||||||
|
JSON_LIST_CONVERTER
|
||||||
|
redefine
|
||||||
|
object
|
||||||
|
end
|
||||||
|
|
||||||
|
create
|
||||||
|
make
|
||||||
|
|
||||||
|
feature -- Access
|
||||||
|
|
||||||
|
object: ARRAYED_LIST [detachable ANY]
|
||||||
|
|
||||||
|
feature {NONE} -- Factory
|
||||||
|
|
||||||
|
new_object (nb: INTEGER): like object
|
||||||
|
do
|
||||||
|
create Result.make (nb)
|
||||||
|
end
|
||||||
|
|
||||||
|
end -- class JSON_ARRAYED_LIST_CONVERTER
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
note
|
note
|
||||||
description: "A JSON converter"
|
description: "A JSON converter"
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: $"
|
date: "$Date$"
|
||||||
revision: "$Revision: $"
|
revision: "$Revision$"
|
||||||
file: "$HeadURL: $"
|
file: "$HeadURL: $"
|
||||||
|
|
||||||
deferred class JSON_CONVERTER
|
deferred class JSON_CONVERTER
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ feature -- Conversion
|
|||||||
until
|
until
|
||||||
i > keys.count
|
i > keys.count
|
||||||
loop
|
loop
|
||||||
h ?= json.object (keys [i], void)
|
h ?= json.object (keys [i], Void)
|
||||||
check h /= Void end
|
check h /= Void end
|
||||||
jv := j.item (keys [i])
|
jv := j.item (keys [i])
|
||||||
if jv /= Void then
|
if jv /= Void then
|
||||||
|
|||||||
@@ -8,56 +8,23 @@ note
|
|||||||
class JSON_LINKED_LIST_CONVERTER
|
class JSON_LINKED_LIST_CONVERTER
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
JSON_CONVERTER
|
JSON_LIST_CONVERTER
|
||||||
|
redefine
|
||||||
|
object
|
||||||
|
end
|
||||||
|
|
||||||
create
|
create
|
||||||
make
|
make
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make
|
|
||||||
do
|
|
||||||
create object.make
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
feature -- Access
|
||||||
|
|
||||||
object: LINKED_LIST [detachable ANY]
|
object: LINKED_LIST [detachable ANY]
|
||||||
|
|
||||||
feature -- Conversion
|
feature {NONE} -- Factory
|
||||||
|
|
||||||
from_json (j: like to_json): detachable like object
|
new_object (nb: INTEGER): like object
|
||||||
local
|
|
||||||
i: INTEGER
|
|
||||||
do
|
do
|
||||||
create Result.make
|
create Result.make
|
||||||
from
|
|
||||||
i := 1
|
|
||||||
until
|
|
||||||
i > j.count
|
|
||||||
loop
|
|
||||||
Result.extend (json.object (j [i], Void))
|
|
||||||
i := i + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
to_json (o: like object): JSON_ARRAY
|
|
||||||
local
|
|
||||||
c: ITERATION_CURSOR [detachable ANY]
|
|
||||||
do
|
|
||||||
create Result.make_array
|
|
||||||
from
|
|
||||||
c := o.new_cursor
|
|
||||||
until
|
|
||||||
c.after
|
|
||||||
loop
|
|
||||||
if attached json.value (c.item) as v then
|
|
||||||
Result.add (v)
|
|
||||||
else
|
|
||||||
check attached_value: False end
|
|
||||||
end
|
|
||||||
c.forth
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end -- class JSON_LINKED_LIST_CONVERTER
|
end -- class JSON_LINKED_LIST_CONVERTER
|
||||||
|
|||||||
74
library/kernel/converters/json_list_converter.e
Normal file
74
library/kernel/converters/json_list_converter.e
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
note
|
||||||
|
description: "A JSON converter for LIST [ANY]"
|
||||||
|
author: "Paul Cohen"
|
||||||
|
date: "$Date$"
|
||||||
|
revision: "$Revision$"
|
||||||
|
file: "$HeadURL: $"
|
||||||
|
|
||||||
|
deferred class JSON_LIST_CONVERTER
|
||||||
|
|
||||||
|
inherit
|
||||||
|
JSON_CONVERTER
|
||||||
|
|
||||||
|
feature {NONE} -- Initialization
|
||||||
|
|
||||||
|
make
|
||||||
|
do
|
||||||
|
object := new_object (0)
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Access
|
||||||
|
|
||||||
|
object: LIST [detachable ANY]
|
||||||
|
|
||||||
|
feature {NONE} -- Factory
|
||||||
|
|
||||||
|
new_object (nb: INTEGER): like object
|
||||||
|
deferred
|
||||||
|
ensure
|
||||||
|
Result /= Void
|
||||||
|
end
|
||||||
|
|
||||||
|
feature -- Conversion
|
||||||
|
|
||||||
|
from_json (j: attached like to_json): detachable like object
|
||||||
|
local
|
||||||
|
i: INTEGER
|
||||||
|
do
|
||||||
|
Result := new_object (j.count)
|
||||||
|
from
|
||||||
|
i := 1
|
||||||
|
until
|
||||||
|
i > j.count
|
||||||
|
loop
|
||||||
|
Result.extend (json.object (j [i], Void))
|
||||||
|
i := i + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
to_json (o: like object): detachable JSON_ARRAY
|
||||||
|
local
|
||||||
|
c: ITERATION_CURSOR [detachable ANY]
|
||||||
|
jv: detachable JSON_VALUE
|
||||||
|
failed: BOOLEAN
|
||||||
|
do
|
||||||
|
create Result.make_array
|
||||||
|
from
|
||||||
|
c := o.new_cursor
|
||||||
|
until
|
||||||
|
c.after
|
||||||
|
loop
|
||||||
|
jv := json.value (c.item)
|
||||||
|
if jv /= Void then
|
||||||
|
Result.add (jv)
|
||||||
|
else
|
||||||
|
failed := True
|
||||||
|
end
|
||||||
|
c.forth
|
||||||
|
end
|
||||||
|
if failed then
|
||||||
|
Result := Void
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end -- class JSON_ARRAYED_LIST_CONVERTER
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
note
|
note
|
||||||
description: "Core factory class for creating JSON objects and corresponding Eiffel objects."
|
description: "Core factory class for creating JSON objects and corresponding Eiffel objects."
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: $"
|
date: "$Date$"
|
||||||
revision: "$Revision: $"
|
revision: "$Revision$"
|
||||||
file: "$HeadURL: $"
|
file: "$HeadURL: $"
|
||||||
|
|
||||||
class EJSON
|
class EJSON
|
||||||
@@ -69,7 +69,7 @@ feature -- Access
|
|||||||
elseif attached {STRING_8} an_object as s8 then
|
elseif attached {STRING_8} an_object as s8 then
|
||||||
create {JSON_STRING} Result.make_json (s8)
|
create {JSON_STRING} Result.make_json (s8)
|
||||||
elseif attached {STRING_32} an_object as s32 then
|
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 ...
|
create {JSON_STRING} Result.make_json_from_string_32 (s32)
|
||||||
end
|
end
|
||||||
|
|
||||||
if Result = Void then
|
if Result = Void then
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ note
|
|||||||
JSON_FACTORY instance.
|
JSON_FACTORY instance.
|
||||||
]"
|
]"
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: $"
|
date: "$Date$"
|
||||||
revision: "$Revision: $"
|
revision: "$Revision: 89185 $"
|
||||||
file: "$HeadURL: $"
|
file: "$HeadURL: $"
|
||||||
|
|
||||||
class SHARED_EJSON
|
class SHARED_EJSON
|
||||||
@@ -17,14 +17,20 @@ feature
|
|||||||
|
|
||||||
json: EJSON
|
json: EJSON
|
||||||
-- A shared EJSON instance with default converters for
|
-- A shared EJSON instance with default converters for
|
||||||
-- DS_LINKED_LIST [ANY] and DS_HASH_TABLE [ANY, HASHABLE]
|
--LINKED_LIST [ANY] and HASH_TABLE [ANY, HASHABLE]
|
||||||
local
|
local
|
||||||
|
jalc: JSON_ARRAYED_LIST_CONVERTER
|
||||||
jllc: JSON_LINKED_LIST_CONVERTER
|
jllc: JSON_LINKED_LIST_CONVERTER
|
||||||
jhtc: JSON_HASH_TABLE_CONVERTER
|
jhtc: JSON_HASH_TABLE_CONVERTER
|
||||||
once
|
once
|
||||||
create Result
|
create Result
|
||||||
|
|
||||||
|
create jalc.make
|
||||||
|
Result.add_converter (jalc)
|
||||||
|
|
||||||
create jllc.make
|
create jllc.make
|
||||||
Result.add_converter (jllc)
|
Result.add_converter (jllc)
|
||||||
|
|
||||||
create jhtc.make
|
create jhtc.make
|
||||||
Result.add_converter (jhtc)
|
Result.add_converter (jhtc)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
note
|
note
|
||||||
description: "A JSON converter for AUTHOR"
|
description: "A JSON converter for AUTHOR"
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: 2010-03-08 20:46:59 -0300 (Mon, 08 Mar 2010) $"
|
date: "$Date$"
|
||||||
revision: "$Revision: 82 $"
|
revision: "$Revision$"
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_author_converter.e $"
|
|
||||||
|
|
||||||
class JSON_AUTHOR_CONVERTER
|
class JSON_AUTHOR_CONVERTER
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
note
|
note
|
||||||
description: "A JSON converter for BOOK_COLLECTION"
|
description: "A JSON converter for BOOK_COLLECTION"
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: 2010-03-08 20:46:59 -0300 (Mon, 08 Mar 2010) $"
|
date: "$Date$"
|
||||||
revision: "$Revision: 82 $"
|
revision: "$Revision$"
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_book_collection_converter.e $"
|
|
||||||
|
|
||||||
class JSON_BOOK_COLLECTION_CONVERTER
|
class JSON_BOOK_COLLECTION_CONVERTER
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
note
|
note
|
||||||
description: "A JSON converter for BOOK"
|
description: "A JSON converter for BOOK"
|
||||||
author: "Paul Cohen"
|
author: "Paul Cohen"
|
||||||
date: "$Date: 2010-03-08 20:46:59 -0300 (Mon, 08 Mar 2010) $"
|
date: "$Date$"
|
||||||
revision: "$Revision: 82 $"
|
revision: "$Revision$"
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_book_converter.e $"
|
|
||||||
|
|
||||||
class JSON_BOOK_CONVERTER
|
class JSON_BOOK_CONVERTER
|
||||||
|
|
||||||
|
|||||||
@@ -632,7 +632,7 @@ feature -- Test
|
|||||||
jrep := "%"foo\\bar%""
|
jrep := "%"foo\\bar%""
|
||||||
create parser.make_parser (jrep)
|
create parser.make_parser (jrep)
|
||||||
if attached {JSON_STRING} parser.parse as jstring then
|
if attached {JSON_STRING} parser.parse as jstring then
|
||||||
assert ("unescaped string %"foo\\bar%" to %"foo\bar%"", jstring.unescaped_string_8.same_string ("foo\bar"))
|
assert ("unescaped string %"foo\\bar%" to %"foo\bar%"", jstring.unescaped_string.same_string ("foo\bar"))
|
||||||
end
|
end
|
||||||
|
|
||||||
create js.make_json_from_string_32 ({STRING_32}"%/20320/%/22909/")
|
create js.make_json_from_string_32 ({STRING_32}"%/20320/%/22909/")
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
class AUTHOR
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make (a_name: UC_STRING) is
|
|
||||||
do
|
|
||||||
set_name (a_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
name: UC_STRING
|
|
||||||
|
|
||||||
feature -- Status setting
|
|
||||||
|
|
||||||
set_name (a_name: UC_STRING) is
|
|
||||||
do
|
|
||||||
name := a_name
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class AUTHOR
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
class BOOK
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make (a_title: UC_STRING; an_author: AUTHOR; an_isbn: UC_STRING) is
|
|
||||||
do
|
|
||||||
set_title (a_title)
|
|
||||||
set_author (an_author)
|
|
||||||
set_isbn (an_isbn)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
title: UC_STRING
|
|
||||||
|
|
||||||
isbn: UC_STRING
|
|
||||||
|
|
||||||
author: AUTHOR
|
|
||||||
|
|
||||||
feature -- Status setting
|
|
||||||
|
|
||||||
set_title (a_title: UC_STRING) is
|
|
||||||
do
|
|
||||||
title := a_title
|
|
||||||
end
|
|
||||||
|
|
||||||
set_author (an_author: AUTHOR) is
|
|
||||||
do
|
|
||||||
author := an_author
|
|
||||||
end
|
|
||||||
|
|
||||||
set_isbn (an_isbn: UC_STRING) is
|
|
||||||
do
|
|
||||||
isbn := an_isbn
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class BOOK
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
class BOOK_COLLECTION
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make (a_name: UC_STRING) is
|
|
||||||
do
|
|
||||||
set_name (a_name)
|
|
||||||
create book_index.make (10)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
name: UC_STRING
|
|
||||||
|
|
||||||
books: DS_LIST [BOOK] is
|
|
||||||
local
|
|
||||||
c: DS_HASH_TABLE_CURSOR [DS_LIST [BOOK], UC_STRING]
|
|
||||||
do
|
|
||||||
from
|
|
||||||
create {DS_LINKED_LIST [BOOK]} Result.make
|
|
||||||
c := book_index.new_cursor
|
|
||||||
c.start
|
|
||||||
until
|
|
||||||
c.after
|
|
||||||
loop
|
|
||||||
Result.append_last (c.item)
|
|
||||||
c.forth
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
books_by_author (an_author: UC_STRING): DS_LIST [BOOK] is
|
|
||||||
do
|
|
||||||
if book_index.has (an_author) then
|
|
||||||
Result := book_index @ an_author
|
|
||||||
else
|
|
||||||
create {DS_LINKED_LIST [BOOK]} Result.make
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Status setting
|
|
||||||
|
|
||||||
set_name (a_name: UC_STRING) is
|
|
||||||
do
|
|
||||||
name := a_name
|
|
||||||
end
|
|
||||||
|
|
||||||
add_book (a_book: BOOK) is
|
|
||||||
local
|
|
||||||
l: DS_LIST [BOOK]
|
|
||||||
do
|
|
||||||
if book_index.has (a_book.author.name) then
|
|
||||||
l := book_index @ a_book.author.name
|
|
||||||
else
|
|
||||||
create {DS_LINKED_LIST [BOOK]} l.make
|
|
||||||
book_index.put (l, a_book.author.name)
|
|
||||||
end
|
|
||||||
l.put_last (a_book)
|
|
||||||
end
|
|
||||||
|
|
||||||
add_books (book_list: like books) is
|
|
||||||
local
|
|
||||||
c: DS_LIST_CURSOR [BOOK]
|
|
||||||
do
|
|
||||||
from
|
|
||||||
c := book_list.new_cursor
|
|
||||||
c.start
|
|
||||||
until
|
|
||||||
c.after
|
|
||||||
loop
|
|
||||||
add_book (c.item)
|
|
||||||
c.forth
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
|
||||||
|
|
||||||
book_index: DS_HASH_TABLE [DS_LIST [BOOK], UC_STRING]
|
|
||||||
|
|
||||||
end -- class BOOK_COLLECTION
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
echo Compiling ejson_test (finalized)
|
|
||||||
ecb -finalize -c_compile -config ejson_test.ecf -batch -clean > NUL 2>&1
|
|
||||||
IF %ERRORLEVEL% EQU -1 goto ERROR
|
|
||||||
copy EIFGENs\ejson_test\F_code\ejson_test.exe ejson_test.exe
|
|
||||||
goto EOF
|
|
||||||
|
|
||||||
:ERROR
|
|
||||||
echo Error occurred during ejson_test compilation
|
|
||||||
goto EOF
|
|
||||||
|
|
||||||
:EOF
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
echo "ec -finalize -c_compile -config ejson_test.ecf > /dev/null 2>&1"
|
|
||||||
ec -finalize -c_compile -config ejson_test.ecf > /dev/null 2>&1
|
|
||||||
cp EIFGENs/ejson_test/F_code/ejson_test .
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
-- Gobo test (getest) configuration file for eJSON
|
|
||||||
|
|
||||||
test
|
|
||||||
ejson_test
|
|
||||||
|
|
||||||
default
|
|
||||||
class ("TEST_[A-Z0-9_]*")
|
|
||||||
feature ("test_[a-z0-9_]*")
|
|
||||||
prefix ("X")
|
|
||||||
testgen ("TESTGEN")
|
|
||||||
compile ("ec_compile.bat")
|
|
||||||
execute ("ejson_test.exe")
|
|
||||||
|
|
||||||
cluster
|
|
||||||
test_dir: "."
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
-- Gobo test (getest) configuration file for eJSON
|
|
||||||
|
|
||||||
test
|
|
||||||
ejson_test
|
|
||||||
|
|
||||||
default
|
|
||||||
class ("TEST_[A-Z0-9_]*")
|
|
||||||
feature ("test_[a-z0-9_]*")
|
|
||||||
prefix ("X")
|
|
||||||
testgen ("TESTGEN")
|
|
||||||
compile ("./ec_compile.sh")
|
|
||||||
execute ("./ejson_test")
|
|
||||||
|
|
||||||
cluster
|
|
||||||
test_dir: "."
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
<?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="ejson_test" uuid="9D4BC920-448B-4F84-9F95-5D47686EBC5E">
|
|
||||||
<target name="ejson_test">
|
|
||||||
<root class="EJSON_TEST" feature="make"/>
|
|
||||||
<file_rule>
|
|
||||||
<exclude>//.svn</exclude>
|
|
||||||
<exclude>/cvs$</exclude>
|
|
||||||
<exclude>/EIFGENs$</exclude>
|
|
||||||
</file_rule>
|
|
||||||
<option warning="true" void_safety="none" syntax="obsolete">
|
|
||||||
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
|
|
||||||
</option>
|
|
||||||
<library name="base" location="$ISE_LIBRARY/library/base/base.ecf"/>
|
|
||||||
<library name="json" location="../../library/json.ecf"/>
|
|
||||||
<library name="json_gobo_extension" location="../../library/json_gobo_extension.ecf"/>
|
|
||||||
<library name="gobo_kernel" location="$ISE_LIBRARY/library/gobo/gobo_kernel.ecf"/>
|
|
||||||
<library name="gobo_structure" location="$ISE_LIBRARY/library/gobo/gobo_structure.ecf"/>
|
|
||||||
<library name="gobo_test" location="$ISE_LIBRARY/library/gobo/gobo_test.ecf"/>
|
|
||||||
<cluster name="tests" location="." recursive="true"/>
|
|
||||||
</target>
|
|
||||||
</system>
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
indexing
|
|
||||||
description: "A JSON converter for AUTHOR"
|
|
||||||
author: "Paul Cohen"
|
|
||||||
date: "$Date$"
|
|
||||||
revision: "$Revision$"
|
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_author_converter.e $"
|
|
||||||
|
|
||||||
class JSON_AUTHOR_CONVERTER
|
|
||||||
|
|
||||||
inherit
|
|
||||||
JSON_CONVERTER
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
local
|
|
||||||
ucs: UC_STRING
|
|
||||||
do
|
|
||||||
create ucs.make_from_string ("")
|
|
||||||
create object.make (ucs)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
value: JSON_OBJECT
|
|
||||||
|
|
||||||
object: AUTHOR
|
|
||||||
|
|
||||||
feature -- Conversion
|
|
||||||
|
|
||||||
from_json (j: like value): like object is
|
|
||||||
local
|
|
||||||
ucs: UC_STRING
|
|
||||||
do
|
|
||||||
ucs ?= json.object (j.item (name_key), Void)
|
|
||||||
check ucs /= Void end
|
|
||||||
create Result.make (ucs)
|
|
||||||
end
|
|
||||||
|
|
||||||
to_json (o: like object): like value is
|
|
||||||
do
|
|
||||||
create Result.make
|
|
||||||
Result.put (json.value (o.name), name_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
|
||||||
|
|
||||||
name_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("name")
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class JSON_AUTHOR_CONVERTER
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
indexing
|
|
||||||
description: "A JSON converter for BOOK_COLLECTION"
|
|
||||||
author: "Paul Cohen"
|
|
||||||
date: "$Date$"
|
|
||||||
revision: "$Revision$"
|
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_book_collection_converter.e $"
|
|
||||||
|
|
||||||
class JSON_BOOK_COLLECTION_CONVERTER
|
|
||||||
|
|
||||||
inherit
|
|
||||||
JSON_CONVERTER
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
local
|
|
||||||
ucs: UC_STRING
|
|
||||||
do
|
|
||||||
create ucs.make_from_string ("")
|
|
||||||
create object.make (ucs)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
value: JSON_OBJECT
|
|
||||||
|
|
||||||
object: BOOK_COLLECTION
|
|
||||||
|
|
||||||
feature -- Conversion
|
|
||||||
|
|
||||||
from_json (j: like value): like object is
|
|
||||||
local
|
|
||||||
ucs: UC_STRING
|
|
||||||
ll: DS_LINKED_LIST [BOOK]
|
|
||||||
b: BOOK
|
|
||||||
ja: JSON_ARRAY
|
|
||||||
i: INTEGER
|
|
||||||
do
|
|
||||||
ucs ?= json.object (j.item (name_key), Void)
|
|
||||||
check ucs /= Void end
|
|
||||||
create Result.make (ucs)
|
|
||||||
ja ?= j.item (books_key)
|
|
||||||
check ja /= Void end
|
|
||||||
from
|
|
||||||
i := 1
|
|
||||||
create ll.make
|
|
||||||
until
|
|
||||||
i > ja.count
|
|
||||||
loop
|
|
||||||
b ?= json.object (ja [i], "BOOK")
|
|
||||||
check b /= Void end
|
|
||||||
ll.put_last (b)
|
|
||||||
i := i + 1
|
|
||||||
end
|
|
||||||
check ll /= Void end
|
|
||||||
Result.add_books (ll)
|
|
||||||
end
|
|
||||||
|
|
||||||
to_json (o: like object): like value is
|
|
||||||
do
|
|
||||||
create Result.make
|
|
||||||
Result.put (json.value (o.name), name_key)
|
|
||||||
Result.put (json.value (o.books), books_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
|
||||||
|
|
||||||
name_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("name")
|
|
||||||
end
|
|
||||||
|
|
||||||
books_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("books")
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class JSON_BOOK_COLLECTION_CONVERTER
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
indexing
|
|
||||||
description: "A JSON converter for BOOK"
|
|
||||||
author: "Paul Cohen"
|
|
||||||
date: "$Date$"
|
|
||||||
revision: "$Revision$"
|
|
||||||
file: "$HeadURL: https://svn.origo.ethz.ch/ejson/branches/POC-converters-factory/test/json_book_converter.e $"
|
|
||||||
|
|
||||||
class JSON_BOOK_CONVERTER
|
|
||||||
|
|
||||||
inherit
|
|
||||||
JSON_CONVERTER
|
|
||||||
|
|
||||||
create
|
|
||||||
make
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
local
|
|
||||||
ucs: UC_STRING
|
|
||||||
a: AUTHOR
|
|
||||||
do
|
|
||||||
create ucs.make_from_string ("")
|
|
||||||
create a.make (ucs)
|
|
||||||
create object.make (ucs, a, ucs)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Access
|
|
||||||
|
|
||||||
value: JSON_OBJECT
|
|
||||||
|
|
||||||
object: BOOK
|
|
||||||
|
|
||||||
feature -- Conversion
|
|
||||||
|
|
||||||
from_json (j: like value): like object is
|
|
||||||
local
|
|
||||||
ucs1, ucs2: UC_STRING
|
|
||||||
a: AUTHOR
|
|
||||||
do
|
|
||||||
ucs1 ?= json.object (j.item (title_key), Void)
|
|
||||||
check ucs1 /= Void end
|
|
||||||
ucs2 ?= json.object (j.item (isbn_key), Void)
|
|
||||||
check ucs2 /= Void end
|
|
||||||
a ?= json.object (j.item (author_key), "AUTHOR")
|
|
||||||
check a /= Void end
|
|
||||||
create Result.make (ucs1, a, ucs2)
|
|
||||||
end
|
|
||||||
|
|
||||||
to_json (o: like object): like value is
|
|
||||||
do
|
|
||||||
create Result.make
|
|
||||||
Result.put (json.value (o.title), title_key)
|
|
||||||
Result.put (json.value (o.isbn), isbn_key)
|
|
||||||
Result.put (json.value (o.author), author_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
feature {NONE} -- Implementation
|
|
||||||
|
|
||||||
title_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("title")
|
|
||||||
end
|
|
||||||
|
|
||||||
isbn_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("isbn")
|
|
||||||
end
|
|
||||||
|
|
||||||
author_key: JSON_STRING is
|
|
||||||
once
|
|
||||||
create Result.make_json ("author")
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class JSON_BOOK_CONVERTER
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
To compile and run the test program do as follows:
|
|
||||||
|
|
||||||
1. Make sure you have a compiled version of getest in your PATH.
|
|
||||||
|
|
||||||
2. In this dircetory, run the command:
|
|
||||||
|
|
||||||
$ getest --verbose --ise ejson_test.cfg
|
|
||||||
|
|
||||||
|
|
||||||
Note: on Windows, you should use ejson_test-win.cfg
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
class TEST_DS
|
|
||||||
|
|
||||||
inherit
|
|
||||||
SHARED_GOBO_EJSON
|
|
||||||
|
|
||||||
TS_TEST_CASE
|
|
||||||
|
|
||||||
create
|
|
||||||
make_default
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
-- Create test object.
|
|
||||||
do
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Test
|
|
||||||
|
|
||||||
test_ds_linked_list_converter is
|
|
||||||
local
|
|
||||||
jc: JSON_DS_LINKED_LIST_CONVERTER
|
|
||||||
l: DS_LINKED_LIST [STRING]
|
|
||||||
l2: DS_LINKED_LIST [ANY]
|
|
||||||
s: STRING
|
|
||||||
jv: JSON_VALUE
|
|
||||||
do
|
|
||||||
create jc.make
|
|
||||||
json.add_converter (jc)
|
|
||||||
create l.make
|
|
||||||
s := "foo"
|
|
||||||
l.put_last (s)
|
|
||||||
s := "bar"
|
|
||||||
l.put_last (s)
|
|
||||||
jv := json.value (l)
|
|
||||||
assert ("jv /= Void", jv /= Void)
|
|
||||||
s := jv.representation
|
|
||||||
l2 ?= json.object (jv, "DS_LINKED_LIST")
|
|
||||||
assert ("l2 /= Void", l2 /= Void)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_ds_hash_table_converter is
|
|
||||||
local
|
|
||||||
tc: JSON_DS_HASH_TABLE_CONVERTER
|
|
||||||
t: DS_HASH_TABLE [STRING, STRING]
|
|
||||||
t2: DS_HASH_TABLE [ANY, HASHABLE]
|
|
||||||
s: STRING
|
|
||||||
ucs_key, ucs_value: UC_STRING
|
|
||||||
jv: JSON_VALUE
|
|
||||||
do
|
|
||||||
create tc.make
|
|
||||||
json.add_converter (tc)
|
|
||||||
create t.make (2)
|
|
||||||
t.put ("foo", "1")
|
|
||||||
t.put ("bar", "2")
|
|
||||||
jv := json.value (t)
|
|
||||||
assert ("jv /= Void", jv /= Void)
|
|
||||||
s := jv.representation
|
|
||||||
t2 ?= json.object (jv, "DS_HASH_TABLE")
|
|
||||||
assert ("t2 /= Void", t2 /= Void)
|
|
||||||
create ucs_key.make_from_string ("1")
|
|
||||||
ucs_value ?= t2 @ ucs_key
|
|
||||||
assert ("ucs_value /= Void", ucs_value /= Void)
|
|
||||||
assert ("ucs_value.string.is_equal (%"foo%")", ucs_value.string.is_equal ("foo"))
|
|
||||||
create ucs_key.make_from_string ("2")
|
|
||||||
ucs_value ?= t2 @ ucs_key
|
|
||||||
assert ("ucs_value /= Void", ucs_value /= Void)
|
|
||||||
assert ("ucs_value.string.is_equal (%"bar%")", ucs_value.string.is_equal ("bar"))
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class TEST_DS
|
|
||||||
@@ -1,703 +0,0 @@
|
|||||||
class TEST_JSON_CORE
|
|
||||||
|
|
||||||
inherit
|
|
||||||
TS_TEST_CASE
|
|
||||||
SHARED_EJSON
|
|
||||||
|
|
||||||
create
|
|
||||||
make_default
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
-- Create test object.
|
|
||||||
do
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Test
|
|
||||||
|
|
||||||
test_json_number_and_integer is
|
|
||||||
local
|
|
||||||
i: INTEGER
|
|
||||||
i8: INTEGER_8
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
i := 42
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_integer (i)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42%")", jn.representation.is_equal ("42"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (i)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42%")", jn.representation.is_equal ("42"))
|
|
||||||
-- JSON representation-> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_8 since the value is 42
|
|
||||||
jrep := "42"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i8 := 0
|
|
||||||
i8 ?= json.object (jn, Void)
|
|
||||||
assert ("i8 = 42", i8 = 42)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_integer_8 is
|
|
||||||
local
|
|
||||||
i8: INTEGER_8
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
i8 := 42
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_integer (i8)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42%")", jn.representation.is_equal ("42"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (i8)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42%")", jn.representation.is_equal ("42"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_8 since the value is 42
|
|
||||||
jrep := "42"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i8 := 0
|
|
||||||
i8 ?= json.object (jn, Void)
|
|
||||||
assert ("i8 = 42", i8 = 42)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_integer_16 is
|
|
||||||
local
|
|
||||||
i16: INTEGER_16
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
i16 := 300
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_integer (i16)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"300%")", jn.representation.is_equal ("300"))
|
|
||||||
-- Eiffel value -> JSON with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (i16)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"300%")", jn.representation.is_equal ("300"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_16 since the value is 300
|
|
||||||
jrep := "300"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i16 := 0
|
|
||||||
i16 ?= json.object (jn, Void)
|
|
||||||
assert ("i16 = 300", i16 = 300)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_integer_32 is
|
|
||||||
local
|
|
||||||
i32: INTEGER_32
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
i32 := 100000
|
|
||||||
-- Eiffel value -> JSON representation -> JSON value
|
|
||||||
create jn.make_integer (i32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"100000%")", jn.representation.is_equal ("100000"))
|
|
||||||
-- Eiffel value -> JSON representation -> JSON value with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (i32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"100000%")", jn.representation.is_equal ("100000"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_32 since the value is 100000
|
|
||||||
jrep := "100000"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i32 := 0
|
|
||||||
i32 ?= json.object (jn, Void)
|
|
||||||
assert ("i32 = 100000", i32 = 100000)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_integer_64 is
|
|
||||||
local
|
|
||||||
i64: INTEGER_64
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
i64 := 42949672960
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_integer (i64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42949672960%")", jn.representation.is_equal ("42949672960"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (i64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"42949672960%")", jn.representation.is_equal ("42949672960"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_32 since the value is 42949672960
|
|
||||||
jrep := "42949672960"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i64 := 0
|
|
||||||
i64 ?= json.object (jn, Void)
|
|
||||||
assert ("i64 = 42949672960", i64 = 42949672960)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_natural_8 is
|
|
||||||
local
|
|
||||||
n8: NATURAL_8
|
|
||||||
i16: INTEGER_16
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
n8 := 200
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_natural (n8)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"200%")", jn.representation.is_equal ("200"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (n8)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"200%")", jn.representation.is_equal ("200"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_16 since the value is 200
|
|
||||||
jrep := "200"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i16 := 0
|
|
||||||
i16 ?= json.object (jn, Void)
|
|
||||||
assert ("i16 = 200", i16 = 200)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_natural_16 is
|
|
||||||
local
|
|
||||||
n16: NATURAL_16
|
|
||||||
i32: INTEGER_32
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
n16 := 32768
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_natural (n16)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"32768%")", jn.representation.is_equal ("32768"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (n16)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"32768%")", jn.representation.is_equal ("32768"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_32 since the value is 32768
|
|
||||||
jrep := "32768"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i32 := 0
|
|
||||||
i32 ?= json.object (jn, Void)
|
|
||||||
assert ("i32 = 32768", i32 = 32768)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_natural_32 is
|
|
||||||
local
|
|
||||||
n32: NATURAL_32
|
|
||||||
i64: INTEGER_64
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
n32 := 2147483648
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_natural (n32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"2147483648%")", jn.representation.is_equal ("2147483648"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn := Void
|
|
||||||
jn ?= json.value (n32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"2147483648%")", jn.representation.is_equal ("2147483648"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_64 since the value is 2147483648
|
|
||||||
jrep := "2147483648"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
i64 := 0
|
|
||||||
i64 ?= json.object (jn, Void)
|
|
||||||
assert ("i64 = 2147483648", i64 = 2147483648)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_large_integers is
|
|
||||||
local
|
|
||||||
jrep: STRING
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
n64: NATURAL_64
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
n64 := 9223372036854775808
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_natural (n64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"9223372036854775808%")", jn.representation.is_equal ("9223372036854775808"))
|
|
||||||
jn := Void
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn ?= json.value (n64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"9223372036854775808%")", jn.representation.is_equal ("9223372036854775808"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- we know it is INTEGER_32 since the value is 42949672960
|
|
||||||
jrep := "9223372036854775808" -- 1 higher than largest positive number that can be represented by INTEGER 64
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
n64 := 0
|
|
||||||
n64 ?= json.object (jn, Void)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_eiffel_real is
|
|
||||||
local
|
|
||||||
r: REAL
|
|
||||||
r64: REAL_64
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
r := 3.14
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_real (r)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1400001049041748%")", jn.representation.is_equal ("3.1400001049041748"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn ?= json.value (r)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1400001049041748%")", jn.representation.is_equal ("3.1400001049041748"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will always return a REAL_64 if the value
|
|
||||||
-- of the JSON number is a floating point number
|
|
||||||
jrep := "3.14"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
r64 := 0
|
|
||||||
r64 ?= json.object (jn, Void)
|
|
||||||
assert ("r64 = 3.1400000000000001", r64 = 3.1400000000000001)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_eiffel_real_32 is
|
|
||||||
local
|
|
||||||
r32: REAL_32
|
|
||||||
r64: REAL_64
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
r32 := 3.14
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_real (r32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1400001049041748%")", jn.representation.is_equal ("3.1400001049041748"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn ?= json.value (r32)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1400001049041748%")", jn.representation.is_equal ("3.1400001049041748"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "3.1400001049041748"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
r64 := 0
|
|
||||||
r64 ?= json.object (jn, Void)
|
|
||||||
assert ("r64 = 3.1400001049041748", r64 = 3.1400001049041748)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_number_and_eiffel_real_64 is
|
|
||||||
local
|
|
||||||
r64: REAL_64
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
r64 := 3.1415926535897931
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn.make_real (r64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1415926535897931%")", jn.representation.is_equal ("3.1415926535897931"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn ?= json.value (r64)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"3.1415926535897931%")", jn.representation.is_equal ("3.1415926535897931"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "3.1415926535897931"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
r64 := 0
|
|
||||||
r64 ?= json.object (jn, Void)
|
|
||||||
assert ("r64 = 3.1415926535897931", r64 = 3.1415926535897931)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_boolean is
|
|
||||||
local
|
|
||||||
b: BOOLEAN
|
|
||||||
jb: JSON_BOOLEAN
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
b := True
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jb.make_boolean (b)
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
assert ("jb.representation.is_equal (%"true%")", jb.representation.is_equal ("true"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jb ?= json.value (b)
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
assert ("jb.representation.is_equal (%"true%")", jb.representation.is_equal ("true"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "true"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jb := Void
|
|
||||||
jb ?= parser.parse
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
b := False
|
|
||||||
b ?= json.object (jb, Void)
|
|
||||||
assert ("b = True", b = True)
|
|
||||||
|
|
||||||
b := False
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jb.make_boolean (b)
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
assert ("jb.representation.is_equal (%"false%")", jb.representation.is_equal ("false"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jb ?= json.value (b)
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
assert ("jb.representation.is_equal (%"false%")", jb.representation.is_equal ("false"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "false"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jb := Void
|
|
||||||
jb ?= parser.parse
|
|
||||||
assert ("jb /= Void", jb /= Void)
|
|
||||||
b := True
|
|
||||||
b ?= json.object (jb, Void)
|
|
||||||
assert ("b = False", b = False)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_null is
|
|
||||||
local
|
|
||||||
a: ANY
|
|
||||||
dummy_object: STRING
|
|
||||||
jn: JSON_NULL
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create jn
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"%"null%"%")", jn.representation.is_equal ("null"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
jn ?= json.value (Void)
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
assert ("jn.representation.is_equal (%"null%")", jn.representation.is_equal ("null"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "null"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jn := Void
|
|
||||||
jn ?= parser.parse
|
|
||||||
assert ("jn /= Void", jn /= Void)
|
|
||||||
create dummy_object.make_empty
|
|
||||||
a := dummy_object
|
|
||||||
a ?= json.object (jn, Void)
|
|
||||||
assert ("a = Void", a = Void)
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_string_and_character is
|
|
||||||
local
|
|
||||||
c: CHARACTER
|
|
||||||
js: JSON_STRING
|
|
||||||
ucs: UC_STRING
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
c := 'a'
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create js.make_json (c.out)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
js ?= json.value (c)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "%"a%""
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
js := Void
|
|
||||||
js ?= parser.parse
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
ucs ?= json.object (js, Void)
|
|
||||||
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_string_and_string is
|
|
||||||
local
|
|
||||||
s: STRING
|
|
||||||
js: JSON_STRING
|
|
||||||
ucs: UC_STRING
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
s := "foobar"
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create js.make_json (s)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
js ?= json.value (s)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "%"foobar%""
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
js := Void
|
|
||||||
js ?= parser.parse
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
ucs ?= json.object (js, Void)
|
|
||||||
assert ("ucs.string.is_equal (%"foobar%")", ucs.string.is_equal ("foobar"))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_string_and_uc_string is
|
|
||||||
local
|
|
||||||
js: JSON_STRING
|
|
||||||
ucs: UC_STRING
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
create ucs.make_from_string ("foobar")
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create js.make_json (ucs)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
js ?= json.value (ucs)
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal ("%"foobar%""))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
jrep := "%"foobar%""
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
js := Void
|
|
||||||
js ?= parser.parse
|
|
||||||
assert ("js /= Void", js /= Void)
|
|
||||||
ucs := Void
|
|
||||||
ucs ?= json.object (js, Void)
|
|
||||||
assert ("ucs.string.is_equal (%"foobar%")", ucs.string.is_equal ("foobar"))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_array is
|
|
||||||
local
|
|
||||||
ll: LINKED_LIST [INTEGER_8]
|
|
||||||
ll2: LINKED_LIST [ANY]
|
|
||||||
ja: JSON_ARRAY
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
i, n: INTEGER
|
|
||||||
do
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
create ll.make
|
|
||||||
ll.extend (0)
|
|
||||||
ll.extend (1)
|
|
||||||
ll.extend (1)
|
|
||||||
ll.extend (2)
|
|
||||||
ll.extend (3)
|
|
||||||
ll.extend (5)
|
|
||||||
-- Note: Currently there is no simple way of creating a JSON_ARRAY
|
|
||||||
-- from an LINKED_LIST.
|
|
||||||
create ja.make_array
|
|
||||||
from
|
|
||||||
ll.start
|
|
||||||
until
|
|
||||||
ll.after
|
|
||||||
loop
|
|
||||||
create jn.make_integer (ll.item)
|
|
||||||
ja.add (jn)
|
|
||||||
ll.forth
|
|
||||||
end
|
|
||||||
assert ("ja /= Void", ja /= Void)
|
|
||||||
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
ja := Void
|
|
||||||
ja ?= json.value (ll)
|
|
||||||
assert ("ja /= Void", ja /= Void)
|
|
||||||
assert ("ja.representation.is_equal (%"[0,1,1,2,3,5]%")", ja.representation.is_equal ("[0,1,1,2,3,5]"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value
|
|
||||||
-- Note: The JSON_FACTORY will return the smallest INTEGER_* object
|
|
||||||
-- that can represent the value of the JSON number, in this case
|
|
||||||
-- it means we will get an LINKED_LIST [ANY] containing the INTEGER_8
|
|
||||||
-- values 0, 1, 1, 2, 3, 5
|
|
||||||
jrep := "[0,1,1,2,3,5]"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
ja := Void
|
|
||||||
ja ?= parser.parse
|
|
||||||
assert ("ja /= Void", ja /= Void)
|
|
||||||
ll2 ?= json.object (ja, Void)
|
|
||||||
assert ("ll2 /= Void", ll2 /= Void)
|
|
||||||
--ll.compare_objects
|
|
||||||
--ll2.compare_objects
|
|
||||||
assert ("ll2.is_equal (ll)", ll2.is_equal (ll))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_object is
|
|
||||||
local
|
|
||||||
t, t2: HASH_TABLE [ANY, UC_STRING]
|
|
||||||
i: INTEGER
|
|
||||||
ucs_key, ucs: UC_STRING
|
|
||||||
a: ARRAY [INTEGER]
|
|
||||||
jo: JSON_OBJECT
|
|
||||||
jn: JSON_NUMBER
|
|
||||||
js_key, js: JSON_STRING
|
|
||||||
ja: JSON_ARRAY
|
|
||||||
jrep: STRING
|
|
||||||
parser: JSON_PARSER
|
|
||||||
do
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation
|
|
||||||
-- Note: Currently there is now way of creating a JSON_OBJECT from
|
|
||||||
-- a DS_HASH_TABLE, so we do it manually.
|
|
||||||
-- t = {"name": "foobar", "size": 42, "contents", [0, 1, 1, 2, 3, 5]}
|
|
||||||
create jo.make
|
|
||||||
create js_key.make_json ("name")
|
|
||||||
create js.make_json ("foobar")
|
|
||||||
jo.put (js, js_key)
|
|
||||||
create js_key.make_json ("size")
|
|
||||||
create jn.make_integer (42)
|
|
||||||
jo.put (jn, js_key)
|
|
||||||
create js_key.make_json ("contents")
|
|
||||||
create ja.make_array
|
|
||||||
create jn.make_integer (0)
|
|
||||||
ja.add (jn)
|
|
||||||
create jn.make_integer (1)
|
|
||||||
ja.add (jn)
|
|
||||||
create jn.make_integer (1)
|
|
||||||
ja.add (jn)
|
|
||||||
create jn.make_integer (2)
|
|
||||||
ja.add (jn)
|
|
||||||
create jn.make_integer (3)
|
|
||||||
ja.add (jn)
|
|
||||||
create jn.make_integer (5)
|
|
||||||
ja.add (jn)
|
|
||||||
jo.put (ja, js_key)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
assert ("jo.representation.is_equal (%"{%"size%":42,%"contents%":[0,1,1,2,3,5],%"name%":%"foobar%"}%")", jo.representation.is_equal ("{%"size%":42,%"contents%":[0,1,1,2,3,5],%"name%":%"foobar%"}"))
|
|
||||||
-- Eiffel value -> JSON value -> JSON representation with factory
|
|
||||||
create t.make (3)
|
|
||||||
create ucs_key.make_from_string ("name")
|
|
||||||
create ucs.make_from_string ("foobar")
|
|
||||||
t.put (ucs, ucs_key)
|
|
||||||
create ucs_key.make_from_string ("size")
|
|
||||||
i := 42
|
|
||||||
t.put (i, ucs_key)
|
|
||||||
create ucs_key.make_from_string ("contents")
|
|
||||||
a := <<0, 1, 1, 2, 3, 5>>
|
|
||||||
t.put (a, ucs_key)
|
|
||||||
jo := Void
|
|
||||||
jo ?= json.value (t)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
assert ("jo.representation.is_equal (%"{%"size%":42,%"contents%":[0,1,1,2,3,5],%"name%":%"foobar%"}%")", jo.representation.is_equal ("{%"size%":42,%"contents%":[0,1,1,2,3,5],%"name%":%"foobar%"}"))
|
|
||||||
-- JSON representation -> JSON value -> Eiffel value -> JSON value -> JSON representation
|
|
||||||
jrep := "{%"size%":42,%"contents%":[0,1,1,2,3,5],%"name%":%"foobar%"}"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jo := Void
|
|
||||||
jo ?= parser.parse
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
t2 ?= json.object (jo, Void)
|
|
||||||
assert ("t2 /= Void", t2 /= Void)
|
|
||||||
jo ?= json.value (t2)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (jo.representation))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_failed_json_conversion is
|
|
||||||
-- Test converting an Eiffel object to JSON that is based on a class
|
|
||||||
-- for which no JSON converter has been registered.
|
|
||||||
local
|
|
||||||
gv: KL_GOBO_VERSION
|
|
||||||
jv: JSON_VALUE
|
|
||||||
exception: BOOLEAN
|
|
||||||
do
|
|
||||||
if not exception then
|
|
||||||
create gv
|
|
||||||
jv := json.value (gv)
|
|
||||||
else
|
|
||||||
assert ("exceptions.is_developer_exception", exceptions.is_developer_exception)
|
|
||||||
assert ("exceptions.is_developer_exception_of_name", exceptions.is_developer_exception_of_name ("eJSON exception: Failed to convert Eiffel object to a JSON_VALUE: KL_GOBO_VERSION"))
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
exception := True
|
|
||||||
retry
|
|
||||||
end
|
|
||||||
|
|
||||||
test_json_failed_eiffel_conversion is
|
|
||||||
-- Test converting from a JSON value to an Eiffel object based on a
|
|
||||||
-- class for which no JSON converter has been registered.
|
|
||||||
local
|
|
||||||
gv: KL_GOBO_VERSION
|
|
||||||
jo: JSON_OBJECT
|
|
||||||
exception: BOOLEAN
|
|
||||||
do
|
|
||||||
if not exception then
|
|
||||||
create jo.make
|
|
||||||
gv ?= json.object (jo, "KL_GOBO_VERSION")
|
|
||||||
else
|
|
||||||
assert ("exceptions.is_developer_exception", exceptions.is_developer_exception)
|
|
||||||
assert ("exceptions.is_developer_exception_of_name", exceptions.is_developer_exception_of_name ("eJSON exception: Failed to convert JSON_VALUE to an Eiffel object: JSON_OBJECT -> KL_GOBO_VERSION"))
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
exception := True
|
|
||||||
retry
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class TEST_JSON_CORE
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
class TEST_JSON_CUSTOM_CLASSES
|
|
||||||
|
|
||||||
inherit
|
|
||||||
SHARED_EJSON
|
|
||||||
|
|
||||||
TS_TEST_CASE
|
|
||||||
|
|
||||||
create
|
|
||||||
make_default
|
|
||||||
|
|
||||||
feature {NONE} -- Initialization
|
|
||||||
|
|
||||||
make is
|
|
||||||
-- Create test object.
|
|
||||||
do
|
|
||||||
end
|
|
||||||
|
|
||||||
feature -- Test
|
|
||||||
|
|
||||||
test_custom_classes is
|
|
||||||
local
|
|
||||||
bc: BOOK_COLLECTION
|
|
||||||
jbc: JSON_BOOK_CONVERTER
|
|
||||||
jbcc: JSON_BOOK_COLLECTION_CONVERTER
|
|
||||||
jac: JSON_AUTHOR_CONVERTER
|
|
||||||
jo: JSON_OBJECT
|
|
||||||
parser: JSON_PARSER
|
|
||||||
jrep: STRING
|
|
||||||
do
|
|
||||||
create jbc.make
|
|
||||||
json.add_converter (jbc)
|
|
||||||
create jbcc.make
|
|
||||||
json.add_converter (jbcc)
|
|
||||||
create jac.make
|
|
||||||
json.add_converter (jac)
|
|
||||||
jrep := "{%"name%":%"Test collection%",%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}]}"
|
|
||||||
create parser.make_parser (jrep)
|
|
||||||
jo := Void
|
|
||||||
jo ?= parser.parse
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
bc := Void
|
|
||||||
bc ?= json.object (jo, "BOOK_COLLECTION")
|
|
||||||
assert ("bc /= Void", bc /= Void)
|
|
||||||
jo ?= json.value (bc)
|
|
||||||
assert ("jo /= Void", jo /= Void)
|
|
||||||
assert ("JSON representation is correct", jo.representation.is_equal ("{%"books%":[{%"title%":%"eJSON: The Definitive Guide%",%"isbn%":%"123123-413243%",%"author%":{%"name%":%"Foo Bar%"}}],%"name%":%"Test collection%"}"))
|
|
||||||
end
|
|
||||||
|
|
||||||
end -- class TEST_JSON_CUSTOM_CLASS
|
|
||||||
Reference in New Issue
Block a user