Resources | Resources |



JSON Parser

The JSON parser is a builder/parser that allows you to build or parse JSON data. JSON is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays called objects, but is a language-independent data format. The JSON format is often used for serializing and transmitting structured data over a network connection. The JSON parser is a DOM-based parser, which means it must have the entire parsed tree in memory before any processing can begin.

Brew MP provides the following interfaces for the JSON Parser:

  • JSONParser
  • JSONGen
  • IJSONTree

JSON interfaces and their use

JSONGen is a library that provides a set of functions to serialize JSON values to construct JSON text. The API is strictly linear in building JSON, which means that each method appends its data to the end of the JSON string being generated. If the original buffer runs out of space, JSONGen creates a new buffer using the provided allocator function and expands it as necessary. The serialization code inserts separators in appropriate places.

JSONParser is a library that provides a set of functions to parse JSON data. The parsing and accessing functions provide a simple and generic mechanism, which requires no memory allocation. A JSONID refers to a specific value within JSON data and identifies a particular JSONType (or just type) value. The root of JSON data is represented by the special JSONID defined as JSON_ROOT. After the parser is constructed using Ctor(), one of the Lookup() methods can be used to query the nested JSON data. Relevant JSONIDs can then be passed to one of the GetXXX() methods to retrieve their underlying values. A JSONParser can be declared on the stack for quick parsing needs.

The IJSONTree interface functions are used to create, manipulate, query, and serialize JSON objects. IJSONTree works as a DOM type of tree representation of the JSON data. A JSValue represents a node in the tree and is a data structure exposed to the user as an opaque pointer. Each node has an associated type and is one of the listed JSONType values. JSValues are created using one of the CreateXXX(), ParseJSON or CloneValue methods. In general, the tree has a root node (object is most common) and various methods can be used to build the tree.