remove license replication from include files, move json_escape/2 to jsx_utils
This commit is contained in:
parent
0d72a07aa7
commit
59a983bbe9
8 changed files with 180 additions and 274 deletions
|
@ -1,26 +1,3 @@
|
|||
%% 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.
|
||||
|
||||
|
||||
-ifndef(error).
|
||||
-define(error(Args),
|
||||
erlang:error(badarg, Args)
|
||||
|
@ -49,7 +26,7 @@
|
|||
|
||||
start({string, String}, [], [], Opts) when is_binary(String); is_list(String) ->
|
||||
{ok,
|
||||
[{string, unicode:characters_to_list(json_escape(String, Opts))}, end_json],
|
||||
[{string, unicode:characters_to_list(jsx_utils:json_escape(String, Opts))}, end_json],
|
||||
fun(X) when is_list(X) -> ?error([X, [], [], Opts]) end
|
||||
};
|
||||
start({float, Float}, [], [], Opts) when is_float(Float) ->
|
||||
|
@ -80,7 +57,7 @@ list_or_object(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
|||
|
||||
|
||||
key([{key, Key}|Forms], T, Stack, Opts) when is_binary(Key); is_list(Key) ->
|
||||
?event([{key, unicode:characters_to_list(json_escape(Key, Opts))}],
|
||||
?event([{key, unicode:characters_to_list(jsx_utils:json_escape(Key, Opts))}],
|
||||
value, Forms, T, Stack, Opts
|
||||
);
|
||||
key([end_object|Forms], T, [object|Stack], Opts) ->
|
||||
|
@ -90,7 +67,7 @@ key(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
|||
|
||||
|
||||
value([{string, S}|Forms], T, Stack, Opts) when is_binary(S); is_list(S) ->
|
||||
?event([{string, unicode:characters_to_list(json_escape(S, Opts))}],
|
||||
?event([{string, unicode:characters_to_list(jsx_utils:json_escape(S, Opts))}],
|
||||
maybe_done, Forms, T, Stack, Opts
|
||||
);
|
||||
value([{float, F}|Forms], T, Stack, Opts) when is_float(F) ->
|
||||
|
@ -127,102 +104,4 @@ done([], T, [], Opts) ->
|
|||
done(X, T, [], Opts)
|
||||
end
|
||||
};
|
||||
done(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
||||
|
||||
|
||||
%% json string escaping, for utf8 binaries. escape the json control sequences to
|
||||
%% their json equivalent, escape other control characters to \uXXXX sequences,
|
||||
%% everything else should be a legal json string component
|
||||
json_escape(String, Opts) when is_binary(String) ->
|
||||
json_escape(String, Opts, <<>>);
|
||||
json_escape(String, Opts) when is_list(String) ->
|
||||
json_escape(String, Opts, []).
|
||||
|
||||
%% double quote
|
||||
json_escape(<<$\", Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $\">>);
|
||||
json_escape([$\"|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$\", $\\] ++ Acc);
|
||||
%% backslash \ reverse solidus
|
||||
json_escape(<<$\\, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $\\>>);
|
||||
json_escape([$\\|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$\\, $\\] ++ Acc);
|
||||
%% backspace
|
||||
json_escape(<<$\b, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $b>>);
|
||||
json_escape([$\b|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$b, $\\] ++ Acc);
|
||||
%% form feed
|
||||
json_escape(<<$\f, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $f>>);
|
||||
json_escape([$\f|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$f, $\\] ++ Acc);
|
||||
%% newline
|
||||
json_escape(<<$\n, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $n>>);
|
||||
json_escape([$\n|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$n, $\\] ++ Acc);
|
||||
%% cr
|
||||
json_escape(<<$\r, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $r>>);
|
||||
json_escape([$\r|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$r, $\\] ++ Acc);
|
||||
%% tab
|
||||
json_escape(<<$\t, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $t>>);
|
||||
json_escape([$\t|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [$t, $\\] ++ Acc);
|
||||
%% other control characters
|
||||
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C >= 0, C < $\s ->
|
||||
json_escape(Rest,
|
||||
Opts,
|
||||
<<Acc/binary,
|
||||
(unicode:characters_to_binary(json_escape_sequence(C)))/binary
|
||||
>>
|
||||
);
|
||||
json_escape([C|Rest], Opts, Acc) when C >= 0, C < $\s ->
|
||||
json_escape(Rest, Opts, lists:reverse(json_escape_sequence(C)) ++ Acc);
|
||||
%% escape forward slashes -- optionally -- to faciliate microsoft's retarded
|
||||
%% date format
|
||||
json_escape(<<$/, Rest/binary>>, Opts=#opts{escape_forward_slash=true}, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, $\\, $/>>);
|
||||
json_escape([$/|Rest], Opts=#opts{escape_forward_slash=true}, Acc) ->
|
||||
json_escape(Rest, Opts, [$/, $\\] ++ Acc);
|
||||
%% escape u+2028 and u+2029 to avoid problems with jsonp
|
||||
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc)
|
||||
when C == 16#2028; C == 16#2029 ->
|
||||
json_escape(Rest,
|
||||
Opts,
|
||||
<<Acc/binary,
|
||||
(unicode:characters_to_binary(json_escape_sequence(C)))/binary
|
||||
>>
|
||||
);
|
||||
json_escape([C|Rest], Opts, Acc) when C =:= 16#2028; C =:= 16#2029 ->
|
||||
json_escape(Rest, Opts, lists:reverse(json_escape_sequence(C)) ++ Acc);
|
||||
%% any other legal codepoint
|
||||
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) ->
|
||||
json_escape(Rest, Opts, <<Acc/binary, C/utf8>>);
|
||||
json_escape([C|Rest], Opts, Acc) ->
|
||||
json_escape(Rest, Opts, [C] ++ Acc);
|
||||
json_escape(<<>>, _Opts, Acc) ->
|
||||
Acc;
|
||||
json_escape([], _Opts, Acc) ->
|
||||
lists:reverse(Acc);
|
||||
json_escape(Rest, Opts, Acc) ->
|
||||
erlang:error(badarg, [Rest, Opts, Acc]).
|
||||
|
||||
|
||||
%% convert a codepoint to it's \uXXXX equiv.
|
||||
json_escape_sequence(X) ->
|
||||
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
||||
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
|
||||
|
||||
|
||||
to_hex(15) -> $f;
|
||||
to_hex(14) -> $e;
|
||||
to_hex(13) -> $d;
|
||||
to_hex(12) -> $c;
|
||||
to_hex(11) -> $b;
|
||||
to_hex(10) -> $a;
|
||||
to_hex(X) -> X + $0.
|
||||
done(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
Loading…
Add table
Add a link
Reference in a new issue