jsx/src/jsx.erl

63 lines
2.4 KiB
Erlang
Raw Normal View History

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).
-author("alisdairsullivan@yahoo.ca").
2010-05-18 13:01:49 -07:00
-export([decoder/0, decoder/1, decoder/2]).
2010-05-18 13:01:49 -07:00
-include("jsx_common.hrl").
2010-05-18 13:01:49 -07:00
2010-05-18 13:01:49 -07:00
decoder() ->
decoder([]).
decoder(Opts) ->
F = fun(completed_parse, State) -> lists:reverse(State) ;(Event, State) -> [Event] ++ State end,
decoder({F, []}, Opts).
2010-05-18 13:01:49 -07:00
decoder({F, _} = Callbacks, OptsList) when is_list(OptsList), is_function(F) ->
Opts = parse_opts(OptsList),
decoder(Callbacks, Opts);
decoder({{Mod, Fun}, State}, OptsList) when is_list(OptsList), is_atom(Mod), is_atom(Fun) ->
Opts = parse_opts(OptsList),
decoder({fun(E, S) -> Mod:Fun(E, S) end, State}, Opts);
decoder(Callbacks, Opts) ->
fun(Stream) -> jsx_decoder:start(Stream, [], Callbacks, Opts) end.
2010-05-18 13:01:49 -07:00
parse_opts(Opts) ->
parse_opts(Opts, {false, codepoint}).
2010-05-18 13:01:49 -07:00
parse_opts([], Opts) ->
Opts;
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode}) ->
2010-05-18 13:01:49 -07:00
true = lists:member(Value, [true, false]),
parse_opts(Rest, {Value, EscapedUnicode});
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode}) ->
2010-05-18 13:01:49 -07:00
true = lists:member(Value, [ascii, codepoint, none]),
parse_opts(Rest, {Comments, Value});
parse_opts([_UnknownOpt|Rest], Opts) ->
parse_opts(Rest, Opts).