Skip to content

Commit 052860b

Browse files
ConaclosConaclos
authored andcommitted
Improve converters.
Replace old syntax with new one and improve implementation.
1 parent c5e1b1e commit 052860b

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

test/autotest/test_suite/json_author_converter.e

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_AUTHOR_CONVERTER
7+
class
8+
JSON_AUTHOR_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -29,12 +30,10 @@ feature -- Access
2930
feature -- Conversion
3031

3132
from_json (j: like to_json): detachable like object
32-
local
33-
ucs: detachable STRING_32
3433
do
35-
ucs ?= json.object (j.item (name_key), Void)
36-
check ucs /= Void end
37-
create Result.make (ucs)
34+
if attached {STRING_32} json.object (j.item (name_key), Void) as l_name then
35+
create Result.make (l_name)
36+
end
3837
end
3938

4039
to_json (o: like object): JSON_OBJECT
@@ -43,9 +42,10 @@ feature -- Conversion
4342
Result.put (json.value (o.name), name_key)
4443
end
4544

46-
feature {NONE} -- Implementation
45+
feature {NONE} -- Implementation
4746

4847
name_key: JSON_STRING
48+
-- Author's name label.
4949
once
5050
create Result.make_json ("name")
5151
end

test/autotest/test_suite/json_book_collection_converter.e

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_BOOK_COLLECTION_CONVERTER
7+
class
8+
JSON_BOOK_COLLECTION_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -30,30 +31,32 @@ feature -- Conversion
3031

3132
from_json (j: like to_json): detachable like object
3233
local
33-
ucs: detachable STRING_32
34-
ll: LINKED_LIST [BOOK]
35-
b: detachable BOOK
36-
ja: detachable JSON_ARRAY
37-
i: INTEGER
34+
l_books: LINKED_LIST [BOOK]
3835
do
39-
ucs ?= json.object (j.item (name_key), Void)
40-
check ucs /= Void end
41-
create Result.make (ucs)
42-
ja ?= j.item (books_key)
43-
check ja /= Void end
44-
from
45-
i := 1
46-
create ll.make
47-
until
48-
i > ja.count
49-
loop
50-
b ?= json.object (ja [i], "BOOK")
51-
check b /= Void end
52-
ll.force (b)
53-
i := i + 1
36+
if
37+
attached {STRING_32} json.object (j.item (name_key), Void) as l_name and
38+
attached {JSON_ARRAY} j.item (books_key) as l_json_array
39+
then
40+
create Result.make (l_name)
41+
create l_books.make
42+
43+
across
44+
l_json_array as it
45+
until
46+
Result = Void
47+
loop
48+
if attached {BOOK} json.object (it.item, "BOOK") as l_book then
49+
l_books.extend (l_book)
50+
else
51+
Result := Void
52+
-- Failed
53+
end
54+
end
55+
56+
if Result /= Void then
57+
Result.add_books (l_books)
58+
end
5459
end
55-
check ll /= Void end
56-
Result.add_books (ll)
5760
end
5861

5962
to_json (o: like object): JSON_OBJECT
@@ -66,11 +69,13 @@ feature -- Conversion
6669
feature {NONE} -- Implementation
6770

6871
name_key: JSON_STRING
72+
-- Collection's name label.
6973
once
7074
create Result.make_json ("name")
7175
end
7276

7377
books_key: JSON_STRING
78+
-- Book list label.
7479
once
7580
create Result.make_json ("books")
7681
end

test/autotest/test_suite/json_book_converter.e

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ note
44
date: "$Date$"
55
revision: "$Revision$"
66

7-
class JSON_BOOK_CONVERTER
7+
class
8+
JSON_BOOK_CONVERTER
89

910
inherit
1011
JSON_CONVERTER
@@ -31,17 +32,14 @@ feature -- Access
3132
feature -- Conversion
3233

3334
from_json (j: like to_json): detachable like object
34-
local
35-
ucs1, ucs2: detachable STRING_32
36-
a: detachable AUTHOR
3735
do
38-
ucs1 ?= json.object (j.item (title_key), Void)
39-
check ucs1 /= Void end
40-
ucs2 ?= json.object (j.item (isbn_key), Void)
41-
check ucs2 /= Void end
42-
a ?= json.object (j.item (author_key), "AUTHOR")
43-
check a /= Void end
44-
create Result.make (ucs1, a, ucs2)
36+
if
37+
attached {STRING_32} json.object (j.item (title_key), Void) as l_title and
38+
attached {STRING_32} json.object (j.item (isbn_key), Void) as l_isbn and
39+
attached {AUTHOR} json.object (j.item (author_key), "AUTHOR") as l_author
40+
then
41+
create Result.make (l_title, l_author, l_isbn)
42+
end
4543
end
4644

4745
to_json (o: like object): JSON_OBJECT
@@ -52,19 +50,22 @@ feature -- Conversion
5250
Result.put (json.value (o.author), author_key)
5351
end
5452

55-
feature {NONE} -- Implementation
53+
feature {NONE} -- Implementation
5654

5755
title_key: JSON_STRING
56+
-- Book's title label.
5857
once
5958
create Result.make_json ("title")
6059
end
6160

6261
isbn_key: JSON_STRING
62+
-- Book ISBN label.
6363
once
6464
create Result.make_json ("isbn")
6565
end
6666

6767
author_key: JSON_STRING
68+
-- Author label.
6869
once
6970
create Result.make_json ("author")
7071
end

0 commit comments

Comments
 (0)