Configure Jason/Postgrex to decode maps with keys as atom by default

I’m using Jason to store some maps in Postgresql. I much prefer the atom syntax for defining and accessing keys and I want to avoid half-string half-atom mixed style maps if possible.

I can see that Jason.decode has an option keys: :atoms, but I can’t for the life of me find anywhere to configure it as a default option with Phoenix/etc. I’ve tried looking around Jason, Ecto and Postgrex’s source.

I’m aware that atoms aren’t garbage collected but I’m not taking key names from users so I’m not worried about uncontrolled memory usage.

You could always implement your own Ecto.Type based on the map field. I doubt ecto itself will ever allow you to set that.

Edit: A Postgrex.Type directly might work as well.

2 Likes

Hmm that’s a bummer. Do you think Ecto will shy away from letting you do it because of the potential for memory usage or spaces in keys, or is it considered bad practice in another way?

A potential DOS attack vector is probably reason enough to not allow it.

2 Likes

If you know beforehand what will that map contain then you can use embeds_one/embeds_many with embedded_schema instead.

4 Likes

Ah, embedded_schema is a great solution to my problem.