diff --git a/include/jsx_format.hrl b/include/jsx_format.hrl index 2746933..562c52a 100644 --- a/include/jsx_format.hrl +++ b/include/jsx_format.hrl @@ -26,7 +26,7 @@ space = 0, indent = 0, output_encoding = utf8, - strict = true + strict = false }). diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index a4d08d2..28cefba 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -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_() -> diff --git a/src/jsx_utils.erl b/src/jsx_utils.erl index 406c6e4..44fe3c9 100644 --- a/src/jsx_utils.erl +++ b/src/jsx_utils.erl @@ -222,7 +222,9 @@ detect_encoding(<>, Opts) when X =/= 0 -> try {incomplete, Next} = (jsx_utf8:parser(Opts))(<>), Next(end_stream) - catch error:function_clause -> {error, {badjson, <>}} + catch + error:function_clause -> {error, {badjson, <>}} + ; error:{badmatch, _} -> {error, {badjson, <>}} end ; (Stream) -> detect_encoding(<>, 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, <>}} end ; (Stream) -> detect_encoding(<<0, X, Stream/binary>>, Opts) end @@ -244,7 +248,9 @@ detect_encoding(<>, Opts) when X =/= 0 -> try {incomplete, Next} = (jsx_utf16le:parser(Opts))(<>), Next(end_stream) - catch error:function_clause -> {error, {badjson, <>}} + catch + error:function_clause -> {error, {badjson, <>}} + ; error:{badmatch, _} -> {error, {badjson, <>}} end ; (Stream) -> detect_encoding(<>, Opts) end diff --git a/src/jsx_verify.erl b/src/jsx_verify.erl index a23d6c0..9bcdb12 100644 --- a/src/jsx_verify.erl +++ b/src/jsx_verify.erl @@ -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",