Skip to content

Commit

Permalink
Use float_to_binary [:short] if available
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmuskala committed Sep 12, 2022
1 parent 433a764 commit 2a83c8f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,23 @@ defmodule Jason.Encode do
Integer.to_string(integer)
end

has_short_format = try do
:erlang.float_to_binary(1.0, [:short])
catch
_, _ -> false
else
_ -> true
end

@spec float(float) :: iodata
def float(float) do
:io_lib_format.fwrite_g(float)
if has_short_format do
def float(float) do
:erlang.float_to_binary(float, [:short])
end
else
def float(float) do
:io_lib_format.fwrite_g(float)
end
end

@spec list(list, opts) :: iodata
Expand Down

0 comments on commit 2a83c8f

Please sign in to comment.