C++ Poco::JSON::Object::stringify()














































C++ Poco::JSON::Object::stringify()



Description

POCO C++ libraries are used in the development of network-centric, portable applications in C++. Poco::JSON::Object class facilitates using JSON object. Check this article to know more about the Object class- C++ Poco::JSON::Object. This article illustrates the use of the stringify() member function of the Object class. The stringify() function of the Object class facilitates the printing of the JSON object. 

Library: JSON

Package: JSON

Header: Poco/JSON/Object.h


Poco::JSON::Object::stringify()

In the previous article, we printed the JSON object by first converting it to Dynamic::Var type and thereby utilizing the toString() function to convert it to std::string type. Here check out the previous article - C++ Poco::JSON::Object::set(). But here we have a member function of the Object class to simplify our task! The stringify function of the Object class facilitates the printing of Object. In this article, we will learn how to use the stringify() function. 


Prototype:  void stringify (std::ostream & out, unsigned int indent = 0, int step = -1); 

Let's look at the parameters passed to this function.

  • out - It is of type std::ostream. This specifies the out stream to print the object.
  • indent - It is of type unsigned int and specifies the indent size used for printing. The default value of indent is zero in which the object is printed in a single line without any indentation.
  • step - It is of type int. The indentation can be increased/decreased using the number of spaces defined in step. The default value is -1 which indicates that the step size is equal to the indent size.
The following program illustrates the stringify function.

/* Including required headers */
#include "Poco/JSON/Object.h"
#include <iostream>

int main()
{
Poco::JSON::Object object; // creating an empty object
Poco::JSON::Object address(Poco::JSON_PRESERVE_KEY_ORDER); // creating object to preserve insertion order

/* Setting the address */
address.set("DoorNumber", 34);
address.set("Area", "Ram Nagar");
address.set("City", "Mumbai");
address.set("State", "Maharashtra");

/* Setting the object */
object.set("Name", "Abraham");
object.set("Age", 25);
object.set("Address", address);

/* Printing using stringify */
std::cout<<"Indent: 0\n";
object.stringify(std::cout);
std::cout<<std::endl;

std::cout<<"\nIndent: 2\n";
object.stringify(std::cout, 2);
std::cout<<std::endl;

std::cout<<"\nIndent: 2, step: 0\n";
object.stringify(std::cout, 2, 0);
std::cout<<std::endl;

std::cout<<"\nIndent: 2, 5\n";
object.stringify(std::cout, 2, 5);
std::cout<<std::endl;

return 0;
}

Output:

Indent: 0
{"Address":{"DoorNumber":34,"Area":"Ram Nagar","City":"Mumbai","State":"Maharashtra"},"Age":25,"Name":"Abraham"}

Indent: 2
{
"Address" : {
"DoorNumber" : 34,
"Area" : "Ram Nagar",
"City" : "Mumbai",
"State" : "Maharashtra"
},
"Age" : 25,
"Name" : "Abraham"
}

Indent: 2, step: 0
{
"Address" : {
"DoorNumber" : 34, "Area" : "Ram Nagar", "City" : "Mumbai", "State" : "Maharashtra" }, "Age" : 25, "Name" : "Abraham" }

Indent: 2, 5
{
"Address" : {
"DoorNumber" : 34,
"Area" : "Ram Nagar",
"City" : "Mumbai",
"State" : "Maharashtra"
},
"Age" : 25,
"Name" : "Abraham"
}

Let's try to understand the output of the program. In the program, we print the object person in the standard output stream using stringify() in four different ways, changing the parameters' values. Let's interpret the output. 

  1. In the first case, we don't pass any parameters, which set the indent to 0, thereby making the step size 0. Hence we see no indentation and the string prints in a single line. 

    Indent: 0
    {"Address":{"DoorNumber":34,"Area":"Ram Nagar","City":"Mumbai","State":"Maharashtra"},"Age":25,"Name":"Abraham"}


  2. In the second case, we set the indent to 2, and the step takes the default value of -1. Hence the step size is equal to the indent size. Therefore, we can observe an indent of size 2 in the output


  3. In the third case, we set the indent to 2 and specify the step size as 0. Hence we see that the indentation has decreased and the data print in a single line. 

    Indent: 2, step: 0
    {
    "Address" : {
    "DoorNumber" : 34, "Area" : "Ram Nagar", "City" : "Mumbai", "State" : "Maharashtra" }, "Age" : 25, "Name" : "Abraham" }


  4. In the last case, we set the indent to 2 and increase the step size to 5. As a result, we get the following output. 

Hence we observe that stringify() helps us to print the JSON object to the out stream in a formatted way, making the task of printing JSON object simpler.  


References:


Comments