Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/stdlib/src/json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Simple JSON value encodeable with `json:encode/1`.
| list(encode_value())
| encode_map(encode_value()).

-type encode_map(Value) :: #{binary() | atom() | integer() => Value}.
-type encode_map(Value) :: #{binary() | atom() | integer() | float() => Value}.

-doc """
Generates JSON corresponding to `Term`.
Expand All @@ -133,6 +133,9 @@ Supports basic data mapping:
| `#{binary() => _}` | Object |
| `#{atom() => _}` | Object |
| `#{integer() => _}` | Object |
| `#{float() => _}` | Object |

Map keys are encoded as JSON object names, which are strings.

This is equivalent to `encode(Term, fun json:encode_value/2)`.

Expand Down Expand Up @@ -250,7 +253,8 @@ list_loop([Elem | Rest], Encode) -> [$,, Encode(Elem, Encode) | list_loop(Rest,
-doc """
Default encoder for maps as JSON objects used by `json:encode/1`.

Accepts maps with atom, binary, integer, or float keys.
Accepts Erlang maps with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
""".
-doc(#{since => <<"OTP 27.0">>}).
-spec encode_map(encode_map(dynamic()), encoder()) -> iodata().
Expand All @@ -263,7 +267,8 @@ do_encode_map(Map, Encode) when is_function(Encode, 2) ->
-doc """
Encoder for maps as JSON objects.

Accepts maps with atom, binary, integer, or float keys.
Accepts Erlang maps with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
Verifies that no duplicate keys will be produced in the
resulting JSON object.

Expand All @@ -279,7 +284,8 @@ encode_map_checked(Map, Encode) ->
-doc """
Encoder for lists of key-value pairs as JSON objects.

Accepts lists with atom, binary, integer, or float keys.
Accepts key-value lists with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
""".
-doc(#{since => <<"OTP 27.0">>}).
-spec encode_key_value_list([{term(), term()}], encoder()) -> iodata().
Expand All @@ -289,7 +295,8 @@ encode_key_value_list(List, Encode) when is_function(Encode, 2) ->
-doc """
Encoder for lists of key-value pairs as JSON objects.

Accepts lists with atom, binary, integer, or float keys.
Accepts key-value lists with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
Verifies that no duplicate keys will be produced in the
resulting JSON object.

Expand Down Expand Up @@ -702,7 +709,8 @@ format_tail([], _, _, _, _) ->
-doc """
Format function for lists of key-value pairs as JSON objects.

Accepts lists with atom, binary, integer, or float keys.
Accepts key-value lists with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
""".
-doc(#{since => <<"OTP 27.2">>}).

Expand All @@ -722,7 +730,8 @@ format_key_value_list(KVList, UserEnc, #{level := Level} = State) ->
-doc """
Format function for lists of key-value pairs as JSON objects.

Accepts lists with atom, binary, integer, or float keys.
Accepts key-value lists with atom, binary, integer, or float keys.
The keys are encoded as JSON object names.
Verifies that no duplicate keys will be produced in the
resulting JSON object.

Expand Down
2 changes: 2 additions & 0 deletions lib/stdlib/test/json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ test_encode_map(_Config) ->
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode(#{<<"foo">> => <<"bar">>})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode(#{foo => bar})),
?assertEqual(<<"{\"42\":\"bar\"}">>, encode(#{42 => bar})),
?assertEqual(<<"{\"1.5\":\"bar\"}">>, encode(#{1.5 => bar})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode_checked(#{<<"foo">> => <<"bar">>})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode_checked(#{foo => bar})),
?assertEqual(<<"{\"42\":\"bar\"}">>, encode_checked(#{42 => bar})),
?assertEqual(<<"{\"1.5\":\"bar\"}">>, encode_checked(#{1.5 => bar})),

MultiKeyMap = #{<<"foo">> => <<"foo1">>, foo => <<"foo2">>},
?assertError({duplicate_key, <<"foo">>}, encode_checked(MultiKeyMap)),
Expand Down
Loading