/
json_encode.pl
240 lines (208 loc) · 7.38 KB
/
json_encode.pl
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
%%====================================================================
%%
%% GNU Prolog -- JSON Library
%%
%% @author Sean James Charles <sean at objitsu dot com>
%%
%% I cannot guarantee this won't smoke your system. Use at your own
%% risk as usual, I take no responsibility for any damage you may
%% cause with it. Usual rules apply concerning this sort of thing.
%%
%%
%% This encodes a prolog structure into a JSON string. The structure
%% MUST follow certain rules to allow for the mismatch of JSON and
%% Prolog types i.e. a list of numbers could be a string so we need to
%% use functors to wrap strings and also to wrap objects to.
%%
%% If you follow the rules then you should have no real problems, they
%% are simple and are as follows:
%%
%% RULES:
%%
%% - atoms are converted to "atom" i.e. a string, enclosed in double
%% quotes. No fancy escaping of the content is performed.
%%
%% - to produce a string from a character code list you MUST enclose
%% the string in the functor `str()` or it will be interpreted
%% literally as a list of numbers. Only character codes are valid.
%%
%% - to create an object "{}" with keys and values, you MUST use the
%% functor `obj([])` and the list MUST contain key-value pairs using
%% the '-' functor as is the normal case when using pairs.
%%
%% - string contents are opaque. What you put in the str() functor
%% is passed through with no processing. This means that putting
%% "\n" in the string works as expected as GNU Prolog will have
%% already converted that character for you. Try it.
%%
%%
%% EXAMPLES:
%%
%% json_encode(obj([]),X).
%% X = "{}".
%%
%% json_encode(obj([app-str("GNU Prolog")]),X).
%% X = {"app":"GNU Prolog"}
%%
%% json_encode(obj([w1-str("Hello"), w1-str("World")]),X).
%% X = {"w1":"Hello","w1":"World"}
%%
%% and finally (broken up for clarity I hope), just to show nested
%% objects in action:
%%
%% json_encode(
%% obj(
%% [ status-42
%% , keys-obj(
%% [ k1-1
%% , k2-obj([z-obj([age-49])])
%% ]
%% )
%% ]
%% ),
%% X).
%%
%% X = {"status":42,"keys":{"k1":1,"k2":{"z":{"age":49}}}}
%%
%%
%% All suggestions for more Prolog like code idioms, efficiency and
%% general all round improvements always welcome via GitHub as usual.
%%
%%====================================================================
:- include(mygputils).
%%--------------------------------------------------------------------
%% json_encode/2
%%
%% This will take a suitably formatted compound term and create a
%% character code list (string) that is hopefully acceptable JSON
%% content.
%%
%% This predicate MUST be able to consume the output from
%% json_decode/2 and produce the same structure, albeit not in the
%% same field order. FIELD ORDER IS IRRELEVANT in JSON and we make no
%% effort to preserve it.
%%
%%--------------------------------------------------------------------
%% json_encode(obj(X))/2 ENFORCES the top level {} requirement
%%--------------------------------------------------------------------
json_encode(obj([]), "{}").
json_encode(obj(Body), Out) :-
fmap(json_kv_encode, Body, Terms),
fmap(json_kv_stringify, Terms, Terms2),
join(Terms2, 44, Terms3),
flatten([0'{, Terms3, 0'}], Out).
json_encode(X, _) :-
json_bad_object_error(X).
%%--------------------------------------------------------------------
%% json_encode/3 -- Accumulator predicates for json/2
%%--------------------------------------------------------------------
json_encode([obj(J) | T], Acc, Out) :-
json_encode(J, [], Object1),
flatten([123, Object1, 125], Object2),
json_encode(T, [Object2|Acc], Out).
json_encode([H|T], Acc, Out) :-
json_encode_term(H, Term),
json_encode(T, [Term | Acc], Out).
json_encode([], Acc, Out) :-
reverse(Acc, Out1),
join(Out1, 44, Out2),
flatten(Out2, Out).
%%--------------------------------------------------------------------
%% json_kv_encode/2
%%
%% Having previosuly establish the [K-V]-ness of the obj() content
%% these predicates are used to encode each term into a character code
%% list for the final JSON output.
%%
%% NOTE: that the special list head `K-obj(V)` is passed to the
%% predicate `json_encode/2` all over again as JSON is recursive in
%% nature.
%%--------------------------------------------------------------------
json_kv_encode(K-obj(V), K-Term) :-
atom(K),
json_encode(obj(V), Term).
json_kv_encode(K-V, K-Term) :-
atom(K),
json_encode_term(V, Term).
json_kv_encode([X|_], _, _):-
json_bad_object_error(X).
json_bad_object_error(X) :-
throw(error(json_kv_encode('obj(X), X MUST be [atom-term]. Given:',X))).
%%--------------------------------------------------------------------
%% json_kv_stringify/2
%%
%% Given a K-V pair, the key (assumed to be an atom at this stage) is
%% converted into a character code list and then Out is unified with
%% the JSON pair string "K":V
%%--------------------------------------------------------------------
json_kv_stringify(K-V, Out) :-
atom(K),
atom_codes(K, Key),
flatten(V, Value),
flatten([0'", Key, 0'", 0':, Value], Out). %"
json_kv_stringify(K-_, _) :-
throw(error(json_kv_stringify('K MUST be atom, given: ', K))).
%%--------------------------------------------------------------------
%% json_encode_term/2
%%
%% This will attempt to convert a term into a character code list so
%% it canbe incorporated into the final JSON string.
%%
%% Failure to supply a recognised type or supported functor will cause
%% an exception to be thrown.
%%
%% @see json_decode for supported functors. [ str(), obj()]
%%--------------------------------------------------------------------
json_encode_term(obj(X), Out) :-
json_encode(X, Tmp),
Out = [0'{, Tmp, 0'}].
%% STRING -- str(X)
%%
%% Explicit string, wrap in double quotes
%% TODO: Escape content, caller must ensure this for now.
json_encode_term(str(T), String) :-
fmap(json_escape_chr, T, Escaped),
flatten([34, Escaped, 34], String).
%% NUMBER -- as-is
%%
%% Numbers are unboxed as it is pointless and inefficient.
json_encode_term(X, Out) :-
number(X),
number_codes(X, Out).
%% ATOM -- str(X)!
%%
%% An atom is converted into a string (should REUSE string handling)
%% TODO: Should either wrap into str() or call same string expander.
%% The empty list atom [] is treated as a special case.
json_encode_term([], "[]").
json_encode_term(X, Out) :-
atom(X),
atom_codes(X, Str),
json_encode_term(str(Str), Out).
%% LIST -- []!
%%
%% A Prolog list is taken to be an actual list of stuff. Strings are
%% lists of characters hence the need for the str() functor in the
%% source data.
json_encode_term(X, Out) :-
list(X),
json_encode(X, [], List),
Out = [ 0'[, List, 0'] ].
%% OOPS! We can't handle it, tell somebody...
json_encode_term(X, _) :-
throw(error(json_encode_term(badtype, X))).
%%--------------------------------------------------------------------
%% json_escape_chr/2
%%
%% This is where we map special charcacters into JSON approved escape
%% sequence. \uNNNN is NOT currently handled.
%%--------------------------------------------------------------------
json_escape_chr(34, [0'\\, 0'"]). %% \" %editor fix!"
json_escape_chr(92, [0'\\, 0'\\]). %% \\
json_escape_chr(34, [0'\\, 0'/]). %% \/
json_escape_chr(8, [0'\\, 0'b]). %% \b
json_escape_chr(12, [0'\\, 0'f]). %% \f
json_escape_chr(10, [0'\\, 0'n]). %% \n
json_escape_chr(13, [0'\\, 0'r]). %% \r
json_escape_chr(9 , [0'\\, 0't]). %% \t
json_escape_chr(C, C).