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
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.
/* 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"
}
Indent: 0
{"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" }
Comments