From 5413c837d4198286ba5300d421c852183a075853 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 3 Aug 2010 21:15:24 -0700 Subject: [PATCH] more tests, more bugs fixed --- src/jsx_eep0018.erl | 9 ++++++--- test/jsx_test.escript | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index 6ea5ed3..d0afe3b 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -70,15 +70,18 @@ collect({event, Start, Next}, Acc, Opts) when Start =:= start_object; Start =:= collect(Next(), [[]|Acc], Opts); %% special case for empty object -collect({event, end_object, Next}, [[], Parent|Rest], Opts) -> +collect({event, end_object, Next}, [[], Parent|Rest], Opts) when is_list(Parent) -> collect(Next(), [[[{}]] ++ Parent] ++ Rest, Opts); %% reverse the array/object accumulator before prepending it to it's parent collect({event, end_object, Next}, [Current, Parent|Rest], Opts) when is_list(Parent) -> collect(Next(), [[lists:reverse(Current)] ++ Parent] ++ Rest, Opts); collect({event, end_array, Next}, [Current, Parent|Rest], Opts) when is_list(Parent) -> collect(Next(), [[lists:reverse(Current)] ++ Parent] ++ Rest, Opts); -collect({event, Start, Next}, [Current, Key, Parent|Rest], Opts) - when Start =:= end_object; Start =:= end_array -> +%% special case for empty object +collect({event, end_object, Next}, [[], Key, Parent|Rest], Opts) -> + collect(Next(), [[{Key, [{}]}] ++ Parent] ++ Rest, Opts); +collect({event, End, Next}, [Current, Key, Parent|Rest], Opts) + when End =:= end_object; End =:= end_array -> collect(Next(), [[{Key, lists:reverse(Current)}] ++ Parent] ++ Rest, Opts); collect({event, end_json, _Next}, [[Acc]], _Opts) -> diff --git a/test/jsx_test.escript b/test/jsx_test.escript index a8d0137..a2cc9c7 100755 --- a/test/jsx_test.escript +++ b/test/jsx_test.escript @@ -46,7 +46,7 @@ test(Dir) -> ValidJSONTests = load_tests(Dir), - etap:plan((length(ValidJSONTests) * 10) + 9), + etap:plan((length(ValidJSONTests) * 10) + 13), run_jsx_tests(ValidJSONTests), etap:is(multi_decode(multi_json_body(), []), multi_test_result(), "multi terms"), @@ -59,6 +59,11 @@ test(Dir) -> ?to_json([{<<"key">>, <<"value">>}, {<<"another key">>, []}], <<"{\"key\":\"value\",\"another key\":[]}">>, "object to json"), ?to_erep(<<"[true, 1, -0.5e7, \"hello world\"]">>, [true, 1, -0.5e7, <<"hello world">>], "array to erep"), ?to_json([true, 1, -0.5e7, <<"hello world">>], <<"[true,1,-5000000.0,\"hello world\"]">>, "array to json"), + ?to_erep(<<"[[[]]]">>, [[[]]], "deep array to erep"), + ?to_json([[[]]], <<"[[[]]]">>, "deep array to json"), + ?to_erep(<<"{\"a\":{\"a\":{\"a\":{}}}}">>, [{<<"a">>, [{<<"a">>, [{<<"a">>, [{}]}]}]}], "deep object to erep"), + ?to_json([{<<"a">>, [{<<"a">>, [{<<"a">>, [{}]}]}]}], <<"{\"a\":{\"a\":{\"a\":{}}}}">>, "deep object to json"), + etap:end_tests().