Skip to content

Arduino library for parsing potentially huge json streams on devices with scarce memory

License

Notifications You must be signed in to change notification settings

mrcodetastic/json-streaming-parser2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Streaming Parser 2

Arduino library for parsing potentially huge json streams on devices with scarce memory. Parses a HTTP stream character by character... but requires you to write a customer FSM (finite state machine) for each particular JSON document.

I suggest using ArduinoJson where possible.

This library is a fork of squix78's json-streaming-parser, which itself is a port of Salsify's PHP based json streaming parser (https://github.com/salsify/jsonstreamingparser).

Furthermore, this fork implements significant improvements by stechio departing from squix78's original library to introduce:

  • Explicit element path tracking (ElementPath class): object keys and array indices are managed in a robust, unified manner and exposed in each and every event (users are relieved the pain to jury-rig their own custom event-filtering mechanism)
  • String flattening: adhering to common best practises, cumbersome instances of std::string have been replaced by plain C-style char arrays.
  • Strongly-typed values: untypified (string) value event method has been replaced by a strongly-typed one corresponding to actual serialization types.

Finally, a couple of useful real life examples have been provided to get you started.

How to install

Until the library becomes available in the Arduino IDE library manager you'll have to do a bit more work by hand.

  1. Download this library: https://github.com/mrfaptastic/json-streaming-parser2/archive/master.zip
  2. Rename master.zip to json-streaming-parser2.zip
  3. Open the zip file in the Arduino IDE from menu Sketch > Include Library > Add ZIP Library...

How to use

This is a streaming parser, which means that you feed a stream of chars into the parser and you take out from that stream whatever you are interested in. In order to do that you will create a subclass of JsonHandler class and implement methods which will be notified in case of certain events in the feed occure. Available events are:

  • startDocument()
  • endDocument()
  • startArray(ElementPath path)
  • endArray(ElementPath path)
  • startObject(ElementPath path)
  • endObject(ElementPath path)
  • value(ElementPath path, ElementValue value)

In your implementation of these methods you will have to write problem specific code to find the parts of the document that you are interested in. Please see the example to understand what that means. In the example the ExampleHandler implements the event methods declared in the JsonHandler interface and prints to the serial console when they are called.

Why a streaming parser?

Generally speaking when parsing data you have two options to make sense of this data: you either create a document object model (DOM) which contains the whole information of that document and lets you retrieve the nodes with random access. A DOM parser needs the whole document to start parsing and only lets you access the data after it has finished the process. The other option you have when parsing a document is to process it char by char (or byte by byte) while it becomes available to your client code. Compared to the DOM parser this has two advantages: a) you can react the data as soon as meaningful parts are available and b) you can drop information as soon as the parser has processed it. This reduces the memory consumption a lot, especially if you retrieve huge documents when only being interested by a small subset of it. But this efficiency comes at a price: your code will have to do more "magic" than with a DOM parser, the business logic becomes part of the parser.

License

Do whatever you want. I don't care.

Credits

squix78 for the original Arduino implementation: https://github.com/squix78/json-streaming-parser

Stechio for enhancements: https://github.com/stechio/json-streaming-parser

About

Arduino library for parsing potentially huge json streams on devices with scarce memory

Resources

License

Stars

Watchers

Forks

Languages

  • C++ 100.0%