Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom formatter for floats #155

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jsx_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
| {indent, non_neg_integer()}
| {depth, non_neg_integer()}
| {newline, binary()}
| {float_formatter, fun((any()) -> any())}
| legacy_option()
| {legacy_option(), boolean()}.
-type legacy_option() :: strict_comments
Expand Down
31 changes: 27 additions & 4 deletions src/jsx_to_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
-export([start_json/0, start_json/1]).
-export([start_object/1, start_array/1, finish/1, insert/2, get_key/1, get_value/1]).

-ifdef(TEST).
-export([custom_float_formatter/1]).
-endif.

-record(config, {
space = 0,
indent = 0,
depth = 0,
newline = <<$\n>>
newline = <<$\n>>,
float_formatter = undefined
}).

-type config() :: proplists:proplist().
Expand Down Expand Up @@ -64,6 +68,8 @@ parse_config([indent|Rest], Config) ->
parse_config(Rest, Config#config{indent = 1});
parse_config([{newline, Val}|Rest], Config) when is_binary(Val) ->
parse_config(Rest, Config#config{newline = Val});
parse_config([{float_formatter, Val}|Rest], Config) when is_function(Val, 1) ->
parse_config(Rest, Config#config{float_formatter = Val});
parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_config:valid_flags()) of
true -> parse_config(Rest, Config)
Expand Down Expand Up @@ -116,9 +122,11 @@ encode(literal, Literal, _Config) ->
erlang:atom_to_list(Literal);
encode(integer, Integer, _Config) ->
erlang:integer_to_list(Integer);
encode(float, Float, _Config) ->
io_lib:format("~p", [Float]).

encode(float, Float, Config) ->
case Config#config.float_formatter of
undefined -> io_lib:format("~p", [Float]);
Fun -> Fun(Float)
end.

space(Config) ->
case Config#config.space of
Expand Down Expand Up @@ -405,5 +413,20 @@ handle_event_test_() ->
} || {Title, JSON, _, Events} <- Data
].

custom_float_formatter(_Float) ->
["foo"].

encode_with_float_formatter_test_() ->
[
{"foo formatter",
?_assert(
encode(float, 3.1234567890987654321, #config{float_formatter = fun ?MODULE:custom_float_formatter/1 }) =:= ["foo"])
},
{"encoded floats with formatter are floats in output",
?_assertEqual(
<<"{\"one\":{\"two\":3.1235}}">>,
jsx:encode(#{one => #{two => 3.1234567890987654321}}, [{float_formatter, fun(F) -> io_lib:format("~.4f", [F]) end}])
)}
].

-endif.