Skip to content

Commit 52a95f7

Browse files
committed
allow to map atoms to other values to handle null -> nil
1 parent 492160e commit 52a95f7

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/poison/encoder.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ defimpl Poison.Encoder, for: Atom do
7575
def encode(false, _), do: "false"
7676

7777
def encode(atom, options) do
78-
Poison.Encoder.BitString.encode(Atom.to_string(atom), options)
78+
mapping = Keyword.get(options, :force, %{})
79+
value = Map.get(mapping, atom, Atom.to_string(atom))
80+
Poison.Encoder.encode(value, Keyword.delete(options, :force))
7981
end
82+
8083
end
8184

8285
defimpl Poison.Encoder, for: BitString do

test/poison/encoder_test.exs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule Poison.EncoderTest do
88
assert to_json(true) == "true"
99
assert to_json(false) == "false"
1010
assert to_json(:poison) == ~s("poison")
11+
assert to_json(:null) == ~s("null")
12+
assert to_json(:null, force: %{null: nil}) == "null"
1113
end
1214

1315
test "Integer" do

0 commit comments

Comments
 (0)