changes default for strict to false from true to better match other json encoders/decoders in the wild

This commit is contained in:
alisdair sullivan 2011-04-26 21:30:57 -07:00
parent 428858ed8c
commit 6cc28573b6
4 changed files with 35 additions and 21 deletions

View file

@ -26,7 +26,7 @@
space = 0,
indent = 0,
output_encoding = utf8,
strict = true
strict = false
}).

View file

@ -40,7 +40,7 @@
json_to_term(JSON, Opts) ->
P = jsx:parser(extract_parser_opts(Opts)),
case proplists:get_value(strict, Opts, true) of
case proplists:get_value(strict, Opts, false) of
true -> collect_strict(P(JSON), [[]], Opts)
; false -> collect(P(JSON), [[]], Opts)
end.
@ -54,7 +54,7 @@ json_to_term(JSON, Opts) ->
-spec term_to_json(JSON::eep0018(), Opts::encoder_opts()) -> binary().
term_to_json(List, Opts) ->
case proplists:get_value(strict, Opts, true) of
case proplists:get_value(strict, Opts, false) of
true when is_list(List) -> continue
; true -> erlang:error(badarg)
; false -> continue
@ -371,18 +371,22 @@ decode_test_() ->
)
},
{"naked true",
?_assert(json_to_term(<<"true">>, [{strict, false}]) =:= true)
?_assert(json_to_term(<<"true">>, []) =:= true)
},
{"naked short number",
?_assert(json_to_term(<<"1">>, [{strict, false}]) =:= 1)
?_assert(json_to_term(<<"1">>, []) =:= 1)
},
{"float", ?_assert(json_to_term(<<"1.0">>, [{strict, false}]) =:= 1.0)},
{"naked float", ?_assert(json_to_term(<<"1.0">>, []) =:= 1.0)},
{"naked string",
?_assert(json_to_term(<<"\"hello world\"">>,
[{strict, false}]
[]
) =:= <<"hello world">>
)
},
{"strict mode", ?_assertError(badarg, json_to_term(<<"1.0">>,
[{strict, true}]
)
)},
{"comments",
?_assert(json_to_term(<<"[ /* a comment in an empty array */ ]">>,
[{comments, true}]
@ -428,24 +432,28 @@ encode_test_() ->
)
},
{"literals",
?_assert(term_to_json([true,false,null],
?_assert(term_to_json([true,false,null],
[]
) =:= <<"[true,false,null]">>
)
},
{"naked true",
?_assert(term_to_json(true, [{strict, false}]) =:= <<"true">>)
?_assert(term_to_json(true, []) =:= <<"true">>)
},
{"naked number",
?_assert(term_to_json(1, [{strict, false}]) =:= <<"1">>)
?_assert(term_to_json(1, []) =:= <<"1">>)
},
{"float", ?_assert(term_to_json(1.0, [{strict, false}]) =:= <<"1.0">>)},
{"float", ?_assert(term_to_json(1.0, []) =:= <<"1.0">>)},
{"naked string",
?_assert(term_to_json(<<"hello world">>,
[{strict, false}]
?_assert(term_to_json(<<"hello world">>,
[]
) =:= <<"\"hello world\"">>
)
}
},
{"strict mode", ?_assertError(badarg, term_to_json(true,
[{strict, true}]
)
)}
].
repeated_keys_test_() ->

View file

@ -222,7 +222,9 @@ detect_encoding(<<X>>, Opts) when X =/= 0 ->
try
{incomplete, Next} = (jsx_utf8:parser(Opts))(<<X>>),
Next(end_stream)
catch error:function_clause -> {error, {badjson, <<X>>}}
catch
error:function_clause -> {error, {badjson, <<X>>}}
; error:{badmatch, _} -> {error, {badjson, <<X>>}}
end
; (Stream) -> detect_encoding(<<X, Stream/binary>>, Opts)
end
@ -233,7 +235,9 @@ detect_encoding(<<0, X>>, Opts) when X =/= 0 ->
try
{incomplete, Next} = (jsx_utf16:parser(Opts))(<<0, X>>),
Next(end_stream)
catch error:function_clause -> {error, {badjson, <<0, X>>}}
catch
error:function_clause -> {error, {badjson, <<0, X>>}}
; error:{badmatch, _} -> {error, {badjson, <<X>>}}
end
; (Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts)
end
@ -244,7 +248,9 @@ detect_encoding(<<X, 0>>, Opts) when X =/= 0 ->
try
{incomplete, Next} = (jsx_utf16le:parser(Opts))(<<X, 0>>),
Next(end_stream)
catch error:function_clause -> {error, {badjson, <<X, 0>>}}
catch
error:function_clause -> {error, {badjson, <<X, 0>>}}
; error:{badmatch, _} -> {error, {badjson, <<X>>}}
end
; (Stream) -> detect_encoding(<<X, 0, Stream/binary>>, Opts)
end

View file

@ -41,7 +41,7 @@
is_json(JSON, Opts) ->
P = jsx:parser(extract_parser_opts(Opts)),
case proplists:get_value(strict, Opts, true) of
case proplists:get_value(strict, Opts, false) of
true -> collect_strict(P(JSON), [[]])
; false -> collect(P(JSON), [[]])
end.
@ -149,10 +149,10 @@ true_test_() ->
false_test_() ->
[
{"naked true", ?_assert(is_json(<<"true">>, []) =:= false)},
{"naked number", ?_assert(is_json(<<"1">>, []) =:= false)},
{"naked true", ?_assert(is_json(<<"true">>, []) =:= true)},
{"naked number", ?_assert(is_json(<<"1">>, []) =:= true)},
{"naked string",
?_assert(is_json(<<"\"i am not json\"">>, []) =:= false)
?_assert(is_json(<<"\"i am not really json\"">>, []) =:= true)
},
{"unbalanced list", ?_assert(is_json(<<"[[[]]">>, []) =:= false)},
{"trailing comma",