Development with openABK openABK development resources

CJsonFormatter

class CJsonFormatter : public CJsonStreamObject

With CJsonFormatter you can generate a string representation of an object. The typical procedure is:

  1. Create an instance of CJsonFormatter
  2. Append members
  3. Close it by reading the string-formatted content

This formatter forms the root object in the formatting process. It holds a std::stringstream where the result is built.

Construction

CJsonFormatter::CJsonFormatter ()

Constructs a CJsonFormatter object ready to receive members.

Appending Members

Since the CJsonFormatter is an object, you can add members as described in CJsonStreamObject. These members can be values, objects and arrays.

Getting the Result

std::stringstream *CJsonFormatter::GetStream (void)

When all members are added to the formatter, the result can be read with the GetStream() member. This also closes the formatting - i.e. closing braces will be added. After calling GetStream(), you must not add further members.

Example

CJsonFormatter jfEvent; /* whole event formatted in JSON */
jfEvent.WriteValue("IntValue",(int)1); /* add an integer value */
time_t tmNow;
time(&tmNow); // get actual time
jfEvent.WriteValue("CurrentTime",tmNow); /* add a time value */
jfEvent.WriteValue("String","This is a string value"); /* add a string value */
std::string strFormatted=jfEvent.GetStream()->str(); /* close and read the formatted content */
printf(str.c_str());

In this example, a formatter object is created and then some values are appended. Then the formatter is closed and the content is read out into a std::string object. Finally the string is printed.

 

openABK