3 ms·
One place this is really problematic is serializing structures to JSON. I worked on a JS front-end at a company whose back-end was all Lisp, and I cannot count
by brundolf 5y ago
One place this is really problematic is serializing structures to JSON. I worked on a JS front-end at a company whose back-end was all Lisp, and I cannot count the number times the app broke with "TypeError: Cannot read property 'map' of null", because every single list that would normally serialize to an array would serialize to `null` when it was empty unless the person writing the service went out of their way to do otherwise.
At some point I wrote an API-validation utility almost entirely to help manage this problem.
- reikonomusha 5y agoAn alternative solution is don’t serialize from/deserialize to NIL. Use MY-JSON:TRUE, MY-JSON:FALSE, MY-JSON:NULL, etc. to create a perfectly bijective map. Lisp is a symbolic language after all, no reason not to take advantage of it.
- brundolf 5y agoThat would mean writing code for each endpoint to convert its response data into the JSON-explicit format instead of dumping it out directly; this wasn't a big company and we didn't have the man-hours for that extra layer I think when they manually fixed it in individual cases, they did something like `(or the-list json:empty-array)`. We just had to encounter that case in practice and then make a Jira ticket for them to go in and patch over it, and then do that over again each and every time it came up :P The utility I wrote[1] would match JSON responses against expected schemas and report the bad `null` (or otherwise), its location in the JSON, and which service it came from, in the JS console. It didn't stop errors from reaching production but it saved a lot of time debugging. [1] https://github.com/brundonsmith/simple-typechecker https://github.com/brundonsmith/simple-typechecker