Search code examples
javajackson-databind

Does Jackson require complete input?


I'm doing a little bit of tinkering for fun with Jackson and I stumbled across the following. Assume you're working in a system which is operating on a large amount of files. The desire is to keep memory consumption low and read those files in a non-blocking asynchronous manner.

So, we create a ByteBuffer with a low amount of bytes allocated (say 1024) and a AsynchronousFileChannel for each file (which are > 1024 bytes), then read from the buffers. Ideally we start parsing as soon as a buffer's Future has completed so we're not waiting on anything then, clear the buffer and continue parsing the file.

Now to the best of my knowledge this is not possible because Jackson will throw an Exception because it needs "complete" input.

However, since I'm not sure, is there a way around this? Or, does Jackson always require complete input before it can start parsing?


Solution

  • Yes, Jackson requires full input. Your way to work around it is to read your file by chunks into a larger buffer on your server side and parse it once the file is fully read. But that negates your low memory footprint requirement. Another possible trick is that if your file contains several Json documents, is to determine when you have a complete doc and parse it than, without waiting for the entire file to be read. But this is more complex solution as you have to do some preliminary parsing to determine that you got the entire Json doc