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

camel case map key formatting #151

Closed
wants to merge 3 commits into from
Closed
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
64 changes: 52 additions & 12 deletions lib/poison/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ defmodule Poison.Encode do
alias Poison.EncodeError
alias String.Chars

@compile {:inline, encode_name: 1}
@compile {:inline, encode_name: 2}

# Fast path encoding string keys
defp encode_name(value) when is_binary(value) do
defp encode_name(value, :default) when is_binary(value) do
value
end

defp encode_name(value) do
defp encode_name(value, :default) do
case Chars.impl_for(value) do
nil ->
raise EncodeError,
Expand All @@ -41,6 +41,26 @@ defmodule Poison.Encode do
impl.to_string(value)
end
end

defp encode_name(value, :camel_case) when is_binary(value) do
value
|> String.split("_", trim: true)
|> camelize(:first)
|> Enum.join()
end

defp encode_name(value, :camel_case) do
value
|> encode_name(:default)
|> encode_name(:camel_case)
end

defp camelize([], _), do: []

defp camelize([h | t], :first),
do: [String.downcase(h) | camelize(t, :rest)]

defp camelize([h | t], _), do: [String.capitalize(h), camelize(t, :rest)]
end
end
end
Expand Down Expand Up @@ -86,13 +106,15 @@ defprotocol Poison.Encoder do
@typep indent :: non_neg_integer
@typep offset :: non_neg_integer
@typep strict_keys :: boolean
@typep format_keys :: :default | :camel_case

@type options :: %{
optional(:escape) => escape,
optional(:pretty) => pretty,
optional(:indent) => indent,
optional(:offset) => offset,
optional(:strict_keys) => strict_keys
optional(:strict_keys) => strict_keys,
optional(:format_keys) => format_keys
}

@spec encode(t, options) :: iodata
Expand Down Expand Up @@ -243,20 +265,24 @@ defimpl Poison.Encoder, for: Map do

def encode(map, options) do
map
|> strict_keys(Map.get(options, :strict_keys, false))
|> strict_keys(
Map.get(options, :strict_keys, false),
options[:format_keys] || :default
)
|> encode(pretty(options), options)
end

def encode(map, true, options) do
indent = indent(options)
offset = offset(options) + indent
options = offset(options, offset)
key_formatter = options[:format_keys] || :default

fun =
&[
",\n",
spaces(offset),
Encoder.BitString.encode(encode_name(&1), options),
Encoder.BitString.encode(encode_name(&1, key_formatter), options),
": ",
Encoder.encode(:maps.get(&1, map), options) | &2
]
Expand All @@ -271,25 +297,25 @@ defimpl Poison.Encoder, for: Map do
end

def encode(map, _, options) do
key_formatter = options[:format_keys] || :default

fun =
&[
?,,
Encoder.BitString.encode(encode_name(&1), options),
Encoder.BitString.encode(encode_name(&1, key_formatter), options),
?:,
Encoder.encode(:maps.get(&1, map), options) | &2
]

[?{, tl(:lists.foldl(fun, [], :maps.keys(map))), ?}]
end

defp strict_keys(map, false), do: map

defp strict_keys(map, true) do
defp strict_keys(map, false, _), do: map
defp strict_keys(map, true, :default) do
map
|> Map.keys()
|> Enum.each(fn key ->
name = encode_name(key)

name = encode_name(key, :default)
if Map.has_key?(map, name) do
raise EncodeError,
value: name,
Expand All @@ -299,6 +325,20 @@ defimpl Poison.Encoder, for: Map do

map
end

defp strict_keys(map, true, :camel_case) do
map
|> Map.keys
|> Enum.reduce(%{}, fn (key, acc) ->
name = encode_name(key, :camel_case)
if Map.has_key?(acc, name) do
raise EncodeError, value: name,
message: "duplicate key found: #{inspect(key)}"
end
Map.put(acc, name, :ok)
end)
map
end
end

defimpl Poison.Encoder, for: List do
Expand Down
18 changes: 18 additions & 0 deletions test/poison/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ defmodule Poison.EncoderTest do
end
end

test "camelCase" do
camel_case = [format_keys: :camel_case]

assert to_json(%{one_two: true}, camel_case) == ~s({"oneTwo":true})
assert to_json(%{One_two_three: true}, camel_case) == ~s({"oneTwoThree":true})
assert to_json(%{"one_two" => true}, camel_case) == ~s({"oneTwo":true})

camel_and_strict = camel_case ++ [strict_keys: true]

assert_raise Poison.EncodeError, ~r/duplicate key found/, fn ->
to_json(%{:one_two => true, "one_two" => 4}, camel_and_strict)
end

assert_raise Poison.EncodeError, ~r/duplicate key found/, fn ->
to_json(%{:one_two => true, "_one_two_" => 4}, camel_and_strict)
end
end

defp to_json(value, options \\ []) do
value
|> Encoder.encode(Map.new(options))
Expand Down