Add return_tail option
This commit is contained in:
parent
a79477d910
commit
82815646e6
3 changed files with 42 additions and 0 deletions
|
@ -65,6 +65,8 @@ parse_config([dirty_strings|Rest], Config) ->
|
||||||
parse_config(Rest, Config#config{dirty_strings=true});
|
parse_config(Rest, Config#config{dirty_strings=true});
|
||||||
parse_config([multi_term|Rest], Config) ->
|
parse_config([multi_term|Rest], Config) ->
|
||||||
parse_config(Rest, Config#config{multi_term=true});
|
parse_config(Rest, Config#config{multi_term=true});
|
||||||
|
parse_config([return_tail|Rest], Config) ->
|
||||||
|
parse_config(Rest, Config#config{return_tail=true});
|
||||||
%% retained for backwards compat, now does nothing however
|
%% retained for backwards compat, now does nothing however
|
||||||
parse_config([repeat_keys|Rest], Config) ->
|
parse_config([repeat_keys|Rest], Config) ->
|
||||||
parse_config(Rest, Config);
|
parse_config(Rest, Config);
|
||||||
|
@ -155,6 +157,7 @@ valid_flags() ->
|
||||||
unescaped_jsonp,
|
unescaped_jsonp,
|
||||||
dirty_strings,
|
dirty_strings,
|
||||||
multi_term,
|
multi_term,
|
||||||
|
return_tail,
|
||||||
repeat_keys,
|
repeat_keys,
|
||||||
strict,
|
strict,
|
||||||
stream,
|
stream,
|
||||||
|
@ -196,6 +199,7 @@ config_test_() ->
|
||||||
unescaped_jsonp = true,
|
unescaped_jsonp = true,
|
||||||
dirty_strings = true,
|
dirty_strings = true,
|
||||||
multi_term = true,
|
multi_term = true,
|
||||||
|
return_tail = true,
|
||||||
strict_comments = true,
|
strict_comments = true,
|
||||||
strict_commas = true,
|
strict_commas = true,
|
||||||
strict_utf8 = true,
|
strict_utf8 = true,
|
||||||
|
@ -209,6 +213,7 @@ config_test_() ->
|
||||||
escaped_strings,
|
escaped_strings,
|
||||||
unescaped_jsonp,
|
unescaped_jsonp,
|
||||||
multi_term,
|
multi_term,
|
||||||
|
return_tail,
|
||||||
repeat_keys,
|
repeat_keys,
|
||||||
strict,
|
strict,
|
||||||
stream,
|
stream,
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
strict_single_quotes = false :: boolean(),
|
strict_single_quotes = false :: boolean(),
|
||||||
strict_escapes = false :: boolean(),
|
strict_escapes = false :: boolean(),
|
||||||
stream = false :: boolean(),
|
stream = false :: boolean(),
|
||||||
|
return_tail = false :: boolean(),
|
||||||
uescape = false :: boolean(),
|
uescape = false :: boolean(),
|
||||||
unescaped_jsonp = false :: boolean(),
|
unescaped_jsonp = false :: boolean(),
|
||||||
error_handler = false :: false | jsx_config:handler(),
|
error_handler = false :: false | jsx_config:handler(),
|
||||||
|
|
|
@ -1129,6 +1129,8 @@ done(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
|
||||||
comment(Rest, Handler, done, [multicomment|Stack], Config);
|
comment(Rest, Handler, done, [multicomment|Stack], Config);
|
||||||
done(<<?solidus>>, Handler, Stack, Config) ->
|
done(<<?solidus>>, Handler, Stack, Config) ->
|
||||||
incomplete(done, <<?solidus>>, Handler, Stack, Config);
|
incomplete(done, <<?solidus>>, Handler, Stack, Config);
|
||||||
|
done(Bin, {_Handler, State}, _Stack, #config{return_tail=true}) ->
|
||||||
|
{with_tail,State, Bin};
|
||||||
done(<<>>, {Handler, State}, [], Config=#config{stream=true}) ->
|
done(<<>>, {Handler, State}, [], Config=#config{stream=true}) ->
|
||||||
incomplete(done, <<>>, {Handler, State}, [], Config);
|
incomplete(done, <<>>, {Handler, State}, [], Config);
|
||||||
done(<<>>, {_Handler, State}, [], _Config) -> State;
|
done(<<>>, {_Handler, State}, [], _Config) -> State;
|
||||||
|
@ -1938,4 +1940,38 @@ custom_incomplete_handler_test_() ->
|
||||||
].
|
].
|
||||||
|
|
||||||
|
|
||||||
|
return_tail_test_() ->
|
||||||
|
[
|
||||||
|
{"return_tail with tail", ?_assertEqual(
|
||||||
|
{with_tail,[{}],<<"3">>},
|
||||||
|
jsx:decode(<<"{} 3">>, [return_tail])
|
||||||
|
)},
|
||||||
|
{"return_tail without tail", ?_assertEqual(
|
||||||
|
{with_tail,[{}],<<"">>},
|
||||||
|
jsx:decode(<<"{}">>, [return_tail])
|
||||||
|
)},
|
||||||
|
{"return_tail with trimmed whitespace", ?_assertEqual(
|
||||||
|
{with_tail,[{}],<<"">>},
|
||||||
|
jsx:decode(<<"{} ">>, [return_tail])
|
||||||
|
)},
|
||||||
|
{"return_tail and streaming", ?_assertEqual(
|
||||||
|
{with_tail,[{}],<<"3">>},
|
||||||
|
begin
|
||||||
|
{incomplete, F} = jsx:decode(<<"{">>, [return_tail, stream]),
|
||||||
|
F(<<"} 3">>)
|
||||||
|
end
|
||||||
|
)},
|
||||||
|
{"return_tail and streaming", ?_assertEqual(
|
||||||
|
{with_tail,[{}],<<"">>},
|
||||||
|
begin
|
||||||
|
%% In case of infinite stream of objects a user does not know
|
||||||
|
%% when to call F(end_stream).
|
||||||
|
%% So, return_tail overwrites conservative stream end.
|
||||||
|
%% This means that we don't need to call end_stream explicitly.
|
||||||
|
{incomplete, F} = jsx:decode(<<"{">>, [return_tail, stream]),
|
||||||
|
F(<<"}">>)
|
||||||
|
end
|
||||||
|
)}
|
||||||
|
].
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue