4 ms·
In Scala (and I assume many other languages), this is exactly how it works in the popular libraries. You design, or automatically derive, the mapping from your
by acjohnson55 6y ago
In Scala (and I assume many other languages), this is exactly how it works in the popular libraries. You design, or automatically derive, the mapping from your domain objects and a JSON AST. As the author mentions, the mapping between the AST and the string encoding is something you can consider to be a black box, unless you want to change settings like compact vs pretty printing.
To me, the most interesting question is what the author alluded on how to manage the coupling between arbitrary business objects and JSON AST. Writing a ton of boilerplate code is not at all fun. But as he points out, attempting to automate the translation through metaprogramming results in a problematic coupling of internal system details and the external contract.
An idea I like is something more like code generation. Macros where the expansion gets written to disk, so hand edits can be made. There should be automatic verification of any changes to the contract, which can be done by generating an OpenAPI spec and version controlling it. Also, it should be really easy to differentiate between the default generated code and hand edits. There shouldn't be any need to crawl through all of the mapping code if there are far fewer changes relative to the stuff that was autogenerated.