From 074c64d91c02ca8d3a7aff05f481f0fa931ec043 Mon Sep 17 00:00:00 2001 From: Lukas Rieder Date: Wed, 20 Dec 2017 16:16:30 +0100 Subject: [PATCH] allow to map atoms to other values to handle null -> nil --- lib/poison/encoder.ex | 5 +++++ test/poison/encoder_test.exs | 2 ++ 2 files changed, 7 insertions(+) diff --git a/lib/poison/encoder.ex b/lib/poison/encoder.ex index 2eefc45b..ef15d4e3 100644 --- a/lib/poison/encoder.ex +++ b/lib/poison/encoder.ex @@ -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 diff --git a/test/poison/encoder_test.exs b/test/poison/encoder_test.exs index c28d1c61..158c1fb2 100644 --- a/test/poison/encoder_test.exs +++ b/test/poison/encoder_test.exs @@ -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