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

View File

@@ -4,7 +4,8 @@ note
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class JSON_AUTHOR_CONVERTER class
JSON_AUTHOR_CONVERTER
inherit inherit
JSON_CONVERTER JSON_CONVERTER
@@ -29,12 +30,10 @@ feature -- Access
feature -- Conversion feature -- Conversion
from_json (j: like to_json): detachable like object from_json (j: like to_json): detachable like object
local
ucs: detachable STRING_32
do do
ucs ?= json.object (j.item (name_key), Void) if attached {STRING_32} json.object (j.item (name_key), Void) as l_name then
check ucs /= Void end create Result.make (l_name)
create Result.make (ucs) end
end end
to_json (o: like object): JSON_OBJECT to_json (o: like object): JSON_OBJECT
@@ -46,6 +45,7 @@ feature -- Conversion
feature {NONE} -- Implementation feature {NONE} -- Implementation
name_key: JSON_STRING name_key: JSON_STRING
-- Author's name label.
once once
create Result.make_json ("name") create Result.make_json ("name")
end end

View File

@@ -4,7 +4,8 @@ note
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class JSON_BOOK_COLLECTION_CONVERTER class
JSON_BOOK_COLLECTION_CONVERTER
inherit inherit
JSON_CONVERTER JSON_CONVERTER
@@ -30,30 +31,32 @@ feature -- Conversion
from_json (j: like to_json): detachable like object from_json (j: like to_json): detachable like object
local local
ucs: detachable STRING_32 l_books: LINKED_LIST [BOOK]
ll: LINKED_LIST [BOOK]
b: detachable BOOK
ja: detachable JSON_ARRAY
i: INTEGER
do do
ucs ?= json.object (j.item (name_key), Void) if
check ucs /= Void end attached {STRING_32} json.object (j.item (name_key), Void) as l_name and
create Result.make (ucs) attached {JSON_ARRAY} j.item (books_key) as l_json_array
ja ?= j.item (books_key) then
check ja /= Void end create Result.make (l_name)
from create l_books.make
i := 1
create ll.make across
l_json_array as it
until until
i > ja.count Result = Void
loop loop
b ?= json.object (ja [i], "BOOK") if attached {BOOK} json.object (it.item, "BOOK") as l_book then
check b /= Void end l_books.extend (l_book)
ll.force (b) else
i := i + 1 Result := Void
-- Failed
end
end
if Result /= Void then
Result.add_books (l_books)
end
end end
check ll /= Void end
Result.add_books (ll)
end end
to_json (o: like object): JSON_OBJECT to_json (o: like object): JSON_OBJECT
@@ -66,11 +69,13 @@ feature -- Conversion
feature {NONE} -- Implementation feature {NONE} -- Implementation
name_key: JSON_STRING name_key: JSON_STRING
-- Collection's name label.
once once
create Result.make_json ("name") create Result.make_json ("name")
end end
books_key: JSON_STRING books_key: JSON_STRING
-- Book list label.
once once
create Result.make_json ("books") create Result.make_json ("books")
end end

View File

@@ -4,7 +4,8 @@ note
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
class JSON_BOOK_CONVERTER class
JSON_BOOK_CONVERTER
inherit inherit
JSON_CONVERTER JSON_CONVERTER
@@ -31,17 +32,14 @@ feature -- Access
feature -- Conversion feature -- Conversion
from_json (j: like to_json): detachable like object from_json (j: like to_json): detachable like object
local
ucs1, ucs2: detachable STRING_32
a: detachable AUTHOR
do do
ucs1 ?= json.object (j.item (title_key), Void) if
check ucs1 /= Void end attached {STRING_32} json.object (j.item (title_key), Void) as l_title and
ucs2 ?= json.object (j.item (isbn_key), Void) attached {STRING_32} json.object (j.item (isbn_key), Void) as l_isbn and
check ucs2 /= Void end attached {AUTHOR} json.object (j.item (author_key), "AUTHOR") as l_author
a ?= json.object (j.item (author_key), "AUTHOR") then
check a /= Void end create Result.make (l_title, l_author, l_isbn)
create Result.make (ucs1, a, ucs2) end
end end
to_json (o: like object): JSON_OBJECT to_json (o: like object): JSON_OBJECT
@@ -55,16 +53,19 @@ feature -- Conversion
feature {NONE} -- Implementation feature {NONE} -- Implementation
title_key: JSON_STRING title_key: JSON_STRING
-- Book's title label.
once once
create Result.make_json ("title") create Result.make_json ("title")
end end
isbn_key: JSON_STRING isbn_key: JSON_STRING
-- Book ISBN label.
once once
create Result.make_json ("isbn") create Result.make_json ("isbn")
end end
author_key: JSON_STRING author_key: JSON_STRING
-- Author label.
once once
create Result.make_json ("author") create Result.make_json ("author")
end end