Usage information for camelCase outgoing parameters
The Plug for incoming parameters is working fine, but I must be missing something to get Phoenix Controller's json to do the translation to camel case.
I've followed along with the readme, trying both option for format_encoders:
config :phoenix, :json_library, Jason
config :phoenix, :format_encoders, json: ProperCase.JSONEncoder.CamelCase
config :phoenix, :format_encoders, json: MyApp.CustomJSONEncoder
And have this in a controller:
conn
|> json(%{okay_awesome: true})
Output:
{
"okay_awesome": true
}
But I would like to see okayAwesome.
Related: #5
+1 for this one, I have the exact same issue and tried already both cases.
Looks like the issue in my case was due to this rescue clause. I was trying to return an Ecto schema in my view and the ProperCase.to_camel_case/1 simply returns the original map when something when it raises Protocol.UndefinedError, which was happening due to the non-Enumerable Ecto fields. The solution for me was to drop such fields before returning:
def render("my_schema.json", %{my_schema: my_schema}) do
%{
ok: true,
my_schema: my_schema |> Map.from_struct() |> Map.drop([:__meta__, :association_field])
}
end
That's probably an improvement point, at least to log the something in the rescue clause to let people know what's up.
Also experiencing the same problem, using plain maps for what it's worth.
Ah I think I figured my (and likely @nathany's) problem out:
The json controller helper skips the template rendering pipeline entirely. However, Phoenix formatters are implicitly tied to templates: they are looked up based on root extension of a template file. So I imagine the formatter fires fine on render conn, "template.json") for some template.json.eex, but not raw data structures passed to the controller json helper.
For now I'm just calling json(conn, ProperCase.to_camel_case(data)).