Browse Source

Support encodings proplists formatted object

Takeru Ohta 10 years ago
parent
commit
8191e8bbb9
4 changed files with 15 additions and 7 deletions
  1. 1 1
      doc/jsone.md
  2. 3 1
      src/jsone.erl
  3. 3 1
      src/jsone_encode.erl
  4. 8 4
      test/jsone_encode_tests.erl

+ 1 - 1
doc/jsone.md

@@ -72,7 +72,7 @@ json_number() = number()
 
 
 <pre><code>
-json_object() = {<a href="#type-json_object_members">json_object_members()</a>}
+json_object() = {<a href="#type-json_object_members">json_object_members()</a>} | [{}] | <a href="#type-json_object_members">json_object_members()</a>
 </code></pre>
 
 

+ 3 - 1
src/jsone.erl

@@ -56,7 +56,9 @@
 -type json_number()         :: number().
 -type json_string()         :: binary().
 -type json_array()          :: [json_value()].
--type json_object()         :: {json_object_members()}.
+-type json_object()         :: {json_object_members()}
+                             | [{}]
+                             | json_object_members().
 -type json_object_members() :: [{json_object_key(), json_value()}].
 -type json_object_key()     :: json_string() | atom(). % NOTE: `decode/1' always returns `json_string()' key
 

+ 3 - 1
src/jsone_encode.erl

@@ -90,8 +90,10 @@ value(true, Nexts, Buf, Opt)                         -> next(Nexts, <<Buf/binary
 value(Value, Nexts, Buf, Opt) when is_integer(Value) -> next(Nexts, <<Buf/binary, (integer_to_binary(Value))/binary>>, Opt);
 value(Value, Nexts, Buf, Opt) when is_float(Value)   -> next(Nexts, <<Buf/binary, (float_to_binary(Value))/binary>>, Opt);
 value(Value, Nexts, Buf, Opt) when is_binary(Value)  -> string(Value, Nexts, Buf, Opt);
-value(Value, Nexts, Buf, Opt) when is_list(Value)    -> array(Value, Nexts, Buf, Opt);
 value({_} = Value, Nexts, Buf, Opt)                  -> object(Value, Nexts, Buf, Opt);
+value([{}], Nexts, Buf, Opt)                         -> object({[]}, Nexts, Buf, Opt);
+value([{_, _}|_] = Value, Nexts, Buf, Opt)           -> object({Value}, Nexts, Buf, Opt);
+value(Value, Nexts, Buf, Opt) when is_list(Value)    -> array(Value, Nexts, Buf, Opt);
 value(Value, Nexts, Buf, _)                        -> ?ERROR(value, [Value, Nexts, Buf]).
 
 -spec string(jsone:json_string(), [next()], binary(), encode_opt()) -> encode_result().

+ 8 - 4
test/jsone_encode_tests.erl

@@ -98,15 +98,19 @@ encode_test_() ->
      %% Objects
      {"simple object",
       fun () ->
-              Input    = {[{<<"key">>, <<"value">>}, {<<"1">>, 2}]},
+              Input1   = {[{<<"key">>, <<"value">>}, {<<"1">>, 2}]},
+              Input2   = [{<<"key">>, <<"value">>}, {<<"1">>, 2}],
               Expected = <<"{\"key\":\"value\",\"1\":2}">>,
-              ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
+              ?assertEqual({ok, Expected}, jsone_encode:encode(Input1)),
+              ?assertEqual({ok, Expected}, jsone_encode:encode(Input2))
       end},
      {"empty object",
       fun () ->
-              Input    = {[]},
+              Input1   = {[]},
+              Input2   = [{}],
               Expected = <<"{}">>,
-              ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
+              ?assertEqual({ok, Expected}, jsone_encode:encode(Input1)),
+              ?assertEqual({ok, Expected}, jsone_encode:encode(Input2))
       end},
      {"atom key is allowed",
       fun () ->