From fd44094459641a42ef2dea651eb3d4656579b5e8 Mon Sep 17 00:00:00 2001 From: williamthome Date: Sat, 22 Feb 2025 09:07:21 -0300 Subject: [PATCH] Fix doc example for json:encode/2 --- lib/stdlib/src/json.erl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/stdlib/src/json.erl b/lib/stdlib/src/json.erl index bfa333eda303..0122d25d6b3c 100644 --- a/lib/stdlib/src/json.erl +++ b/lib/stdlib/src/json.erl @@ -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">>}).