jsoniter-scala icon indicating copy to clipboard operation
jsoniter-scala copied to clipboard

Is there a faster way to go from json bytes to formatted output?

Open mjsmith707 opened this issue 1 year ago • 4 comments

I have an interesting usecase where I'm deserializing a JSONB column from Postgres into a class object. In most cases however I'm just sending these as a CRUD API output over http. So it would be faster if I could skip the deserialization step and just go straight to writing the bytes instead. As the bytes are already known to be valid json (via postgres), then reparsing them isn't really needed, rather, just having the output formatted correctly is the important part. I've also experimented with just deserializing to Circe's JSON and reserializing that and it was slower than just using the class's codec.

I wrote a lazy codec (contains either type T or an Array[Byte]) and then in that I wrote a utility to scan across the bytes and then call JsonWriter's methods like out.writeVal or out.writeObjectStart() etc. That all works but it's pretty slow versus the serialization generated by jsoniter (7s vs 14s on my machine in 10m iterations). It is however, still faster than deserializing to the class then reserializing again (23s vs 14s). The CharArrayJsoniterWriter was kind of hacked up so there's probably bugs/room for improvements.

Any thoughts on this? It would be great if JsonWriter had an out.writeFormattedBytes method :sweat_smile:

Here's my char writer and LazyJson class https://gist.github.com/mjsmith707/9bdf76091da4bd324308b70e9638e5a8

mjsmith707 avatar Sep 23 '24 20:09 mjsmith707