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

View File

@@ -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
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
i > ja.count
Result = Void
loop
b ?= json.object (ja [i], "BOOK")
check b /= Void end
ll.force (b)
i := i + 1
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

View File

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