From 2a83c8fa6072fb5062f2a7e59ea37c3e1fef3935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Muska=C5=82a?= Date: Mon, 12 Sep 2022 20:50:23 +0200 Subject: [PATCH] Use float_to_binary [:short] if available --- lib/encode.ex | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/encode.ex b/lib/encode.ex index 854ca1e..3c42dcd 100644 --- a/lib/encode.ex +++ b/lib/encode.ex @@ -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