Implemented escaping of slash '/' only in case of '</' to avoid potential issue with javascript and </script> Many feature renaming to match Eiffel style and naming convention, kept previous feature as obsolete. Restructured the library to make easy extraction of "converter" classes if needed in the future. Updated part of the code to use new feature names.
83 lines
1.5 KiB
Plaintext
83 lines
1.5 KiB
Plaintext
note
|
|
description: "A JSON converter for HASH_TABLE [ANY, HASHABLE]"
|
|
author: "Paul Cohen"
|
|
date: "$Date: 2014-01-30 15:27:41 +0100 (jeu., 30 janv. 2014) $"
|
|
revision: "$Revision: 94128 $"
|
|
file: "$HeadURL: $"
|
|
|
|
class
|
|
JSON_HASH_TABLE_CONVERTER
|
|
|
|
inherit
|
|
|
|
JSON_CONVERTER
|
|
|
|
create
|
|
make
|
|
|
|
feature {NONE} -- Initialization
|
|
|
|
make
|
|
do
|
|
create object.make (0)
|
|
end
|
|
|
|
feature -- Access
|
|
|
|
object: HASH_TABLE [ANY, HASHABLE]
|
|
|
|
feature -- Conversion
|
|
|
|
from_json (j: attached like to_json): like object
|
|
do
|
|
create Result.make (j.count)
|
|
across
|
|
j as ic
|
|
loop
|
|
if attached json.object (ic.item, Void) as l_object then
|
|
if attached {HASHABLE} json.object (ic.key, Void) as h then
|
|
Result.put (l_object, h)
|
|
else
|
|
check
|
|
key_is_hashable: False
|
|
end
|
|
end
|
|
else
|
|
check
|
|
object_attached: False
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
to_json (o: like object): detachable JSON_OBJECT
|
|
local
|
|
js: JSON_STRING
|
|
failed: BOOLEAN
|
|
do
|
|
create Result.make
|
|
across
|
|
o as c
|
|
loop
|
|
if attached {JSON_STRING} json.value (c.key) as l_key then
|
|
js := l_key
|
|
else
|
|
if attached {READABLE_STRING_GENERAL} c.key as s_key then
|
|
create js.make_from_string_general (s_key)
|
|
else
|
|
create js.make_from_string (c.key.out)
|
|
end
|
|
end
|
|
if attached json.value (c.item) as jv then
|
|
Result.put (jv, js)
|
|
else
|
|
failed := True
|
|
end
|
|
end
|
|
if failed then
|
|
Result := Void
|
|
end
|
|
end
|
|
|
|
end -- class JSON_HASH_TABLE_CONVERTER
|