-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_helper.e
52 lines (43 loc) · 980 Bytes
/
json_helper.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
note
description: "Summary description for {JSON_HELPER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
JSON_HELPER
inherit
ANY
create
default_create
feature
encode(data: HASH_TABLE[ANY, HASHABLE]): STRING
local
serializer: JSON_HASH_TABLE_CONVERTER
do
create serializer.make
create Result.make_empty
if attached serializer.to_json(data) as converted then
Result := converted.representation
else
Result := "{%"status%": %"error%", %"msg%": %"JSON serialization error%"}"
end
ensure
Result /= Void
end
decode(data: STRING): HASH_TABLE[ANY, HASHABLE]
local
serializer: JSON_HASH_TABLE_CONVERTER
tmp: JSON_OBJECT
do
create serializer.make
create tmp.make
tmp.put_string ("str", data)
if attached serializer.from_json (tmp) as converted then
Result := converted
else
create Result.make (2)
Result["status"] := "error"
Result["msg"] := "Deserialization error"
end
end
end