Skip to content

Commit 2a83c8f

Browse files
committed
Use float_to_binary [:short] if available
1 parent 433a764 commit 2a83c8f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/encode.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,23 @@ defmodule Jason.Encode do
127127
Integer.to_string(integer)
128128
end
129129

130+
has_short_format = try do
131+
:erlang.float_to_binary(1.0, [:short])
132+
catch
133+
_, _ -> false
134+
else
135+
_ -> true
136+
end
137+
130138
@spec float(float) :: iodata
131-
def float(float) do
132-
:io_lib_format.fwrite_g(float)
139+
if has_short_format do
140+
def float(float) do
141+
:erlang.float_to_binary(float, [:short])
142+
end
143+
else
144+
def float(float) do
145+
:io_lib_format.fwrite_g(float)
146+
end
133147
end
134148

135149
@spec list(list, opts) :: iodata

0 commit comments

Comments
 (0)