2010-05-25 21:26:55 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2010-05-25 21:48:36 -07:00
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
2010-05-25 21:26:55 -07:00
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
|
|
|
|
2010-05-18 13:01:49 -07:00
|
|
|
-module(jsx).
|
2010-05-25 22:23:06 -07:00
|
|
|
-author("alisdairsullivan@yahoo.ca").
|
2010-05-18 13:01:49 -07:00
|
|
|
|
2010-06-20 17:00:08 -07:00
|
|
|
%% the core parser api
|
2010-06-28 18:14:14 -07:00
|
|
|
-export([parser/0, parser/1]).
|
2010-08-03 14:16:56 -07:00
|
|
|
-export([term_to_json/1, term_to_json/2]).
|
|
|
|
-export([json_to_term/1, json_to_term/2]).
|
|
|
|
-export([is_json/1, is_json/2]).
|
|
|
|
-export([format/1, format/2]).
|
2010-08-12 20:08:39 -07:00
|
|
|
-export([eventify/1]).
|
2010-06-09 00:57:15 -07:00
|
|
|
|
2010-08-05 21:05:08 -07:00
|
|
|
|
2010-06-20 17:00:08 -07:00
|
|
|
%% types for function specifications
|
2010-07-26 18:04:23 -07:00
|
|
|
-include("./include/jsx_types.hrl").
|
2010-06-21 23:06:55 -07:00
|
|
|
|
2010-08-05 21:05:08 -07:00
|
|
|
|
2010-06-26 07:27:38 -07:00
|
|
|
%% opts record
|
|
|
|
-record(opts, {
|
|
|
|
comments = false,
|
|
|
|
escaped_unicode = codepoint,
|
|
|
|
multi_term = false,
|
|
|
|
encoding = auto
|
|
|
|
}).
|
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2010-08-10 12:31:05 -07:00
|
|
|
|
2010-06-09 00:57:15 -07:00
|
|
|
-spec parser() -> jsx_parser().
|
2010-06-09 06:21:03 -07:00
|
|
|
-spec parser(Opts::jsx_opts()) -> jsx_parser().
|
2010-05-29 18:28:14 -07:00
|
|
|
|
2010-06-09 00:57:15 -07:00
|
|
|
parser() ->
|
|
|
|
parser([]).
|
2010-05-31 02:35:49 -07:00
|
|
|
|
2010-06-11 21:57:42 -07:00
|
|
|
parser(OptsList) ->
|
2010-06-01 01:03:28 -07:00
|
|
|
F = case proplists:get_value(encoding, OptsList, auto) of
|
2010-06-11 21:57:42 -07:00
|
|
|
utf8 -> fun jsx_utf8:parse/2
|
|
|
|
; utf16 -> fun jsx_utf16:parse/2
|
|
|
|
; utf32 -> fun jsx_utf32:parse/2
|
|
|
|
; {utf16, little} -> fun jsx_utf16le:parse/2
|
|
|
|
; {utf32, little} -> fun jsx_utf32le:parse/2
|
|
|
|
; auto -> fun detect_encoding/2
|
2010-06-01 01:03:28 -07:00
|
|
|
end,
|
2010-08-03 14:16:56 -07:00
|
|
|
case parse_opts(OptsList) of
|
|
|
|
{error, badopt} -> {error, badopt}
|
|
|
|
; Opts -> fun(Stream) -> F(Stream, Opts) end
|
|
|
|
end.
|
|
|
|
|
2010-05-31 02:35:49 -07:00
|
|
|
|
2010-08-11 22:33:50 -07:00
|
|
|
-spec term_to_json(JSON::eep0018()) -> binary().
|
|
|
|
-spec term_to_json(JSON::eep0018(), Opts::encoder_opts()) -> binary().
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
term_to_json(JSON) ->
|
|
|
|
term_to_json(JSON, []).
|
|
|
|
|
|
|
|
term_to_json(JSON, Opts) ->
|
2010-08-06 17:58:10 -07:00
|
|
|
jsx_eep0018:term_to_json(JSON, Opts).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
|
2010-08-11 23:11:12 -07:00
|
|
|
-spec json_to_term(JSON::binary()) -> eep0018().
|
|
|
|
-spec json_to_term(JSON::binary(), Opts::decoder_opts()) -> eep0018().
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
json_to_term(JSON) ->
|
|
|
|
json_to_term(JSON, []).
|
|
|
|
|
|
|
|
json_to_term(JSON, Opts) ->
|
2010-08-03 20:29:49 -07:00
|
|
|
jsx_eep0018:json_to_term(JSON, Opts).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
-spec is_json(JSON::binary()) -> true | false.
|
|
|
|
-spec is_json(JSON::binary(), Opts::verify_opts()) -> true | false.
|
|
|
|
|
|
|
|
is_json(JSON) ->
|
|
|
|
is_json(JSON, []).
|
|
|
|
|
|
|
|
is_json(JSON, Opts) ->
|
2010-08-03 20:29:49 -07:00
|
|
|
jsx_verify:is_json(JSON, Opts).
|
2010-08-03 14:16:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
-spec format(JSON::binary()) -> binary() | iolist().
|
|
|
|
-spec format(JSON::binary(), Opts::format_opts()) -> binary() | iolist().
|
|
|
|
|
|
|
|
format(JSON) ->
|
|
|
|
format(JSON, []).
|
|
|
|
|
|
|
|
format(JSON, Opts) ->
|
2010-08-12 20:08:39 -07:00
|
|
|
jsx_format:format(JSON, Opts).
|
|
|
|
|
|
|
|
|
|
|
|
-spec eventify(List::list()) -> jsx_parser_result().
|
|
|
|
|
2010-08-12 20:55:07 -07:00
|
|
|
%% fake the jsx api for any list, useful if you want to serialize a structure to
|
|
|
|
%% json using the pretty printer, or verify a sequence could be valid json
|
2010-08-12 20:08:39 -07:00
|
|
|
|
|
|
|
eventify([]) ->
|
2010-08-12 20:55:07 -07:00
|
|
|
fun() -> {incomplete, fun(List) when is_list(List) -> eventify(List); (_) -> erlang:error(badarg) end} end;
|
2010-08-12 20:08:39 -07:00
|
|
|
eventify([Next|Rest]) ->
|
|
|
|
fun() -> {event, Next, eventify(Rest)} end.
|
2010-08-03 14:16:56 -07:00
|
|
|
|
2010-06-01 01:03:28 -07:00
|
|
|
|
2010-08-03 14:16:56 -07:00
|
|
|
%% ----------------------------------------------------------------------------
|
|
|
|
%% internal functions
|
|
|
|
%% ----------------------------------------------------------------------------
|
2010-06-09 00:57:15 -07:00
|
|
|
|
2010-06-20 17:00:08 -07:00
|
|
|
%% option parsing
|
|
|
|
|
|
|
|
%% converts a proplist into a tuple
|
2010-05-31 02:35:49 -07:00
|
|
|
parse_opts(Opts) ->
|
2010-06-26 07:27:38 -07:00
|
|
|
parse_opts(Opts, #opts{}).
|
2010-05-31 02:35:49 -07:00
|
|
|
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts;
|
2010-06-26 07:27:38 -07:00
|
|
|
parse_opts([{comments, Value}|Rest], Opts) ->
|
2010-05-31 02:35:49 -07:00
|
|
|
true = lists:member(Value, [true, false]),
|
2010-06-26 07:27:38 -07:00
|
|
|
parse_opts(Rest, Opts#opts{comments = Value});
|
|
|
|
parse_opts([{escaped_unicode, Value}|Rest], Opts) ->
|
2010-05-31 02:35:49 -07:00
|
|
|
true = lists:member(Value, [ascii, codepoint, none]),
|
2010-06-26 07:27:38 -07:00
|
|
|
parse_opts(Rest, Opts#opts{escaped_unicode = Value});
|
|
|
|
parse_opts([{multi_term, Value}|Rest], Opts) ->
|
2010-06-24 15:55:08 -07:00
|
|
|
true = lists:member(Value, [true, false]),
|
2010-06-26 07:27:38 -07:00
|
|
|
parse_opts(Rest, Opts#opts{multi_term = Value});
|
2010-06-11 21:57:42 -07:00
|
|
|
parse_opts([{encoding, _}|Rest], Opts) ->
|
2010-08-03 14:16:56 -07:00
|
|
|
parse_opts(Rest, Opts);
|
|
|
|
parse_opts(_, _) ->
|
|
|
|
{error, badopt}.
|
2010-05-29 18:28:14 -07:00
|
|
|
|
2010-06-20 17:00:08 -07:00
|
|
|
|
|
|
|
%% encoding detection
|
2010-06-01 01:03:28 -07:00
|
|
|
%% first check to see if there's a bom, if not, use the rfc4627 method for determining
|
|
|
|
%% encoding. this function makes some assumptions about the validity of the stream
|
2010-06-11 21:57:42 -07:00
|
|
|
%% which may delay failure later than if an encoding is explicitly provided
|
2010-06-01 01:03:28 -07:00
|
|
|
|
|
|
|
%% utf8 bom detection
|
2010-06-23 23:36:34 -07:00
|
|
|
detect_encoding(<<16#ef, 16#bb, 16#bf, Rest/binary>>, Opts) -> jsx_utf8:parse(Rest, Opts);
|
|
|
|
%% utf32-little bom detection (this has to come before utf16-little or it'll match that)
|
|
|
|
detect_encoding(<<16#ff, 16#fe, 0, 0, Rest/binary>>, Opts) -> jsx_utf32le:parse(Rest, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf16-big bom detection
|
2010-06-23 23:36:34 -07:00
|
|
|
detect_encoding(<<16#fe, 16#ff, Rest/binary>>, Opts) -> jsx_utf16:parse(Rest, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf16-little bom detection
|
2010-06-23 23:36:34 -07:00
|
|
|
detect_encoding(<<16#ff, 16#fe, Rest/binary>>, Opts) -> jsx_utf16le:parse(Rest, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf32-big bom detection
|
2010-06-23 23:36:34 -07:00
|
|
|
detect_encoding(<<0, 0, 16#fe, 16#ff, Rest/binary>>, Opts) -> jsx_utf32:parse(Rest, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
|
|
|
|
%% utf32-little null order detection
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<X, 0, 0, 0, _Rest/binary>> = JSON, Opts) when X =/= 0 ->
|
|
|
|
jsx_utf32le:parse(JSON, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf16-big null order detection
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<0, X, 0, Y, _Rest/binary>> = JSON, Opts) when X =/= 0, Y =/= 0 ->
|
|
|
|
jsx_utf16:parse(JSON, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf16-little null order detection
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<X, 0, Y, 0, _Rest/binary>> = JSON, Opts) when X =/= 0, Y =/= 0 ->
|
|
|
|
jsx_utf16le:parse(JSON, Opts);
|
2010-06-01 01:03:28 -07:00
|
|
|
%% utf32-big null order detection
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<0, 0, 0, X, _Rest/binary>> = JSON, Opts) when X =/= 0 ->
|
|
|
|
jsx_utf32:parse(JSON, Opts);
|
2010-06-01 23:33:56 -07:00
|
|
|
%% utf8 null order detection
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<X, Y, _Rest/binary>> = JSON, Opts) when X =/= 0, Y =/= 0 ->
|
|
|
|
jsx_utf8:parse(JSON, Opts);
|
2010-06-01 23:33:56 -07:00
|
|
|
|
|
|
|
%% a problem, to autodetect naked single digits' encoding, there is not enough data
|
|
|
|
%% to conclusively determine the encoding correctly. below is an attempt to solve
|
|
|
|
%% the problem
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<X>>, Opts) when X =/= 0 ->
|
2010-07-27 00:05:15 -07:00
|
|
|
{incomplete,
|
|
|
|
fun(end_stream) ->
|
|
|
|
try
|
|
|
|
{incomplete, Next} = jsx_utf8:parse(<<X>>, Opts),
|
|
|
|
Next(end_stream)
|
|
|
|
catch error:function_clause -> {error, badjson}
|
|
|
|
end
|
|
|
|
; (Stream) -> detect_encoding(<<X, Stream/binary>>, Opts)
|
2010-06-21 23:06:55 -07:00
|
|
|
end
|
|
|
|
};
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<0, X>>, Opts) when X =/= 0 ->
|
2010-07-27 00:05:15 -07:00
|
|
|
{incomplete,
|
|
|
|
fun(end_stream) ->
|
|
|
|
try
|
|
|
|
{incomplete, Next} = jsx_utf16:parse(<<0, X>>, Opts),
|
|
|
|
Next(end_stream)
|
|
|
|
catch error:function_clause -> {error, badjson}
|
|
|
|
end
|
|
|
|
; (Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts)
|
2010-06-21 23:06:55 -07:00
|
|
|
end
|
|
|
|
};
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(<<X, 0>>, Opts) when X =/= 0 ->
|
2010-07-27 00:05:15 -07:00
|
|
|
{incomplete,
|
|
|
|
fun(end_stream) ->
|
|
|
|
try
|
|
|
|
{incomplete, Next} = jsx_utf16le:parse(<<X, 0>>, Opts),
|
|
|
|
Next(end_stream)
|
|
|
|
catch error:function_clause -> {error, badjson}
|
|
|
|
end
|
|
|
|
; (Stream) -> detect_encoding(<<X, 0, Stream/binary>>, Opts)
|
2010-06-21 23:06:55 -07:00
|
|
|
end
|
|
|
|
};
|
2010-06-01 01:03:28 -07:00
|
|
|
|
|
|
|
%% not enough input, request more
|
2010-06-11 21:57:42 -07:00
|
|
|
detect_encoding(Bin, Opts) ->
|
2010-07-27 00:05:15 -07:00
|
|
|
{incomplete,
|
|
|
|
fun(end_stream) -> {error, badjson}
|
|
|
|
; (Stream) -> detect_encoding(<<Bin/binary, Stream/binary>>, Opts)
|
|
|
|
end
|
2010-08-10 12:31:05 -07:00
|
|
|
}.
|