2010-05-18 13:01:49 -07:00
|
|
|
-module(jsx).
|
|
|
|
|
2010-05-25 13:31:41 -07:00
|
|
|
-export([decoder/0, decoder/2, tail_clean/1]).
|
2010-05-18 13:01:49 -07:00
|
|
|
|
2010-05-18 13:16:47 -07:00
|
|
|
-include("jsx_common.hrl").
|
2010-05-18 13:01:49 -07:00
|
|
|
|
2010-05-24 16:41:03 -07:00
|
|
|
|
2010-05-18 13:01:49 -07:00
|
|
|
decoder() ->
|
2010-05-25 13:32:54 -07:00
|
|
|
decoder({none, []}, []).
|
2010-05-18 13:01:49 -07:00
|
|
|
|
2010-05-23 22:52:15 -07:00
|
|
|
decoder(Callbacks, OptsList) when is_list(OptsList) ->
|
|
|
|
Opts = parse_opts(OptsList),
|
|
|
|
decoder(Callbacks, Opts);
|
|
|
|
decoder(Callbacks, Opts) ->
|
2010-05-23 22:40:35 -07:00
|
|
|
case Opts#opts.encoding of
|
2010-05-23 22:52:15 -07:00
|
|
|
utf8 ->
|
2010-05-25 13:32:54 -07:00
|
|
|
fun(Stream) -> jsx_decoder:start(Stream, [], Callbacks, Opts) end
|
2010-05-18 13:01:49 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
parse_opts(Opts) ->
|
|
|
|
parse_opts(Opts, #opts{}).
|
|
|
|
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts;
|
|
|
|
parse_opts([{comments, Value}|Rest], Opts) ->
|
|
|
|
true = lists:member(Value, [true, false]),
|
|
|
|
parse_opts(Rest, Opts#opts{comments = Value});
|
|
|
|
parse_opts([{escaped_unicode, Value}|Rest], Opts) ->
|
|
|
|
true = lists:member(Value, [ascii, codepoint, none]),
|
|
|
|
parse_opts(Rest, Opts#opts{escaped_unicode = Value});
|
|
|
|
parse_opts([{naked_values, Value}|Rest], Opts) ->
|
|
|
|
true = lists:member(Value, [true, false]),
|
2010-05-24 15:47:29 -07:00
|
|
|
parse_opts(Rest, Opts#opts{naked_values = Value, explicit_termination = true});
|
2010-05-18 13:01:49 -07:00
|
|
|
parse_opts([{encoding, Value}|Rest], Opts) ->
|
2010-05-24 15:47:29 -07:00
|
|
|
true = lists:member(Value, [utf8]),
|
|
|
|
parse_opts(Rest, Opts#opts{encoding = Value});
|
|
|
|
parse_opts([{explicit_termination, Value}|Rest], Opts) ->
|
|
|
|
true = lists:member(Value, [true, false]),
|
|
|
|
parse_opts(Rest, Opts#opts{explicit_termination = Value}).
|
2010-05-25 13:32:54 -07:00
|
|
|
|
2010-05-23 22:40:35 -07:00
|
|
|
|
2010-05-25 21:04:52 -07:00
|
|
|
%% ensures there's no invalid characters left in the stream upon completion of parsing
|
|
|
|
|
2010-05-25 13:31:41 -07:00
|
|
|
tail_clean(<<X/utf8, Rest/binary>>) when ?is_whitespace(X) ->
|
|
|
|
tail_clean(Rest);
|
|
|
|
tail_clean(<<>>) ->
|
|
|
|
true;
|
|
|
|
tail_clean(_) ->
|
|
|
|
false.
|