2010-09-20 19:11:31 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
|
|
|
|
|
|
%% 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.
|
|
|
|
|
|
|
|
|
2011-03-22 18:53:02 -07:00
|
|
|
-module(jsx_utils).
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2012-03-02 22:53:23 -08:00
|
|
|
-export([parse_opts/1]).
|
2012-03-04 18:40:00 -08:00
|
|
|
-export([extract_opts/1]).
|
2012-03-31 20:31:28 -07:00
|
|
|
-export([json_escape_sequence/1]).
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2012-03-06 19:59:49 -08:00
|
|
|
-include("jsx_opts.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2010-09-22 23:22:55 -07:00
|
|
|
|
2011-10-19 06:51:36 -07:00
|
|
|
%% parsing of jsx opts
|
|
|
|
parse_opts(Opts) ->
|
|
|
|
parse_opts(Opts, #opts{}).
|
|
|
|
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts;
|
|
|
|
parse_opts([loose_unicode|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{loose_unicode=true});
|
|
|
|
parse_opts([escape_forward_slash|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{escape_forward_slash=true});
|
2011-10-21 18:16:16 -07:00
|
|
|
parse_opts([explicit_end|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{explicit_end=true});
|
2012-03-14 23:01:59 -07:00
|
|
|
parse_opts([single_quotes|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{single_quotes=true});
|
2012-03-15 23:06:19 -07:00
|
|
|
parse_opts([no_jsonp_escapes|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{no_jsonp_escapes=true});
|
2012-03-19 14:34:07 -07:00
|
|
|
parse_opts([comments|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{comments=true});
|
2012-03-24 19:42:00 -07:00
|
|
|
parse_opts([json_escape|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{json_escape=true});
|
2012-03-29 00:01:50 -07:00
|
|
|
parse_opts([dirty_strings|Rest], Opts) ->
|
2012-03-29 00:24:31 -07:00
|
|
|
parse_opts(Rest, Opts#opts{dirty_strings=true});
|
2012-03-29 00:18:53 -07:00
|
|
|
parse_opts([ignore_bad_escapes|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{ignore_bad_escapes=true});
|
2012-03-29 00:24:31 -07:00
|
|
|
parse_opts([relax|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{
|
|
|
|
loose_unicode = true,
|
|
|
|
single_quotes = true,
|
|
|
|
comments = true,
|
|
|
|
ignore_bad_escapes = true
|
|
|
|
});
|
2011-10-19 06:51:36 -07:00
|
|
|
parse_opts(_, _) ->
|
|
|
|
{error, badarg}.
|
|
|
|
|
|
|
|
|
2012-03-15 23:06:19 -07:00
|
|
|
valid_flags() ->
|
|
|
|
[
|
|
|
|
loose_unicode,
|
|
|
|
escape_forward_slash,
|
|
|
|
explicit_end,
|
|
|
|
single_quotes,
|
2012-03-19 14:34:07 -07:00
|
|
|
no_jsonp_escapes,
|
2012-03-24 19:42:00 -07:00
|
|
|
comments,
|
2012-03-29 00:01:50 -07:00
|
|
|
json_escape,
|
2012-03-29 00:18:53 -07:00
|
|
|
dirty_strings,
|
2012-03-29 00:24:31 -07:00
|
|
|
ignore_bad_escapes,
|
|
|
|
relax
|
2012-03-15 23:06:19 -07:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
extract_opts(Opts) ->
|
|
|
|
extract_parser_opts(Opts, []).
|
|
|
|
|
|
|
|
extract_parser_opts([], Acc) -> Acc;
|
|
|
|
extract_parser_opts([{K,V}|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2012-03-04 18:40:00 -08:00
|
|
|
true -> extract_parser_opts(Rest, [{K,V}] ++ Acc)
|
|
|
|
; false -> extract_parser_opts(Rest, Acc)
|
|
|
|
end;
|
|
|
|
extract_parser_opts([K|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2012-03-04 18:40:00 -08:00
|
|
|
true -> extract_parser_opts(Rest, [K] ++ Acc)
|
|
|
|
; false -> extract_parser_opts(Rest, Acc)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-09-14 06:44:52 -07:00
|
|
|
%% convert a codepoint to it's \uXXXX equiv.
|
|
|
|
json_escape_sequence(X) ->
|
|
|
|
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
2012-03-31 01:24:06 -07:00
|
|
|
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
|
2011-09-14 06:44:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
to_hex(10) -> $a;
|
|
|
|
to_hex(11) -> $b;
|
|
|
|
to_hex(12) -> $c;
|
|
|
|
to_hex(13) -> $d;
|
|
|
|
to_hex(14) -> $e;
|
|
|
|
to_hex(15) -> $f;
|
|
|
|
to_hex(X) -> X + 48. %% ascii "1" is [49], "2" is [50], etc...
|
2010-09-20 19:11:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
%% eunit tests
|
|
|
|
-ifdef(TEST).
|
2011-08-10 01:44:38 -07:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2012-03-31 20:31:28 -07:00
|
|
|
json_escape_sequence_test_() ->
|
2011-09-14 06:44:52 -07:00
|
|
|
[
|
2012-03-31 20:31:28 -07:00
|
|
|
{"json escape sequence test - 16#0000", ?_assertEqual(json_escape_sequence(16#0000), "\\u0000")},
|
|
|
|
{"json escape sequence test - 16#abc", ?_assertEqual(json_escape_sequence(16#abc), "\\u0abc")},
|
|
|
|
{"json escape sequence test - 16#def", ?_assertEqual(json_escape_sequence(16#def), "\\u0def")}
|
2011-09-14 06:44:52 -07:00
|
|
|
].
|
|
|
|
|
2012-03-29 06:03:14 -07:00
|
|
|
opts_test_() ->
|
|
|
|
[
|
|
|
|
{"all flags",
|
|
|
|
?_assertEqual(
|
|
|
|
parse_opts([
|
|
|
|
loose_unicode,
|
|
|
|
escape_forward_slash,
|
|
|
|
explicit_end,
|
|
|
|
single_quotes,
|
|
|
|
no_jsonp_escapes,
|
|
|
|
comments,
|
|
|
|
dirty_strings,
|
|
|
|
ignore_bad_escapes
|
|
|
|
]),
|
|
|
|
#opts{
|
|
|
|
loose_unicode=true,
|
|
|
|
escape_forward_slash=true,
|
|
|
|
explicit_end=true,
|
|
|
|
single_quotes=true,
|
|
|
|
no_jsonp_escapes=true,
|
|
|
|
comments=true,
|
|
|
|
dirty_strings=true,
|
|
|
|
ignore_bad_escapes=true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"relax flag",
|
|
|
|
?_assertEqual(
|
|
|
|
parse_opts([relax]),
|
|
|
|
#opts{
|
|
|
|
loose_unicode=true,
|
|
|
|
single_quotes=true,
|
|
|
|
comments=true,
|
|
|
|
ignore_bad_escapes=true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2010-09-20 19:11:31 -07:00
|
|
|
-endif.
|