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

Add bigint_to_string option to encode big integer to string #189

Open
wants to merge 7 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ iex> Poison.encode!(%{:foo => "foo1", "foo" => "foo2"}, strict_keys: true)
** (Poison.EncodeError) duplicate key found: :foo
```

### 64bit Integers

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})
"\"4294967296\""
```

## Benchmarking

```sh-session
Expand Down
13 changes: 10 additions & 3 deletions lib/poison/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ defprotocol Poison.Encoder do
@typep indent :: non_neg_integer
@typep offset :: non_neg_integer
@typep strict_keys :: boolean
@typep bigint_to_string :: boolean

@type options :: %{
optional(:escape) => escape,
optional(:pretty) => pretty,
optional(:indent) => indent,
optional(:offset) => offset,
optional(:strict_keys) => strict_keys
optional(:strict_keys) => strict_keys,
optional(:bigint_to_string) => bigint_to_string
}

@spec encode(t, options) :: iodata
Expand Down Expand Up @@ -221,8 +223,13 @@ 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 Map.get(options, :bigint_to_string, false) &&
(integer > 0xFFFFFFFF || integer < -0x80000000) do
integer |> Integer.to_string() |> Poison.Encoder.encode(options)
else
Integer.to_string(integer)
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/poison/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ defmodule Poison.EncoderTest do
assert to_json(42) == "42"
end

test "BigInteger to string" do
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}")
end

test "Float" do
assert to_json(99.99) == "99.99"
assert to_json(9.9e100) == "9.9e100"
Expand Down