Re:On the complexity of JSON serialization (2020)
by waynesonfire on Sunday January 24, 2021 @07:34PM
(#25897714)
This sounds pretty sweet, do you happen to have any resources handy that dive deeper into these concepts?
Re:On the complexity of JSON serialization (2020)
by dnautics on Sunday January 24, 2021 @07:40PM
(#25897752)
unfortunately it's not super well documented. Some of the concepts are a bit new (~2 yrs or so), and I kind of stumbled into this as a best practice by accident literally in the last two months when I had to do this for work and was like, "oh, this is a thing".
This video helped: https://www.youtube.com/watch?v=k_xDi7zAcNM
For database ingress, I use schemas, and put in aggressive validations in the changesets. For jsonb values, I use embedded_schemas (https://thoughtbot.com/blog/embedding-elixir-structs-in-ecto...), and cast their creation as a normal part of filling in the database object. For egress to a third party that accepts JSON, I use naked embedded_schemas (even better if they can be shared with the database) and use a protocol to build an custom encoder that can for example send the 3rd party a deeply structured JSON from a flat struct. Due to the way protocols work in elixir, when I send it to Oauth, Oauth will (indirectly) call my protocol code.