Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert snake_case keyed map to camelCase keyed map in elixir, phoenix before sending stuff as JSON

I want to change keys of maps in elixir from snake case to camel case before sending stuff as JSON. How can I do that? Should it be just a function in which I will wrap every response or should it be done on some lower level, i.e. in Poison?

Thanks

like image 847
almeynman Avatar asked Jun 17 '16 11:06

almeynman


1 Answers

Many don't know that this is built-into Elixir:

iex> Macro.underscore "SAPExample"
"sap_example"

iex> Macro.camelize "sap_example"
"SapExample"

iex> Macro.camelize "hello_10"
"Hello10"

See Macro.underscore/1 docs or the implementation

like image 132
user734320 Avatar answered Oct 27 '22 09:10

user734320