|
BDE 4.14.0 Production release
|
Provide a simple formatter for encoding data in the JSON format.
This component provides a class, baljsn::SimpleFormatter, for rendering JSON conforming text for objects, arrays, and various scalar types.
This component provides an interface that is easier to use, and renders more readable "pretty" JSON, than baljsn::Formatter. Clients are encouraged to use baljsn::SimpleFormatter instead of baljsn::Formatter (see {Comparison to baljsn::Formatter}).
The SimpleFormatter class also provides the ability to specify formatting options at construction. The options that can be provided include the encoding style (compact or pretty), the initial indentation level and spaces per level if encoding in the pretty format.
Here is the side-by-side sequence of calls to create the following JSON using both components, assuming an existing stream os:
Some extra indentation has been added in these examples to show the various open/close call nesting levels.
The JSON encoding format (see http://json.org or ECMA-404 standard for more information) specifies a self-describing and simple syntax that is built on two structures:
SimpleFormatter class allows encoding objects by providing the openObject and closeObject methods to open and close an object and overloads for openObject, openArray, addValue and addNullValue which take a name to specify the named fields in the object, or the use of the addMemberName manipulator followed by the overloads of openObject, openArray, addValue, and addNullValue which do not take a name.SimpleFormatter class provides the openArray and closeArray method to open and close an array, as well as overloads for openObject, openArray, addValue and addNullValue which do not take a name for array elements.The SimpleFormatter class provides the ability to specify formatting options at construction. The options that can be provided include the encoding style (compact or pretty), the initial indentation level and spaces per level if encoding in the pretty format.
This section illustrates intended use of this component.
Let us suppose we have to encode a JSON document containing information about a small portfolio of stocks. The eventual data we want to encode is represented by the following JSON string (which is the expected output of the encoding process):
First, we specify the result that we are expecting to get:
Then, to encode this JSON document we create a baljsn::SimpleFormatter object. Since we want the document to be written in a pretty, easy to understand format we will specify true for the usePrettyStyle option and provide an appropriate initial indent level and spaces per level values:
Next, we encode the start of the top level object, and open the first member "Stocks" (which holds an array of stock information):
Next, we render each element within the array of "Stocks" as an object that contains information for an individual stock:
We now encode the other elements in the stock object.
Then, close the first stock object.
Next, we add another stock object.
Similarly, we can continue to format the rest of the document. For the purpose of this usage example we will complete this document.
Once the formatting is complete the written data can be viewed from the stream passed to the formatter at construction.
Finally, verify the received result:
Let us say we want to encode an array of various values.
First, we create our formatter as we did above:
Then we open our array.
Next, we populate the array with a series of unnamed values. Named values are only used in objects, not arrays.
Then, we demonstrate that arrays can be nested, opening another level of array, populating it, and closing it:
Arrays can also contain (unnamed) objects:
Next, we add (named) values to our object:
Then we close the nested object:
Finally, we close the outer array: