Skip to content

Commit

Permalink
allow to map atoms to other values to handle null -> nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Overbryd committed Mar 15, 2018
1 parent 492160e commit 074c64d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/poison/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ defimpl Poison.Encoder, for: Atom do
def encode(true, _), do: "true"
def encode(false, _), do: "false"

def encode(atom, %{force: mapping} = options) do
value = Map.get(mapping, atom, Atom.to_string(atom))
Poison.Encoder.encode(value, Map.delete(options, :force))
end

def encode(atom, options) do
Poison.Encoder.BitString.encode(Atom.to_string(atom), options)
end
Expand Down
2 changes: 2 additions & 0 deletions test/poison/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Poison.EncoderTest do
assert to_json(true) == "true"
assert to_json(false) == "false"
assert to_json(:poison) == ~s("poison")
assert to_json(:null) == ~s("null")
assert to_json(:null, force: %{null: nil}) == "null"
end

test "Integer" do
Expand Down

0 comments on commit 074c64d

Please sign in to comment.