Properly calculate serialized JSON length
Move JSON serialization boilerplate to its own function
Sprinkle UNLIKELY() macros to error paths
Internal error should be sent if memory allocation fails
Revise /queries?queries=N bounds checking to be according to the rules