@@ -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 .
557565format (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 .
565578format (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 >= 26 ).
609627format_value (Atom , UserEnc , State ) when is_atom (Atom ) ->
610628 json :encode_atom (Atom , fun (Value , Enc ) -> UserEnc (Value , Enc , State ) end );
611629format_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 );
623641format_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
626662format_list ([Head |Rest ], UserEnc , #{level := Level , col := Col0 , max := Max } = State0 ) ->
627663 State1 = State0 #{level := Level + 1 },
0 commit comments