Improve converters.

Replace old syntax with new one and improve
implementation.
This commit is contained in:
Conaclos
2014-06-30 18:31:42 +02:00
parent c5e1b1ee69
commit 052860b62c
3 changed files with 48 additions and 42 deletions
@@ -4,7 +4,8 @@ note
date: "$Date$"
revision: "$Revision$"
class JSON_BOOK_COLLECTION_CONVERTER
class
JSON_BOOK_COLLECTION_CONVERTER
inherit
JSON_CONVERTER
@@ -30,30 +31,32 @@ feature -- Conversion
from_json (j: like to_json): detachable like object
local
ucs: detachable STRING_32
ll: LINKED_LIST [BOOK]
b: detachable BOOK
ja: detachable JSON_ARRAY
i: INTEGER
l_books: LINKED_LIST [BOOK]
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.force (b)
i := i + 1
if
attached {STRING_32} json.object (j.item (name_key), Void) as l_name and
attached {JSON_ARRAY} j.item (books_key) as l_json_array
then
create Result.make (l_name)
create l_books.make
across
l_json_array as it
until
Result = Void
loop
if attached {BOOK} json.object (it.item, "BOOK") as l_book then
l_books.extend (l_book)
else
Result := Void
-- Failed
end
end
if Result /= Void then
Result.add_books (l_books)
end
end
check ll /= Void end
Result.add_books (ll)
end
to_json (o: like object): JSON_OBJECT
@@ -66,11 +69,13 @@ feature -- Conversion
feature {NONE} -- Implementation
name_key: JSON_STRING
-- Collection's name label.
once
create Result.make_json ("name")
end
books_key: JSON_STRING
-- Book list label.
once
create Result.make_json ("books")
end