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

Handle dashed keys, like in jsonapi.org recommendations. #103

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion lib/poison/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ defmodule Poison.Decode do
defp transform_struct(value, keys, as, options) do
Map.from_struct(as)
|> Enum.reduce(%{}, fn {key, default}, acc ->
Map.put(acc, key, Map.get(value, Atom.to_string(key), default))
string_key = if options[:dash] do
key |> Atom.to_string() |> String.replace("_", "-")
else
key |> Atom.to_string()
end
Map.put(acc, key, Map.get(value, string_key, default))
end)
|> do_transform_struct(keys, as, options)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/poison/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ defimpl Poison.Encoder, for: BitString do
def encode("", _), do: "\"\""

def encode(string, options) do
[?", escape(string, options[:escape]), ?"]
[?", dash(string, options[:dash]) |> escape(options[:escape]), ?"]
end

defp dash(string, true) when is_binary(string) do
String.replace(string, "_", "-")
end
defp dash(string, _), do: string

defp escape("", _), do: []

for {char, seq} <- Enum.zip('"\\\n\t\r\f\b', '"\\ntrfb') do
Expand Down
9 changes: 9 additions & 0 deletions test/poison/decoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ defmodule Poison.DecoderTest do
defstruct [:email, :telephone, call_count: 0]
end

defmodule PersonDash do
defstruct [:person_name, :address, :contact, age: 42]
end

defimpl Poison.Decoder, for: Address do
def decode(address, _options) do
"#{address.street}, #{address.city}, #{address.state} #{address.zip}"
Expand All @@ -34,6 +38,11 @@ defmodule Poison.DecoderTest do
assert decode(person, as: %Person{}) == %Person{name: "Devin Torres", age: 27}
end

test "decoding single :as with string-dash keys" do
person = %{"person-name" => "Devin Torres", "age" => 27}
assert decode(person, as: %PersonDash{}, dash: true) == %PersonDash{person_name: "Devin Torres", age: 27}
end

test "decoding single :as with atom keys" do
person = %{name: "Devin Torres", age: 27}
assert decode(person, keys: :atoms!, as: %Person{}) == %Person{name: "Devin Torres", age: 27}
Expand Down
1 change: 1 addition & 0 deletions test/poison/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Poison.EncoderTest do
assert to_json("\u2028\u2029", escape: :javascript) == ~s("\\u2028\\u2029")
assert to_json("</script>", escape: :html_safe) == ~s("<\\/script>")
assert to_json("áéíóúàèìòùâêîôûãẽĩõũ") == ~s("áéíóúàèìòùâêîôûãẽĩõũ")
assert to_json("hello_world", dash: true) == ~s("hello-world")
end

test "Map" do
Expand Down