BDE 4.14.0 Production release
|
Provide a formatter for encoding data in the JSON format.
This component provides a class, baljsn::Formatter
, for formatting JSON objects, arrays, and name-value pairs in the JSON encoding format to a specified output stream.
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:
Formatter
class
allows encoding objects by providing the openObject
and closeObject
methods to open and close an object and the openMember
, closeMember
, and putValue
methods to add members and values to an object.Formatter
class
provides the openArray
and closeArray
method to open and close an array. Additionally the Formatter
class
allows of separation of array items by a comma via the addArrayElementSeparator
method.The Formatter
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.
The Formatter
class
does only minimal checking to verify that the sequence of operations called on its object result in a valid JSON document. It is the user's responsibility to ensure that the methods provided by this component are called in the right order.
This section illustrates intended use of this component.
Let us say that we have to encode a JSON document with the following information about stocks that we are interested in. For brevity we just show and encode a part of the complete document.
First, we specify the result that we are expecting to get:
Then, to encode this JSON document we create a baljsn::Formatter
object. Since we want the document to be written in a pretty, easy to understand format we will specify the true
for the usePrettyStyle
option and provide an appropriate initial indent level and spaces per level values:
Next, we start calling the sequence of methods requires to produce this document. We start with the top level object and add an element named Stocks
to it:
Then, we see that Stocks
is an array element so we specify the start of the array:
Next, each element within Stocks
is an object that contains the information for an individual stock. So we have to output an object here:
We now encode the other elements in the stock object. The closeMember
terminates the element by adding a ,
at the end. For the last element in an object do not call the closeMember
method.
Then, close the first stock object and separate it from the second one using the addArrayElementSeparator
method.
Next, we add another stock object. But we don't need to separate it as it is the last one.
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: