Development with openABK openABK development resources

Coding and Decoding JSON objects

openABK transports its data with the HTTP protocol. HTTP offers a text-based payload for various mime types. openABK codes its objects in JSON format and then sends this string-like object via HTTP to the recipient. For details see www.json.org. In addition to the JSON-predefined data types, openABK needs to transport date and time objects. These have to be formatted accordingly.

Most of the items are transported in a fixed schema. This allows the ready code to encode C++ data types to JSON and on the receiving side to decode it back into C++ data types. However, a few items are not bound to a schema by openABK and their JSON representation must be handled by your code. For example, a server can optionally provide a lot of diagnostic data within an object. openABK does not specify the schema. If you want to expose such data, you have to encode it into JSON. If you want to read such data on a display you may want to show it on a tree view.

The ready C++ code provides a JSON parser and a JSON encoder.

JSON Encoder

The JSON encoder is based on the std::stringstream object. Each element which is added is fed into the string stream immediately. The encoder takes care of syntactical elements such as commas and braces. Strings of values and member names are escaped by the encoder.

To handle the required structures, three classes are provided:

  • CJsonFormatter is the formatter which can collect values directly. Objects and arrays have their own class to feed them to the formatter
  • CJsonStreamObject builds the context for feeding objects to the formatter.
  • CJsonStreamArray builds the context for feeding array members to the formatter.

JSON Parser

The JSON parser works by iterating through the formatted string. Due to performance reasons, it does not create an object tree as a result. This would require several memory allocation operations and when accessing the members, search operations would be necessary. With this approach, the applying code provides a loop and tests for the expected members. For example, if you want to extract the values "Val1" and "Val2", test for existence of Val1 and then for Val2 in your loop. There is one class providing decoding functionality:

 

 

openABK