Merged converters and factory classes from POC_CONVERTERS_FACTORY to trunk
This commit is contained in:
41
library/kernel/converters/json_converter.e
Normal file
41
library/kernel/converters/json_converter.e
Normal file
@@ -0,0 +1,41 @@
|
||||
indexing
|
||||
description: "A JSON converter"
|
||||
author: "Paul Cohen"
|
||||
date: "$Date: $"
|
||||
revision: "$Revision: $"
|
||||
file: "$HeadURL: $"
|
||||
|
||||
deferred class JSON_CONVERTER
|
||||
|
||||
inherit
|
||||
SHARED_EJSON
|
||||
|
||||
feature -- Access
|
||||
|
||||
value: JSON_VALUE is
|
||||
-- JSON value
|
||||
deferred
|
||||
end
|
||||
|
||||
object: ANY is
|
||||
-- Eiffel object
|
||||
deferred
|
||||
end
|
||||
|
||||
feature -- Conversion
|
||||
|
||||
from_json (j: like value): like object is
|
||||
-- Convert from JSON value. Returns Void if unable to convert
|
||||
deferred
|
||||
end
|
||||
|
||||
to_json (o: like object): like value is
|
||||
-- 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
|
||||
|
||||
79
library/kernel/converters/json_hash_table_converter.e
Normal file
79
library/kernel/converters/json_hash_table_converter.e
Normal file
@@ -0,0 +1,79 @@
|
||||
indexing
|
||||
description: "A JSON converter for HASH_TABLE [ANY, HASHABLE]"
|
||||
author: "Paul Cohen"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
file: "$HeadURL: $"
|
||||
|
||||
class JSON_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: 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
|
||||
js: JSON_STRING
|
||||
jv: JSON_VALUE
|
||||
failed: BOOLEAN
|
||||
do
|
||||
create Result.make
|
||||
from
|
||||
o.start
|
||||
until
|
||||
o.after
|
||||
loop
|
||||
create js.make_json (o.key_for_iteration.out)
|
||||
jv := json.value (o.item_for_iteration)
|
||||
if jv /= Void then
|
||||
Result.put (jv, js)
|
||||
else
|
||||
failed := True
|
||||
end
|
||||
o.forth
|
||||
end
|
||||
if failed then
|
||||
Result := Void
|
||||
end
|
||||
end
|
||||
|
||||
end -- class JSON_HASH_TABLE_CONVERTER
|
||||
61
library/kernel/converters/json_linked_list_converter.e
Normal file
61
library/kernel/converters/json_linked_list_converter.e
Normal file
@@ -0,0 +1,61 @@
|
||||
indexing
|
||||
description: "A JSON converter for LINKED_LIST [ANY]"
|
||||
author: "Paul Cohen"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
file: "$HeadURL: $"
|
||||
|
||||
class JSON_LINKED_LIST_CONVERTER
|
||||
|
||||
inherit
|
||||
JSON_CONVERTER
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make is
|
||||
do
|
||||
create object.make
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
value: JSON_ARRAY
|
||||
|
||||
object: 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.extend (json.object (j [i], Void))
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
|
||||
to_json (o: like object): like value is
|
||||
local
|
||||
c: LINKED_LIST_CURSOR [ANY]
|
||||
do
|
||||
create Result.make_array
|
||||
from
|
||||
o.start
|
||||
until
|
||||
o.after
|
||||
loop
|
||||
Result.add (json.value (o.item))
|
||||
o.forth
|
||||
end
|
||||
end
|
||||
|
||||
end -- class JSON_LINKED_LIST_CONVERTER
|
||||
Reference in New Issue
Block a user