Skip to content

Commit c8c3bd9

Browse files
committed
allow to map atoms to other values to handle null -> nil
1 parent dfcb30c commit c8c3bd9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/poison/encoder.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ defimpl Poison.Encoder, for: Atom do
104104
def encode(false, _), do: "false"
105105

106106
def encode(atom, options) do
107-
Encoder.BitString.encode(Atom.to_string(atom), options)
107+
{mapping, options} = Keyword.pop(options, :force, %{})
108+
case Map.get(mapping, atom) do
109+
nil -> Encoder.BitString.encode(Atom.to_string(atom), options)
110+
value -> Poison.Encoder.encode(value, options)
111+
end
108112
end
113+
109114
end
110115

111116
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)