Thursday, June 7, 2012

Streaming JSON Parser

DCStreamingJSONParser off of github So I was talking to a mobile dev dude, who seemed to know his stuff. He told me about how he coded up a JSON parser that parsed as the byte stream was getting downloaded to lower latency. Basically if you start parsing the second you start getting bytes you will only have a smaller subset to parse after the download is complete. Anyhow I decided to take a rough cut at implementing something like this and I ended up doing it in Objective C++ because NSString append is way too slow in a tight loop. I ended up using c arrays and cstrings for most of the heavy lifting. For really large JSON data the streaming parser actually seems faster than the vanilla NSJSONSerialization. In parse only time the iOS implementation is still faster however, since I get a head start, the total time between downloading and the completed parse is smaller for the streaming case. There still are some bugs with escape characters as I haven't covered all the wacky cases yet.

No comments: