Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing "Streaming" JSON

I have a grid in a browser.

I want to send rows of data to the grid via JSON, but the browser should continuously parse the JSON as it receives it and add rows to the grid as they are parsed. Put another way, the rows shouldn't be added to the grid all at once after the entire JSON object is received -- they should be added as they are received.

Is this possible? Particularly using jQuery, Jackson, and Spring 3 MVC?

Does this idea have a name? I only see bits of this idea sparsely documented online.

like image 564
Donald Taylor Avatar asked Jun 07 '12 14:06

Donald Taylor


3 Answers

You can use Oboe.js which was built exactly for this use case.

Oboe.js is an open source Javascript library for loading JSON using streaming, combining the convenience of DOM with the speed and fluidity of SAX.

It can parse any JSON as a stream, is small enough to be a micro-library, doesn’t have dependencies, and doesn’t care which other libraries you need it to speak to.

like image 105
str Avatar answered Sep 24 '22 02:09

str


Lazy.js is able to parse "streaming" JSON (demo).

like image 29
Donald Taylor Avatar answered Sep 24 '22 02:09

Donald Taylor


You can't parse incomplete or invalid JSON using the browser's JSON.parse. If you are streaming text, it will invariably try and parse invalid JSON at some point which will cause it to fail. There exists streaming JSON parsers out there, you might be able to find something to suit your needs.

Easiest way in your case would remain to send complete JSON documents for each row.

like image 32
Alex Turpin Avatar answered Sep 21 '22 02:09

Alex Turpin