Skip to content

Commit

Permalink
Fix doc example for json:encode/2
Browse files Browse the repository at this point in the history
  • Loading branch information
williamthome committed Feb 22, 2025
1 parent 64185e7 commit fd44094
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/stdlib/src/json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ An encoder that uses a heuristic to differentiate object-like
lists of key-value pairs from plain lists:

```erlang
> encoder([{_, _} | _] = Value, Encode) -> json:encode_key_value_list(Value, Encode);
> encoder(Other, Encode) -> json:encode_value(Other, Encode).
> custom_encode(Value) -> json:encode(Value, fun(Value, Encode) -> encoder(Value, Encode) end).
> iolist_to_binary(custom_encode([{a, []}, {b, 1}])).
<<"{\"a\":[],\"b\":1}">>
> Encoder = fun
([{_, _} | _] = Proplist, Encode) ->
json:encode(proplists:to_map(Proplist), Encode);
(Other, Encode) ->
json:encode_value(Other, Encode)
end.
> CustomEncode = fun(Value) -> json:encode(Value, Encoder) end.
> iolist_to_binary(CustomEncode([{a, []}, {b, 1}])).
<<"{\"b\":1,\"a\":[]}">>
```
""".
-doc(#{since => <<"OTP 27.0">>}).
Expand Down

0 comments on commit fd44094

Please sign in to comment.