Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a streaming API for JSON? [closed]

Tags:

json

streaming

Is DOM the only way to parse JSON?

like image 686
kal Avatar asked Jan 14 '09 19:01

kal


People also ask

What is the difference between streaming API and REST API?

The difference between REST APIs and streaming APIs is: Streaming APIs updates are sent to the consumer when an event happens. REST APIs operate in a client-server architecture.

What is a streaming API?

Streaming API is a subscription mechanism based on CometD, which enables real-time streaming of event messages. CometD enables the server to push data to the client when the data is available and while the client maintains a connection to the server.

How does JSON stream work?

Concatenated JSON streaming allows the sender to simply write each JSON object into the stream with no delimiters. It relies on the receiver using a parser that can recognize and emit each JSON object as the terminating character is parsed.

What is application stream JSON?

application/stream+json is for server to server/http client (anything that's not a browser) communications. It won't prefix the data and will just use CRLF to split the pieces of data.


2 Answers

Some JSON parsers do offer incremental ("streaming") parser; for Java, at least following parsers from json.org page offer such an interface:

  • Jackson (pull interface)
  • Json-simple (SAX-style push interface)

(in addition to Software Monkey's parser referred to by another answer)

Actually, it is kind of odd that so many JSON parsers do NOT offer this simple low-level interface -- after all, they already need to implement low-level parsing, so why not expose it.

EDIT (June 2011): Gson too has its own streaming API (with gson 1.6)

like image 196
StaxMan Avatar answered Oct 09 '22 15:10

StaxMan


By DOM, I assume you mean that the parser reads an entire document at once before you can work with it. Note that saying DOM tends to imply XML, these days, but IMO that is not really an accurate inference.

So, in answer to your questions - "Yes", there are streaming API's and "No", DOM is not the only way. That said, processing a JSON document as a stream is often problematic in that many objects are not simple field/value pairs, but contain other objects as values, which you need to parse to process, and this tends to end up a recursive thing. But for simple messages you can do useful things with a stream/event based parser.

I have written a pull-event parser for JSON (it was one class, about 700 lines). But most of the others I have seen are document oriented. One of the layers I have built on top of my parser is a document reader, which took about 30 LOC. I have only ever used my parser in practice as a document loader (for the above reason).

I am sure if you search the net you will find pull and push based parsers for JSON.

EDIT: I have posted the parser to my site for download. A working compilable class and a complete example are included.

EDIT2: You'll also want to look at the JSON website.

like image 43
Lawrence Dol Avatar answered Oct 09 '22 16:10

Lawrence Dol