Improve converters.
Replace old syntax with new one and improve implementation.
This commit is contained in:
@@ -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
|
||||
@@ -43,9 +42,10 @@ feature -- Conversion
|
||||
Result.put (json.value (o.name), name_key)
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
name_key: JSON_STRING
|
||||
-- Author's name label.
|
||||
once
|
||||
create Result.make_json ("name")
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -52,19 +50,22 @@ feature -- Conversion
|
||||
Result.put (json.value (o.author), author_key)
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user