Skip to content

Commit c91dc3a

Browse files
committed
Act on CI errors
1 parent 85d0347 commit c91dc3a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/json.erl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,11 @@ error_info(Skip) ->
541541
%% Format implementation
542542
%%
543543

544+
-if(?OTP_RELEASE >= 26).
544545
-type formatter() :: fun((Term :: dynamic(), Encoder :: formatter(), State :: map()) -> iodata()).
546+
-else.
547+
-type formatter() :: fun((Term :: term(), Encoder :: formatter(), State :: map()) -> iodata()).
548+
-endif.
545549

546550
%% @doc Generates formatted JSON corresponding to `Term`.
547551
%% Similiar to `encode/1` but with added whitespaces for formatting.
@@ -553,15 +557,24 @@ error_info(Skip) ->
553557
%% }
554558
%% ok
555559
%% ```
560+
-if(?OTP_RELEASE >= 26).
556561
-spec format(Term :: dynamic()) -> iodata().
562+
-else.
563+
-spec format(Term :: term()) -> iodata().
564+
-endif.
557565
format(Term) ->
558566
Enc = fun format_value/3,
559567
format(Term, Enc, #{}).
560568

561569
%% @doc Generates formatted JSON corresponding to `Term`.
562570
%% Equivalent to `format(Term, fun json:format_value/3, Options)` or `format(Term, Encoder, #{})`
571+
-if(?OTP_RELEASE >= 26).
563572
-spec format(Term :: encode_value(), Opts :: map()) -> iodata();
564573
(Term :: dynamic(), Encoder::formatter()) -> iodata().
574+
-else.
575+
-spec format(Term :: encode_value(), Opts :: map()) -> iodata();
576+
(Term :: term(), Encoder::formatter()) -> iodata().
577+
-endif.
565578
format(Term, Options) when is_map(Options) ->
566579
Enc = fun format_value/3,
567580
format(Term, Enc, Options);
@@ -605,7 +618,12 @@ format(Term, Encoder, Options) when is_function(Encoder, 3) ->
605618
%% @doc Default format function used by `json:format/1`.
606619
%% Recursively calls `Encode` on all the values in `Value`,
607620
%% and indents objects and lists.
621+
-if(?OTP_RELEASE >= 26).
608622
-spec format_value(Value::dynamic(), Encode::formatter(), State::map()) -> iodata().
623+
-else.
624+
-spec format_value(Value::term(), Encode::formatter(), State::map()) -> iodata().
625+
-endif.
626+
-if(?OTP_RELEASE > 24).
609627
format_value(Atom, UserEnc, State) when is_atom(Atom) ->
610628
json:encode_atom(Atom, fun(Value, Enc) -> UserEnc(Value, Enc, State) end);
611629
format_value(Bin, _Enc, _State) when is_binary(Bin) ->
@@ -622,6 +640,24 @@ format_value(Map, UserEnc, State) when is_map(Map) ->
622640
format_key_value_list(OrderedKV, UserEnc, State);
623641
format_value(Other, _Enc, _State) ->
624642
error({unsupported_type, Other}).
643+
-else.
644+
format_value(Atom, UserEnc, State) when is_atom(Atom) ->
645+
json:encode_atom(Atom, fun(Value, Enc) -> UserEnc(Value, Enc, State) end);
646+
format_value(Bin, _Enc, _State) when is_binary(Bin) ->
647+
json:encode_binary(Bin);
648+
format_value(Int, _Enc, _State) when is_integer(Int) ->
649+
json:encode_integer(Int);
650+
format_value(Float, _Enc, _State) when is_float(Float) ->
651+
json:encode_float(Float);
652+
format_value(List, UserEnc, State) when is_list(List) ->
653+
format_list(List, UserEnc, State);
654+
format_value(Map, UserEnc, State) when is_map(Map) ->
655+
%% Ensure order of maps are the same in each export
656+
OrderedKV = lists:keysort(1, maps:to_list(Map)),
657+
format_key_value_list(OrderedKV, UserEnc, State);
658+
format_value(Other, _Enc, _State) ->
659+
error({unsupported_type, Other}).
660+
-endif.
625661

626662
format_list([Head|Rest], UserEnc, #{level := Level, col := Col0, max := Max} = State0) ->
627663
State1 = State0#{level := Level+1},

test/json_polyfill_SUITE.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ decode(_Config) ->
5858
test_encode_proplist/1,
5959
test_encode_escape_all/1,
6060
test_format_list/1,
61+
test_format_proplist/1,
6162
test_format_map/1,
6263
test_format_fun/1,
6364
test_decode_atoms/1,

0 commit comments

Comments
 (0)