From 73fa8e49761d502c636dac4f3395d2fea768cdf0 Mon Sep 17 00:00:00 2001 From: "yuan.lei" Date: Thu, 25 May 2017 21:47:57 +0900 Subject: [PATCH 1/6] add the `bigint_to_string` option --- README.md | 11 +++++++++++ lib/poison/encoder.ex | 8 ++++++-- test/poison/encoder_test.exs | 5 +++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58dba8d7..338866f5 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,17 @@ iex> Poison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true) ** (Poison.EncodeError) duplicate key found: "foo" ``` +### 64bit Integers + +Some programming languages such as Javascript cannot support 64bit integers. +So sometime 64bit integers should be converted to string type. + +```iex +iex> Poison.encode!(4_294_967_296, bigint_to_string: true) +"4294967296" +``` + + ## Benchmarking ```sh-session diff --git a/lib/poison/encoder.ex b/lib/poison/encoder.ex index 6abb94e6..400437b9 100644 --- a/lib/poison/encoder.ex +++ b/lib/poison/encoder.ex @@ -181,8 +181,12 @@ defimpl Poison.Encoder, for: BitString do end defimpl Poison.Encoder, for: Integer do - def encode(integer, _options) do - Integer.to_string(integer) + def encode(integer, options) do + if options[:bigint_to_string] && (integer > 0xffffffff || integer < -0x80000000) do + Integer.to_string(integer) |> Poison.Encoder.encode(options) + else + Integer.to_string(integer) + end end end diff --git a/test/poison/encoder_test.exs b/test/poison/encoder_test.exs index 2ceb711b..efe92081 100644 --- a/test/poison/encoder_test.exs +++ b/test/poison/encoder_test.exs @@ -14,6 +14,11 @@ defmodule Poison.EncoderTest do assert to_json(42) == "42" end + test "BigInteger to string" do + assert to_json(0xffffffff + 1, bigint_to_string: true) == ~s("#{0xffffffff + 1}") + assert to_json(-0x80000000 - 1, bigint_to_string: true) == ~s("#{-0x80000000 - 1}") + end + test "Float" do assert to_json(99.99) == "99.99" assert to_json(9.9e100) == "9.9e100" From a20855416ddea6d1fdf67ab025bbf8e6e062b330 Mon Sep 17 00:00:00 2001 From: "yuan.lei" Date: Mon, 13 Nov 2017 16:19:22 +0900 Subject: [PATCH 2/6] fix warning --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 6daf69db..f6de05cc 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Poison.Mixfile do use Mix.Project - @version File.read!("VERSION") |> String.strip + @version File.read!("VERSION") |> String.trim def project do [app: :poison, From e52e4a7cb66c446916578687fbc2e94c806875f2 Mon Sep 17 00:00:00 2001 From: fieldinrain Date: Wed, 17 Jul 2019 17:47:28 +0900 Subject: [PATCH 3/6] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 430e0c8a..2f11a2d7 100644 --- a/README.md +++ b/README.md @@ -154,8 +154,8 @@ iex> Poison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true) ### 64bit Integers -Some programming languages such as Javascript which can not support 64bit integers. -So sometime 64bit integers should be converted to string type. +Some programming languages such as Javascript do not support 64bit integers. +You can pass the `bigint_to_string: true` option to convert it to string type. ```iex iex> Poison.encode!(4_294_967_296, %{bigint_to_string: true}) From a77c2ec5e886ebd2cb6870539f010e7d75823685 Mon Sep 17 00:00:00 2001 From: fieldinrain Date: Wed, 17 Jul 2019 18:03:07 +0900 Subject: [PATCH 4/6] fix format --- lib/poison/encoder.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/poison/encoder.ex b/lib/poison/encoder.ex index 7ed61e00..fa54bc13 100644 --- a/lib/poison/encoder.ex +++ b/lib/poison/encoder.ex @@ -224,8 +224,10 @@ end defimpl Poison.Encoder, for: Integer do def encode(integer, options) do - if Map.get(options, :bigint_to_string, false) && (integer > 0xffffffff || integer < -0x80000000) do - Integer.to_string(integer) |> Poison.Encoder.encode(options) + if Map.get(options, :bigint_to_string, false) && + (integer > 0xFFFFFFFF || integer < -0x80000000) do + Integer.to_string(integer) + |> Poison.Encoder.encode(options) else Integer.to_string(integer) end From 7df1b3b399549f427e93b4ab4402d2b02e496c14 Mon Sep 17 00:00:00 2001 From: fieldinrain Date: Wed, 17 Jul 2019 18:12:18 +0900 Subject: [PATCH 5/6] format the test file --- test/poison/encoder_test.exs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/poison/encoder_test.exs b/test/poison/encoder_test.exs index d399a667..1bee5935 100644 --- a/test/poison/encoder_test.exs +++ b/test/poison/encoder_test.exs @@ -15,10 +15,14 @@ defmodule Poison.EncoderTest do end test "BigInteger to string" do - assert to_json(0xffffffff + 1) == "#{0xffffffff + 1}" + assert to_json(0xFFFFFFFF + 1) == "#{0xFFFFFFFF + 1}" assert to_json(-0x80000000 - 1) == "#{-0x80000000 - 1}" - assert to_json(0xffffffff + 1, bigint_to_string: true) == ~s("#{0xffffffff + 1}") - assert to_json(-0x80000000 - 1, bigint_to_string: true) == ~s("#{-0x80000000 - 1}") + + assert to_json(0xFFFFFFFF + 1, bigint_to_string: true) == + ~s("#{0xFFFFFFFF + 1}") + + assert to_json(-0x80000000 - 1, bigint_to_string: true) == + ~s("#{-0x80000000 - 1}") end test "Float" do From 45e2a1613659ac8b1c60d5ebf291e6dd6383a7ae Mon Sep 17 00:00:00 2001 From: fieldinrain Date: Wed, 17 Jul 2019 18:36:42 +0900 Subject: [PATCH 6/6] fix credo --- lib/poison/encoder.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/poison/encoder.ex b/lib/poison/encoder.ex index fa54bc13..152a109b 100644 --- a/lib/poison/encoder.ex +++ b/lib/poison/encoder.ex @@ -226,8 +226,7 @@ defimpl Poison.Encoder, for: Integer do def encode(integer, options) do if Map.get(options, :bigint_to_string, false) && (integer > 0xFFFFFFFF || integer < -0x80000000) do - Integer.to_string(integer) - |> Poison.Encoder.encode(options) + integer |> Integer.to_string() |> Poison.Encoder.encode(options) else Integer.to_string(integer) end