C++ Poco::JSON Introduction

C++ Poco::JSON Introduction


POCO C++ libraries are used in the development of network-centric, portable applications in C++. Though there are many packages and libraries provided by the POCO community, this is an introductory article to give you an insight into the JSON package provided by the POCO community. JSON is a widely used format covering almost all the applications that we use today.


Before we dig into the library let's quickly know about JSON. JSON(JavaScript Object Notation) is a lightweight, text-based, language-independent data-interchange format. It can be viewed as a syntax for storing and exchanging data. We can convert any JavaScript object to JSON to transmit data to the server; setback JSON at the server to JavaScript object. Being text-based, JSON can be easily transmitted across networks. 

Four simple rules structure the JSON.

  1. Curly braces hold objects.
  2. Data is in name/value pairs.
  3. Data is separated by commas.
  4. Arrays are held in square brackets.
      "Name" : "Sam",
      "Age" : 20,
      "Country" : "India"

Now let's move to our main topic after that brief digression.


Poco::JSON provides us with various classes and functions to facilitate and simply programming with JSON in C++. Let's explore the classes of Poco::JSON and know their uses in brief.

The following table gives us a brief description of the classes. 

Class Description
Array Represents a JSON array. Array provides a representation based on shared pointers and optimized for performance. Facilitates us to perform various operations on JSON array
Handler Interface for handling parsing events generated by the JSON Parser.
Allows us to handle exceptions
Object Represents a JSON object. The object provides a representation based on shared pointers and is optimized for performance. Provides various methods to work with the JSON objects
ParseHandler ParseHandler is the default handler for the JSON Parser. This handler will construct an Object or Array based on the handlers called by the Parser.
Parser A Parser for reading JSON from strings or streams.
PrintHandler Allows us to print the JSON object in formatted to either user-provided std::ostream or standard output. The output is a condensed JSON string if intend is zero; otherwise, the proper indentation is applied to elements.
Query Used to search for a value in a JSON object or array.
Stringifier Helper class for creating a string from a JSON object or array.
Template A template engine that uses JSON as input for generating output. There are commands for looping over JSON arrays, include other templates, conditional output, etc. All text is sent to the output stream.
TemplateCache Use to cache parsed templates. Templates are stored in a map with the full path as a key. When a template file has changed, the cache will remove the old template from the cache and load a new one.

So Poco::JSON provides us methods and classes to create, modify, parse, etc. a JSON object. It simplifies programming with JSON in C++. The above-mentioned table briefs about the various classes provided by Poco::JSON.