Skip to content

Commit befe52a

Browse files
committed
Avoid generating json value for Void objects
1 parent 7bad923 commit befe52a

5 files changed

+39
-40
lines changed

library/converter/cj_arrayed_list_json_converter.e

+3-9
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,17 @@ feature -- Conversion
4242

4343
to_json (o: like object): detachable JSON_ARRAY
4444
local
45-
c: ITERATION_CURSOR [detachable ANY]
46-
jv: detachable JSON_VALUE
4745
failed: BOOLEAN
4846
do
4947
create Result.make_array
50-
from
51-
c := o.new_cursor
52-
until
53-
c.after
48+
across
49+
o as c
5450
loop
55-
jv := json.value (c.item)
56-
if jv /= Void then
51+
if attached json.value (c.item) as jv then
5752
Result.add (jv)
5853
else
5954
failed := True
6055
end
61-
c.forth
6256
end
6357
if failed then
6458
Result := Void

library/converter/cj_collection_json_converter.e

+15-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,21 @@ feature -- Conversion
105105
create l_collection.make
106106
l_collection.put (json.value (o.version), version_key)
107107
l_collection.put (json.value (o.href), href_key)
108-
l_collection.put (json.value (o.links), links_key)
109-
l_collection.put (json.value (o.items), items_key)
110-
l_collection.put (json.value (o.queries), queries_key)
111-
l_collection.put (json.value (o.template), template_key)
112-
l_collection.put (json.value (o.error), error_key)
108+
if attached o.links as o_links then
109+
l_collection.put (json.value (o_links), links_key)
110+
end
111+
if attached o.items as o_items then
112+
l_collection.put (json.value (o_items), items_key)
113+
end
114+
if attached o.queries as o_queries then
115+
l_collection.put (json.value (o_queries), queries_key)
116+
end
117+
if attached o.template as o_template then
118+
l_collection.put (json.value (o_template), template_key)
119+
end
120+
if attached o.error as o_error then
121+
l_collection.put (json.value (o_error), error_key)
122+
end
113123

114124
create Result.make
115125
Result.put (l_collection, collection_key)

library/converter/cj_data_json_converter.e

+13-22
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,33 @@ feature -- Access
2727
feature -- Conversion
2828

2929
from_json (j: like to_json): detachable like object
30-
local
31-
l_name: detachable STRING_32
32-
l_prompt: detachable STRING_32
33-
l_value: detachable STRING_32
3430
do
35-
if attached {STRING_32} json_to_object (j.item (name_key), Void) as l_ucs then
36-
l_name := l_ucs
37-
end
38-
if attached {STRING_32} json_to_object (j.item (prompt_key), Void) as l_ucs then
39-
l_prompt := l_ucs
40-
end
41-
if attached {STRING_32} json_to_object (j.item (value_key), Void) as l_ucs then
42-
l_value := l_ucs
43-
end
44-
--|TODO improve this code
45-
--|is there a better way to write this?
46-
--|is a good idea create the Result object at the first line and then set the value
47-
--|if it is attached?
4831
create Result.make
49-
if l_name /= Void then
32+
if attached {STRING_32} json_to_object (j.item (name_key), Void) as l_name then
5033
Result.set_name (l_name)
5134
end
52-
if l_prompt /= Void then
35+
if attached {STRING_32} json_to_object (j.item (prompt_key), Void) as l_prompt then
5336
Result.set_prompt (l_prompt)
5437
end
55-
if l_value /= Void then
38+
if attached {STRING_32} json_to_object (j.item (value_key), Void) as l_value then
5639
Result.set_value (l_value)
5740
end
41+
--|TODO improve this code
42+
--|is there a better way to write this?
43+
--|is a good idea create the Result object at the first line and then set the value
44+
--|if it is attached?
5845
end
5946

6047
to_json (o: like object): JSON_OBJECT
6148
do
6249
create Result.make
63-
Result.put (json.value (o.prompt), prompt_key)
6450
Result.put (json.value (o.name), name_key)
65-
Result.put (json.value (o.value), value_key)
51+
if attached o.prompt as o_prompt then
52+
Result.put (json.value (o_prompt), prompt_key)
53+
end
54+
if attached o.value as o_value then
55+
Result.put (json.value (o_value), value_key)
56+
end
6657
end
6758

6859
feature {NONE} -- Implementation

library/converter/cj_item_json_converter.e

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ feature -- Conversion
6666
do
6767
create Result.make
6868
Result.put (json.value (o.href), href_key)
69-
Result.put (json.value (o.data), data_key)
70-
Result.put (json.value (o.links), links_key)
69+
if attached o.data as o_data then
70+
Result.put (json.value (o_data), data_key)
71+
end
72+
if attached o.links as o_links then
73+
Result.put (json.value (o_links), links_key)
74+
end
7175
end
7276

7377
feature {NONE} -- Implementation

test/test_cj_suite.e

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ feature -- Test routines
223223
local
224224
l_doc : detachable CJ_TEMPLATE
225225
do
226-
l_doc := json_to_cjtempalte ("write_representation.json")
226+
l_doc := json_to_cj_template ("write_representation.json")
227227
assert ("Not Void", l_doc /= Void)
228228
if attached {CJ_TEMPLATE } l_doc as ll_doc then
229229
assert ("Expect 4 elements", ll_doc.data.count = 4)
@@ -234,7 +234,7 @@ feature -- Test routines
234234
feature -- Implementation
235235

236236

237-
json_to_cjtempalte (fn: STRING): detachable CJ_TEMPLATE
237+
json_to_cj_template (fn: STRING): detachable CJ_TEMPLATE
238238
local
239239
dc: CJ_DATA_JSON_CONVERTER
240240
tc: CJ_TEMPLATE_JSON_CONVERTER

0 commit comments

Comments
 (0)