tags:

views:

2227

answers:

7

Is DOM the only way to parse JSON.

+5  A: 

JSON is JavaScript syntax... it has nothing to do with XML and DOM.

If you wanted to "stream" it... I guess you could transfer chunks of valid JSON across the line, and evaluate them one at a time.

Maybe if you wrote a little more about the problem you are trying to solve, you might get a better answer.

bobwienholt
+2  A: 

In reply to your 2nd question, no, many languages have JSON parsers. PHP, Java, C, Ruby and many others. Just Google for the language of your choice plus "JSON parser".

Ryan Doherty
+5  A: 

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 build 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 - I plan to publish mine to my website (www.SoftwareMonkey.org) but, only "sometime in the next month or so".

EDIT: I have posted the parser to my site for you to download; note that the page is not yet complete, so it is not linked elsewhere on the site. However, a working compilable class and a complete example are there.

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

Software Monkey
Your link (where you posted the parser) points to localhost, so it's not accessible from here. It should be http://www.softwaremonkey.org/Code/JsonParser
Matthew Crumley
@Mathew - Thanks; I was testing my local version before deploying the website update and copy/pasted from the wrong browser - oops. Now corrected.
Software Monkey
+3  A: 

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

(in addition to monkey parser referred to by the other entry)

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.

StaxMan
+1  A: 

LitJSON supports a streaming-style API. Quoting from the manual:

"An alternative interface to handling JSON data that might be familiar to some developers is through classes that make it possible to read and write data in a stream-like fashion. These classes are JsonReader and JsonWriter.

"These two types are in fact the foundation of this library, and the JsonMapper type is built on top of them, so in a way, the developer can think of the reader and writer classes as the low-level programming interface for LitJSON."

Vulcan Eager
+1  A: 

Answering the question title: YAJL a JSON parser library in C:

YAJL remembers all state required to support restarting parsing. This allows parsing to occur incrementally as data is read off a disk or network.

So I guess using yajl to parse JSON can be considered as processing stream of data.

stefanB
A: 

As stefanB mentioned, http://lloyd.github.com/yajl/ is a C library for stream parsing JSON. There are also many wrappers mentioned on that page for other languages:

  • yajl-ruby - ruby bindings for YAJL
  • yajl-objc - Objective-C bindings for YAJL
  • YAJL IO bindings (for the IO language)
  • Python bindings come in two flavors, py-yajl OR yajl-py
  • yajl-js - node.js bindings (mirrored to github).
  • lua-yajl - lua bindings
  • ooc-yajl - ooc bindings
  • yajl-tcl - tcl bindings

some of them may not allow streaming, but many of them certainly do.

pykler