Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 765 lines (664 sloc) 27.623 kB
96534b2 @vinoski replace json.erl with json2.erl to fix issue 50
vinoski authored
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %%% WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED
4 %%% WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED
5 %%%
6 %%% Use module json2.erl instead
7 %%%
8 %%% This module is deprecated. It uses list_to_atom and so could potentially
9 %%% fill the atom table. It also fails to pass its own internal tests due to
10 %%% changes made years ago outside the context of Yaws.
11 %%%
12 %%% Do not report problems with this module, as they will not be fixed. You
13 %%% should instead convert your code to use the json2 module.
14 %%%
15 %%% WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED
16 %%% WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED WARNING DEPRECATED
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
20 %(%% Copyright (c) 2005-2006, A2Z Development USA, Inc. All Rights Reserved.
21 %%%
22 %%% The contents of this file are subject to the Erlang Public License,
23 %%% Version 1.1, (the "License"); you may not use this file except in
24 %%% compliance with the License. You should have received a copy of the
25 %%% Erlang Public License along with this software. If not, it can be
26 %%% retrieved via the world wide web at http://www.erlang.org/.
455578a @vinoski major trailing whitespace cleanup
vinoski authored
27 %%%
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
28 %%% Software distributed under the License is distributed on an "AS IS"
29 %%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
30 %%% the License for the specific language governing rights and limitations
31 %%% under the License.
455578a @vinoski major trailing whitespace cleanup
vinoski authored
32 %%%
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
33 %%% The Initial Developer of the Original Code is A2Z Development USA, Inc.
34 %%% All Rights Reserved.
35
36 -module(json).
96534b2 @vinoski replace json.erl with json2.erl to fix issue 50
vinoski authored
37 -deprecated(module).
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
38 -export([encode/1, decode_string/1, decode/2]).
39 -export([is_obj/1, obj_new/0, obj_fetch/2, obj_find/2, obj_is_key/2]).
40 -export([obj_store/3, obj_from_list/1, obj_fold/3]).
41 -export([test/0]).
42 -author("Jim Larson <jalarson@amazon.com>, Robert Wai-Chi Chu <robchu@amazon.com>").
43 -author("Gaspar Chilingarov <nm@web.am>, Gurgen Tumanyan <barbarian@armkb.com>").
44 -vsn("2").
45
46 %%% JavaScript Object Notation ("JSON", http://www.json.org) is a simple
47 %%% data syntax meant as a lightweight alternative to other representations,
48 %%% such as XML. JSON is natively supported by JavaScript, but many
49 %%% other languages have conversion libraries available.
50 %%%
51 %%% This module translates JSON types into the following Erlang types:
52 %%%
53 %%% JSON Erlang
54 %%% ---- ------
55 %%% number number
56 %%% string string
57 %%% array {array, ElementList}
58 %%% object tagged proplist with string (or atom) keys (i.e. {struct, PropList} )
59 %%% true, false, null atoms 'true', 'false', and 'null'
60 %%%
61 %%% Character Sets: the external representation, and the internal
62 %%% representation of strings, are lists of UTF-16 code units.
63 %%% The encoding of supplementary characters, as well as
64 %%% transcoding to other schemes, such as UTF-8, can be provided
65 %%% by other modules. (See discussion at
66 %%% http://groups.yahoo.com/group/json/message/52)
67 %%%
455578a @vinoski major trailing whitespace cleanup
vinoski authored
68 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
69 %%% UPD by Gaspar: for this moment utf-8 encoding inplemented by default
70 %%% if incoming character list have symbols with codes
71 %%% > 255
455578a @vinoski major trailing whitespace cleanup
vinoski authored
72 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
73 %%%
74 %%% Numbers: Thanks to Erlang's bignums, JSON-encoded integers of any
75 %%% size can be parsed. Conversely, extremely large integers may
76 %%% be JSON-encoded. This may cause problems for interoperability
77 %%% with JSON parsers which can't handle arbitrary-sized integers.
78 %%% Erlang's floats are of fixed precision and limited range, so
79 %%% syntactically valid JSON floating-point numbers could silently
80 %%% lose precision or noisily cause an overflow. However, most
81 %%% other JSON libraries are likely to behave in the same way.
82 %%%
83 %%% Strings: If we represented JSON string data as Erlang binaries,
84 %%% we would have to choose a particular unicode format. Instead,
85 %%% we use lists of UTF-16 code units, which applications may then
86 %%% change to binaries in their application-preferred manner.
87 %%%
88 %%% Arrays: Because of the string decision above, and Erlang's
89 %%% lack of a distinguished string datatype, JSON arrays map
90 %%% to Erlang tuples. Consider utilities like tuple_fold/3
91 %%% to deal with tuples in their native form.
455578a @vinoski major trailing whitespace cleanup
vinoski authored
92 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
93 %%% UPD by Gaspar: array changed to {array, ArrayElementList}
94 %%% ArrayElementList -> list
95 %%% to provide compatibility to xmlrpc module
455578a @vinoski major trailing whitespace cleanup
vinoski authored
96 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
97 %%%
98 %%% Objects: Though not explicitly stated in the JSON "spec",
99 %%% JSON's JavaScript heritage mandates that member names must
100 %%% be unique within an object. The object/tuple ambiguity is
101 %%% not a problem, since the atom 'struct' is not an
102 %%% allowable value. Object keys may be atoms or strings on
103 %%% encoding but are always decoded as strings.
104 %%%
455578a @vinoski major trailing whitespace cleanup
vinoski authored
105 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
106 %%% UPD by Gaspar: struct changed to {array, PropList}
455578a @vinoski major trailing whitespace cleanup
vinoski authored
107 %%% object keys always decoded to atoms to
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
108 %%% provide full compatility with xmlrpc module
455578a @vinoski major trailing whitespace cleanup
vinoski authored
109 %%%######################################################################
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
110 %%%
111
112 %%% ENCODING
113
114 %% Encode an erlang number, string, tuple, or object to JSON syntax, as a
115 %% possibly deep list of UTF-16 code units, throwing a runtime error in the
116 %% case of un-convertible input.
117 %% Note: object keys may be either strings or atoms.
118
119 encode(true) -> "true";
120 encode(false) -> "false";
121 encode(null) -> "null";
5749449 @klacke rpc patch by adam.boz@gmail.com
authored
122 encode(undefined) -> "null";
9cee816 @klacke json binary keys patch by TBBle
authored
123 encode(B) when is_binary(B) -> encode_string(B);
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
124 encode(I) when is_integer(I) -> integer_to_list(I);
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
125 encode(F) when is_float(F) -> float_to_list(F);
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
126 encode(L) when is_list(L) ->
127 case is_string(L) of
128 yes -> encode_string(L);
129 unicode -> encode_string(xmerl_ucs:to_utf8(L));
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
130 no -> encode({array, L})
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
131 end;
132 encode({array, Props}) when is_list(Props) -> encode_array(Props);
133 encode({struct, Props} = T) when is_list(Props) -> encode_object(T);
134 encode(Bad) -> exit({json_encode, {bad_term, Bad}}).
135
136 %% Encode an Erlang string to JSON.
137 %% Accumulate strings in reverse.
138
9cee816 @klacke json binary keys patch by TBBle
authored
139 encode_string(B) when is_binary(B) -> encode_string(binary_to_list(B));
455578a @vinoski major trailing whitespace cleanup
vinoski authored
140 encode_string(S) -> encode_string(S, [$"]).
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
141
455578a @vinoski major trailing whitespace cleanup
vinoski authored
142 encode_string([], Acc) -> lists:reverse([$" | Acc]);
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
143 encode_string([C | Cs], Acc) ->
144 case C of
145 $" -> encode_string(Cs, [$", $\\ | Acc]);
146 % (don't escape solidus on encode)
147 $\\ -> encode_string(Cs, [$\\, $\\ | Acc]);
148 $\b -> encode_string(Cs, [$b, $\\ | Acc]); % note missing \
149 $\f -> encode_string(Cs, [$f, $\\ | Acc]);
150 $\n -> encode_string(Cs, [$n, $\\ | Acc]);
151 $\r -> encode_string(Cs, [$r, $\\ | Acc]);
152 $\t -> encode_string(Cs, [$t, $\\ | Acc]);
153 C when C >= 0, C < $\s ->
154 % Control characters must be unicode-encoded.
155 Hex = lists:flatten(io_lib:format("~4.16.0b", [C])),
9cee816 @klacke json binary keys patch by TBBle
authored
156 encode_string(Cs, lists:reverse(Hex) ++ "u\\" ++ Acc); % "
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
157 C when C =< 16#FFFF -> encode_string(Cs, [C | Acc]);
158 _ -> exit({json_encode, {bad_char, C}})
159 end.
160
161 %% Encode an Erlang object as a JSON object, allowing string or atom keys.
162 %% Note that order is irrelevant in both internal and external object
163 %% representations. Nevertheless, the output will respect the order
164 %% of the input.
165
166 encode_object({struct, _Props} = Obj) ->
167 M = obj_fold(fun({Key, Value}, Acc) ->
168 S = case Key of
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
169 B when is_binary(B) -> encode_string(B);
170 L when is_list(L) ->
171 case is_string(L) of
172 yes -> encode_string(L);
173 unicode -> encode_string(xmerl_ucs:to_utf8(L));
174 no -> exit({json_encode, {bad_key, Key}})
175 end;
176 A when is_atom(A) -> encode_string(atom_to_list(A));
177 _ -> exit({json_encode, {bad_key, Key}})
178 end,
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
179 V = encode(Value),
180 case Acc of
181 [] -> [S, $:, V];
182 _ -> [Acc, $,, S, $:, V]
183 end
184 end, [], Obj),
185 [${, M, $}].
186
187 %% Encode an Erlang tuple as a JSON array.
188 %% Order *is* significant in a JSON array!
189
190 encode_array(T) ->
191 M = lists:foldl(fun(E, Acc) ->
192 V = encode(E),
193 case Acc of
194 [] -> V;
195 _ -> [Acc, $,, V]
196 end
197 end, [], T),
198 [$[, M, $]].
199
200 %%% SCANNING
201 %%%
202 %%% Scanning funs return either:
203 %%% {done, Result, LeftOverChars}
204 %%% if a complete token is recognized, or
205 %%% {more, Continuation}
206 %%% if more input is needed.
207 %%% Result is {ok, Term}, 'eof', or {error, Reason}.
208 %%% Here, the Continuation is a simple Erlang string.
209 %%%
210 %%% Currently, error handling is rather crude - errors are recognized
211 %%% by match failures. EOF is handled only by number scanning, where
212 %%% it can delimit a number, and otherwise causes a match failure.
213 %%%
214 %%% Tokens are one of the following
215 %%% JSON string -> erlang string
216 %%% JSON number -> erlang number
217 %%% true, false, null -> erlang atoms
218 %%% { } [ ] : , -> lcbrace rcbrace lsbrace rsbrace colon comma
219
220 token([]) -> {more, []};
221 token(eof) -> {done, eof, []};
222
223 token("true" ++ Rest) -> {done, {ok, true}, Rest};
224 token("tru") -> {more, "tru"};
225 token("tr") -> {more, "tr"};
226 token("t") -> {more, "t"};
227
228 token("false" ++ Rest) -> {done, {ok, false}, Rest};
229 token("fals") -> {more, "fals"};
230 token("fal") -> {more, "fal"};
231 token("fa") -> {more, "fa"};
232 token("f") -> {more, "f"};
233
234 token("null" ++ Rest) -> {done, {ok, null}, Rest};
235 token("nul") -> {more, "nul"};
236 token("nu") -> {more, "nu"};
237 token("n") -> {more, "n"};
238
239 token([C | Cs] = Input) ->
240 case C of
241 $\s -> token(Cs); % eat whitespace
242 $\t -> token(Cs); % eat whitespace
243 $\n -> token(Cs); % eat whitespace
244 $\r -> token(Cs); % eat whitespace
245 $" -> scan_string(Input);
246 $- -> scan_number(Input);
247 D when D >= $0, D =< $9-> scan_number(Input);
248 ${ -> {done, {ok, lcbrace}, Cs};
249 $} -> {done, {ok, rcbrace}, Cs};
250 $[ -> {done, {ok, lsbrace}, Cs};
251 $] -> {done, {ok, rsbrace}, Cs};
252 $: -> {done, {ok, colon}, Cs};
253 $, -> {done, {ok, comma}, Cs};
254 $/ -> case scan_comment(Cs) of
255 {more, X} -> {more, X};
256 {done, _, Chars} -> token(Chars)
257 end;
258 _ -> {done, {error, {bad_char, C}}, Cs}
259 end.
260
261 scan_string([$" | Cs] = Input) ->
262 scan_string(Cs, [], Input).
263
264 %% Accumulate in reverse order, save original start-of-string for continuation.
265
266 scan_string([], _, X) -> {more, X};
267 scan_string(eof, _, X) -> {done, {error, missing_close_quote}, X};
268 scan_string([$" | Rest], A, _) -> {done, {ok, lists:reverse(A)}, Rest};
269 scan_string([$\\], _, X) -> {more, X};
270 scan_string([$\\, $u, U1, U2, U3, U4 | Rest], A, X) ->
271 scan_string(Rest, [uni_char([U1, U2, U3, U4]) | A], X);
272 scan_string([$\\, $u | _], _, X) -> {more, X};
273 scan_string([$\\, C | Rest], A, X) ->
274 scan_string(Rest, [esc_to_char(C) | A], X);
275 scan_string([C | Rest], A, X) ->
276 scan_string(Rest, [C | A], X).
277
278 %% Given a list of hex characters, convert to the corresponding integer.
279
280 uni_char(HexList) ->
281 erlang:list_to_integer(HexList, 16).
282
283 esc_to_char($") -> $";
284 esc_to_char($/) -> $/;
285 esc_to_char($\\) -> $\\;
286 esc_to_char($b) -> $\b;
287 esc_to_char($f) -> $\f;
288 esc_to_char($n) -> $\n;
289 esc_to_char($r) -> $\r;
290 esc_to_char($t) -> $\t.
291
292 scan_number([]) -> {more, []};
293 scan_number(eof) -> {done, {error, incomplete_number}, []};
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored
294 scan_number([$-, $- | _Ds]) -> {done, {error, invalid_number}, []};
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
295 scan_number([$- | Ds] = Input) ->
296 case scan_number(Ds) of
297 {more, _Cont} -> {more, Input};
298 {done, {ok, N}, CharList} -> {done, {ok, -1 * N}, CharList};
299 {done, Other, Chars} -> {done, Other, Chars}
300 end;
301 scan_number([D | Ds] = Input) when D >= $0, D =< $9 ->
302 scan_number(Ds, D - $0, Input).
303
304 %% Numbers don't have a terminator, so stop at the first non-digit,
305 %% and ask for more if we run out.
306
307 scan_number([], _A, X) -> {more, X};
308 scan_number(eof, A, _X) -> {done, {ok, A}, eof};
309 scan_number([$.], _A, X) -> {more, X};
310 scan_number([$., D | Ds], A, X) when D >= $0, D =< $9 ->
311 scan_fraction([D | Ds], A, X);
312 scan_number([D | Ds], A, X) when A > 0, D >= $0, D =< $9 ->
313 % Note that nonzero numbers can't start with "0".
314 scan_number(Ds, 10 * A + (D - $0), X);
315 scan_number([D | Ds], A, X) when D == $E; D == $e ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
316 scan_exponent_begin(Ds, integer_to_list(A) ++ ".0", X);
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
317 scan_number([D | _] = Ds, A, _X) when D < $0; D > $9 ->
318 {done, {ok, A}, Ds}.
319
320 scan_fraction(Ds, I, X) -> scan_fraction(Ds, [], I, X).
321
322 scan_fraction([], _Fs, _I, X) -> {more, X};
323 scan_fraction(eof, Fs, I, _X) ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
324 R = list_to_float(lists:append([integer_to_list(I), ".", lists:reverse(Fs)])),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
325 {done, {ok, R}, eof};
326 scan_fraction([D | Ds], Fs, I, X) when D >= $0, D =< $9 ->
327 scan_fraction(Ds, [D | Fs], I, X);
328 scan_fraction([D | Ds], Fs, I, X) when D == $E; D == $e ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
329 R = lists:append([integer_to_list(I), ".", lists:reverse(Fs)]),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
330 scan_exponent_begin(Ds, R, X);
331 scan_fraction(Rest, Fs, I, _X) ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
332 R = list_to_float(lists:append([integer_to_list(I), ".", lists:reverse(Fs)])),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
333 {done, {ok, R}, Rest}.
334
335 scan_exponent_begin(Ds, R, X) ->
336 scan_exponent_begin(Ds, [], R, X).
337
338 scan_exponent_begin([], _Es, _R, X) -> {more, X};
339 scan_exponent_begin(eof, _Es, _R, X) -> {done, {error, missing_exponent}, X};
340 scan_exponent_begin([D | Ds], Es, R, X) when D == $-;
341 D == $+;
342 D >= $0, D =< $9 ->
455578a @vinoski major trailing whitespace cleanup
vinoski authored
343 scan_exponent(Ds, [D | Es], R, X).
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
344
345 scan_exponent([], _Es, _R, X) -> {more, X};
346 scan_exponent(eof, Es, R, _X) ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
347 X = list_to_float(lists:append([R, "e", lists:reverse(Es)])),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
348 {done, {ok, X}, eof};
349 scan_exponent([D | Ds], Es, R, X) when D >= $0, D =< $9 ->
350 scan_exponent(Ds, [D | Es], R, X);
351 scan_exponent(Rest, Es, R, _X) ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
352 X = list_to_float(lists:append([R, "e", lists:reverse(Es)])),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
353 {done, {ok, X}, Rest}.
354
355 scan_comment([]) -> {more, "/"};
356 scan_comment(eof) -> {done, eof, []};
357 scan_comment([$/ | Rest]) -> scan_cpp_comment(Rest);
358 scan_comment([$* | Rest]) -> scan_c_comment(Rest).
359
360 %% Ignore up to next CR or LF. If the line ends in CRLF,
361 %% the LF will be treated as separate whitespace, which is
362 %% okay since it will also be ignored.
363
364 scan_cpp_comment([]) -> {more, "//"};
365 scan_cpp_comment(eof) -> {done, eof, []};
366 scan_cpp_comment([$\r | Rest]) -> {done, [], Rest};
367 scan_cpp_comment([$\n | Rest]) -> {done, [], Rest};
368 scan_cpp_comment([_ | Rest]) -> scan_cpp_comment(Rest).
369
370 scan_c_comment([]) -> {more, "/*"};
371 scan_c_comment(eof) -> {done, eof, []};
372 scan_c_comment([$*]) -> {more, "/**"};
373 scan_c_comment([$*, $/ | Rest]) -> {done, [], Rest};
374 scan_c_comment([_ | Rest]) -> scan_c_comment(Rest).
375
376 %%% PARSING
377 %%%
378 %%% The decode function takes a char list as input, but
379 %%% interprets the end of the list as only an end to the available
380 %%% input, and returns a "continuation" requesting more input.
381 %%% When additional characters are available, they, and the
382 %%% continuation, are fed into decode/2. You can use the atom 'eof'
383 %%% as a character to signal a true end to the input stream, and
384 %%% possibly flush out an unfinished number. The decode_string/1
385 %%% function appends 'eof' to its input and calls decode/1.
386 %%%
387 %%% Parsing and scanning errors are handled only by match failures.
388 %%% The external caller must take care to wrap the call in a "catch"
389 %%% or "try" if better error-handling is desired. Eventually parse
390 %%% or scan errors will be returned explicitly with a description,
391 %%% and someday with line numbers too.
392 %%%
393 %%% The parsing code uses a continuation-passing style to allow
394 %%% for the parsing to suspend at any point and be resumed when
395 %%% more input is available.
396 %%% See http://en.wikipedia.org/wiki/Continuation_passing_style
397
398 %% Return the first JSON value decoded from the input string.
399 %% The string must contain at least one complete JSON value.
400
401 decode_string(CharList) ->
402 {done, V, _} = decode([], CharList ++ eof),
403 V.
404
405 %% Attempt to decode a JSON value from the input string
406 %% and continuation, using empty list for the initial continuation.
407 %% Return {done, Result, LeftoverChars} if a value is recognized,
408 %% or {more, Continuation} if more input characters are needed.
409 %% The Result can be {ok, Value}, eof, or {error, Reason}.
410 %% The Continuation is then fed as an argument to decode/2 when
411 %% more input is available.
412 %% Use the atom 'eof' instead of a char list to signal
413 %% a true end to the input, and may flush a final number.
414
415 decode([], CharList) ->
416 decode(first_continuation(), CharList);
417
418 decode(Continuation, CharList) ->
419 {OldChars, Kt} = Continuation,
420 get_token(OldChars ++ CharList, Kt).
421
422 first_continuation() ->
423 {[], fun
424 (eof, Cs) ->
425 {done, eof, Cs};
426 (T, Cs) ->
427 parse_value(T, Cs, fun(V, C2) ->
428 {done, {ok, V}, C2}
429 end)
430 end}.
431
432 %% Continuation Kt must accept (TokenOrEof, Chars)
433
434 get_token(Chars, Kt) ->
435 case token(Chars) of
436 {done, {ok, T}, Rest} -> Kt(T, Rest);
437 {done, eof, Rest} -> Kt(eof, Rest);
438 {done, {error, Reason}, Rest} -> {done, {error, Reason}, Rest};
439 {more, X} -> {more, {X, Kt}}
440 end.
441
442 %% Continuation Kv must accept (Value, Chars)
443
444 parse_value(eof, C, _Kv) -> {done, {error, premature_eof}, C};
445 parse_value(true, C, Kv) -> Kv(true, C);
446 parse_value(false, C, Kv) -> Kv(false, C);
447 parse_value(null, C, Kv) -> Kv(null, C);
448 parse_value(S, C, Kv) when is_list(S) -> Kv(S, C);
449 parse_value(N, C, Kv) when is_number(N) -> Kv(N, C);
450 parse_value(lcbrace, C, Kv) -> parse_object(C, Kv);
451 parse_value(lsbrace, C, Kv) -> parse_array(C, Kv);
452 parse_value(_, C, _Kv) -> {done, {error, syntax_error}, C}.
453
454 %% Continuation Kv must accept (Value, Chars)
455
456 parse_object(Chars, Kv) ->
457 get_token(Chars, fun(T, C2) ->
458 Obj = obj_new(),
459 case T of
460 rcbrace -> Kv(Obj, C2); % empty object
461 _ -> parse_object(Obj, T, C2, Kv) % token must be string
462 end
463 end).
464
465 parse_object(_Obj, eof, C, _Kv) ->
466 {done, {error, premature_eof}, C};
467
468 parse_object(Obj, S, C, Kv) when is_list(S) -> % S is member name
469 get_token(C, fun
470 (colon, C2) ->
471 parse_object2(Obj, S, C2, Kv);
472 (T, C2) ->
473 {done, {error, {expecting_colon, T}}, C2}
474 end);
475
476 parse_object(_Obj, M, C, _Kv) ->
477 {done, {error, {member_name_not_string, M}}, C}.
478
479 parse_object2(Obj, S, C, Kv) ->
480 get_token(C, fun
481 (eof, C2) ->
482 {done, {error, premature_eof}, C2};
483 (T, C2) ->
484 parse_value(T, C2, fun(V, C3) -> % V is member value
485 Obj2 = obj_store(S, V, Obj),
486 get_token(C3, fun
487 (rcbrace, C4) -> % "}" end of object
488 {struct, PropList1} = Obj2,
489 Kv({struct, lists:reverse(PropList1)}, C4);
490 (comma, C4) -> % "," another member follows
491 get_token(C4, fun(T3, C5) ->
492 parse_object(Obj2, T3, C5, Kv)
493 end);
494 (eof, C4) ->
495 {done, {error, premature_eof}, C4};
496 (T2, C4) ->
497 {done, {error, {expecting_comma_or_curly, T2}}, C4}
498 end)
499 end)
500 end).
501
502 %% Continuation Kv must accept (Value, Chars)
503
504 parse_array(C, Kv) ->
505 get_token(C, fun
506 (eof, C2) -> {done, {error, premature_eof}, C2};
455578a @vinoski major trailing whitespace cleanup
vinoski authored
507 (rsbrace, C2) -> Kv({array, []}, C2); % empty array
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
508 (T, C2) -> parse_array([], T, C2, Kv)
509 end).
510
511 parse_array(E, T, C, Kv) ->
512 parse_value(T, C, fun(V, C2) ->
513 E2 = [V | E],
514 get_token(C2, fun
515 (rsbrace, C3) -> % "]" end of array
516 Kv({array, lists:reverse(E2)}, C3);
517
518 (comma, C3) -> % "," another value follows
519 get_token(C3, fun(T3, C4) ->
520 parse_array(E2, T3, C4, Kv)
521 end);
522 (eof, C3) ->
523 {done, {error, premature_eof}, C3};
524 (T2, C3) ->
525 {done, {error, {expecting_comma_or_close_array, T2}}, C3}
526 end)
527 end).
528
529 %%% OBJECTS
530 %%%
531 %%% We'll use tagged property lists as the internal representation
532 %%% of JSON objects. Unordered lists perform worse than trees for
533 %%% lookup and modification of members, but we expect objects to be
534 %%% have only a few members. Lists also print better.
535
536 %% Is this a proper JSON object representation?
537
538 is_obj({struct, Props}) when is_list(Props) ->
539 lists:all(fun
540 ({Member, _Value}) when is_atom(Member); is_list(Member) -> true;
541 (_) -> false
542 end, Props);
543
544 is_obj(_) ->
545 false.
546
547 %% Create a new, empty object.
548
549 obj_new() ->
550 {struct, []}.
551
552 %% Fetch an object member's value, expecting it to be in the object.
553 %% Return value, runtime error if no member found with that name.
554
555 obj_fetch(Key, {struct, Props}) when is_list(Props) ->
556 case proplists:get_value(Key, Props) of
557 undefined ->
558 exit({struct_no_key, Key});
559 Value ->
560 Value
561 end.
455578a @vinoski major trailing whitespace cleanup
vinoski authored
562
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
563 %% Fetch an object member's value, or indicate that there is no such member.
564 %% Return {ok, Value} or 'error'.
565
566 obj_find(Key, {struct, Props}) when is_list(Props) ->
567 case proplists:get_value(Key, Props) of
568 undefined ->
569 error;
570 Value ->
571 {ok, Value}
572 end.
573
574 obj_is_key(Key, {struct, Props}) ->
575 proplists:is_defined(Key, Props).
576
577 %% Store a new member in an object. Returns a new object.
578
579 obj_store(KeyList, Value, {struct, Props}) when is_list(Props) ->
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
580 Key = try list_to_atom(KeyList)
581 catch error:badarg -> KeyList
582 end,
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
583 {struct, [{Key, Value} | proplists:delete(Key, Props)]}.
584
585 %% Create an object from a list of Key/Value pairs.
586
587 obj_from_list(Props) ->
588 Obj = {struct, Props},
589 case is_obj(Obj) of
590 true -> Obj;
591 false -> exit(json_bad_object)
592 end.
593
594 %% Fold Fun across object, with initial accumulator Acc.
595 %% Fun should take (Value, Acc) as arguments and return Acc.
596
597 obj_fold(Fun, Acc, {struct, Props}) ->
598 lists:foldl(Fun, Acc, Props).
599
600 is_string([]) -> yes;
601 is_string(List) -> is_string(List, non_unicode).
602
661aaf9 @vinoski fixes for json2.erl and json.erl (Nico Kruber)
vinoski authored
603 is_string([C|Rest], non_unicode) when is_integer(C), C >= 0, C =< 255 -> is_string(Rest, non_unicode);
604 is_string([C|Rest], _) when is_integer(C), C>= 0, C =< 65000 -> is_string(Rest, unicode);
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
605 is_string([], non_unicode) -> yes;
606 is_string([], unicode) -> unicode;
607 is_string(_, _) -> no.
608
609
610 %%% TESTING
611 %%%
612 %%% We can't expect to round-trip from JSON -> Erlang -> JSON,
613 %%% due to the degrees of freedom in the JSON syntax: whitespace,
614 %%% and ordering of object members. We can, however, expect to
615 %%% round-trip from Erlang -> JSON -> Erlang, so the JSON parsing
616 %%% tests will in fact test the Erlang equivalence of the
617 %%% JSON -> Erlang -> JSON -> Erlang coding chain.
618
619 %% Test driver. Return 'ok' or {failed, Failures}.
620
621 test() ->
622 E2Js = e2j_test_vec(),
623 Failures = lists:foldl(fun({E, J}, Fs) ->
624 case (catch test_e2j(E, J)) of
625 ok ->
626 case (catch round_trip(E)) of
627 ok ->
628 case (catch round_trip_one_char(E)) of
629 ok -> Fs;
630 Reason -> [{round_trip_one_char, E, Reason} | Fs]
631 end;
632 Reason ->
633 [{round_trip, E, Reason} | Fs]
634 end;
635 Reason ->
636 [{erlang_to_json, E, J, Reason} | Fs]
637 end;
638 (end_of_tests, Fs) -> Fs end, [], E2Js),
639 case Failures of
640 [] -> ok;
641 _ -> {failed, Failures}
642 end.
643
644 %% Test for conversion from Erlang to JSON. Note that unequal strings
645 %% may represent equal JSON data, due to discretionary whitespace,
646 %% object member order, trailing zeroes in floating point, etc.
647 %% Legitimate changes to the encoding routines may require tweaks to
648 %% the reference JSON strings in e2j_test_vec().
649
650 test_e2j(E, J) ->
651 J2 = lists:flatten(encode(E)),
652 J = J2, % raises error if unequal
653 ok.
654
655 %% Test that Erlang -> JSON -> Erlang round-trip yields equivalent term.
656
657 round_trip(E) ->
658 J2 = lists:flatten(encode(E)),
659 {ok, E2} = decode_string(J2),
660 true = equiv(E, E2), % raises error if false
661 ok.
662
663 %% Round-trip with one character at a time to test all continuations.
664
665 round_trip_one_char(E) ->
666 J = lists:flatten(encode(E)),
667 {done, {ok, E2}, _} = lists:foldl(fun(C, Ret) ->
668 case Ret of
669 {done, _, _} -> Ret;
670 {more, Cont} -> decode(Cont, [C])
671 end
672 end, {more, first_continuation()}, J ++ [eof]),
673 true = equiv(E, E2), % raises error if false
674 ok.
675
676 %% Test for equivalence of Erlang terms.
677 %% Due to arbitrary order of construction, equivalent objects might
678 %% compare unequal as erlang terms, so we need to carefully recurse
679 %% through aggregates (tuples and objects).
680
681 equiv({struct, Props1}, {struct, Props2}) ->
682 equiv_object(Props1, Props2);
683 equiv(T1, T2) when is_tuple(T1), is_tuple(T2) ->
684 equiv_tuple(T1, T2);
685 equiv(N1, N2) when is_number(N1), is_number(N2) -> N1 == N2;
686 equiv(S1, S2) when is_list(S1), is_list(S2) -> S1 == S2;
687 equiv(true, true) -> true;
688 equiv(false, false) -> true;
689 equiv(null, null) -> true.
690
691 %% Object representation and traversal order is unknown.
692 %% Use the sledgehammer and sort property lists.
693
694 equiv_object(Props1, Props2) ->
695 L1 = lists:keysort(1, Props1),
696 L2 = lists:keysort(1, Props2),
697 Pairs = lists:zip(L1, L2),
698 true = lists:all(fun({{K1, V1}, {K2, V2}}) ->
699 equiv(K1, K2) and equiv(V1, V2)
700 end, Pairs).
701
702 %% Recursively compare tuple elements for equivalence.
703
704 equiv_tuple({}, {}) ->
705 true;
706 equiv_tuple(T1, T2) when size(T1) == size(T2) ->
707 S = size(T1),
708 lists:all(fun(I) ->
709 equiv(element(I, T1), element(I, T2))
710 end, lists:seq(1, S)).
711
712 e2j_test_vec() -> [
713 {1, "1"},
714 {3.1416, "3.14160"}, % text representation may truncate, trail zeroes
715 {-1, "-1"},
716 {-3.1416, "-3.14160"},
717 {12.0e10, "1.20000e+11"},
718 {1.234E+10, "1.23400e+10"},
719 {-1.234E-10, "-1.23400e-10"},
720 {"foo", "\"foo\""},
721 {"foo" ++ [500] ++ "bar", [$", $f, $o, $o, 500, $b, $a, $r, $"]},
722 {"foo" ++ [5] ++ "bar", "\"foo\\u0005bar\""},
723 {"", "\"\""},
724 {[], "\"\""},
725 {"\n\n\n", "\"\\n\\n\\n\""},
726 {obj_new(), "{}"},
727 {obj_from_list([{"foo", "bar"}]), "{\"foo\":\"bar\"}"},
728 {obj_from_list([{"foo", "bar"}, {"baz", 123}]),
729 "{\"foo\":\"bar\",\"baz\":123}"},
730 {{}, "[]"},
731 {{{}}, "[[]]"},
732 {{1, "foo"}, "[1,\"foo\"]"},
733
734 % json array in a json object
735 {obj_from_list([{"foo", {123}}]),
736 "{\"foo\":[123]}"},
737
738 % json object in a json object
739 {obj_from_list([{"foo", obj_from_list([{"bar", true}])}]),
740 "{\"foo\":{\"bar\":true}}"},
741
742 % fold evaluation order
743 {obj_from_list([{"foo", {}},
744 {"bar", obj_from_list([{"baz", true}])},
745 {"alice", "bob"}]),
746 "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"},
747
748 % json object in a json array
749 {{-123, "foo", obj_from_list([{"bar", {}}]), null},
750 "[-123,\"foo\",{\"bar\":[]},null]"},
751
752 end_of_tests
753 ].
754
755 %%% TODO:
756 %%%
757 %%% Measure the overhead of the CPS-based parser by writing a conventional
758 %%% scanner-parser that expects all input to be available.
759 %%%
760 %%% JSON has dropped comments - disable their parsing.
761 %%%
762 %%% Allow a compile-time option to decode object member names as atoms,
763 %%% to reduce the internal representation overheads when communicating
764 %%% with trusted peers.
Something went wrong with that request. Please try again.