From 7296f790b21e1e77ff77010569f01469a8f19075 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Sun, 27 Oct 2013 01:28:49 +0000 Subject: [PATCH] convert all space/newlines in json formatter to emit binaries --- src/jsx_to_json.erl | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index 35ec437..98fa072 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -157,22 +157,17 @@ encode(float, Float, _Config) -> space(Config) -> case Config#config.space of - 0 -> [] + 0 -> <<>> ; X when X > 0 -> binary:copy(?space, X) end. indent(Config) -> case Config#config.indent of - 0 -> [] - ; X when X > 0 -> - Indent = binary:copy(?space, X), - indent(Indent, Config#config.depth, [?newline]) + 0 -> <<>> + ; X when X > 0 -> <> end. -indent(_Indent, 0, Acc) -> Acc; -indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]). - indent_or_space(Config) -> case Config#config.indent > 0 of @@ -258,7 +253,7 @@ config_test_() -> space_test_() -> [ - {"no space", ?_assertEqual([], space(#config{space=0}))}, + {"no space", ?_assertEqual(<<>>, space(#config{space=0}))}, {"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))}, {"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))} ]. @@ -266,21 +261,21 @@ space_test_() -> indent_test_() -> [ - {"no indent", ?_assertEqual([], indent(#config{indent=0, depth=1}))}, + {"no indent", ?_assertEqual(<<>>, indent(#config{indent=0, depth=1}))}, {"indent 1 depth 1", ?_assertEqual( - [[?newline], ?space], + <>/binary>>, indent(#config{indent=1, depth=1}) )}, {"indent 1 depth 2", ?_assertEqual( - [[[?newline], ?space], ?space], + <>/binary>>, indent(#config{indent=1, depth=2}) )}, {"indent 4 depth 1", ?_assertEqual( - [[?newline], <<" ">>], + <>/binary>>, indent(#config{indent=4, depth=1}) )}, {"indent 4 depth 2", ?_assertEqual( - [[[?newline], <<" ">>], <<" ">>], + <>/binary, <<" ">>/binary>>, indent(#config{indent=4, depth=2}) )} ]. @@ -293,7 +288,7 @@ indent_or_space_test_() -> indent_or_space(#config{space=1, indent=0, depth=1}) )}, {"indent so no space", ?_assertEqual( - [[?newline], ?space], + <>/binary>>, indent_or_space(#config{space=1, indent=1, depth=1}) )} ].