This example begins by including the necessary includes, including json/json.h, which defines the interface to JsonCpp. We explicitly reference the std namespace for brevity, although don't do so for the Json namespace, in which JsonCpp defines all of its interfaces.
The JsonCpp implementation defines Json::Reader and Json::Writer, specifying the interfaces to JSON readers and writers, respectively. In practice, the Json::Reader interface is also the implementation of a JSON class that can read JSON, returning its values as Json::Value. The Json::Writer variable just defines an interface; you'll want to use a subclass of it such as Json::FastWriter or Json::StyledWriter to create JSON from Json::Value objects.
The previous listing begins by defining Json::Reader and Json::Value; we'll use the reader to read the JSON we define on the next line and store its value in the Json::Value variable root. (Presumably your C++ application would get its JSON from another source, such as a web service or local file.)
Parsing JSON is as simple as calling the reader's parse function, passing the JSON and Json::Value into which it will write the JSON values. It returns a Boolean, which will be true if the JSON parsing succeeds.
The Json::Value class represents the JSON object as a tree; individual values are referenced by the attribute name in the original JSON, and the values are the values of those keys, accessible through methods such as asString, which returns the value of the object as a native C++ type. These methods of Json::Value includes the following:
asString, which returns std::stringasInt, which returns IntasUInt, which returns UIntasInt64, which returns Int64asFloat, which returns floatasDouble, which returns doubleasBool, which returns bool
In addition, the class provides operator[], letting you access array elements.
You can also query a Json::Value object to determine its type using one of these methods:
isNull, which returns true if the value is nullisBool, which returns true if the value is boolisInt, which returns true if the value is IntisUInt, which returns true if the value is UIntisIntegral, which returns true if the value is an integerisDouble, which returns true if the value is doubleisNumeric, which returns true if the value is numericisString, which returns true if the value is a stringisArray, which returns true if the value is an arrayisObject, which returns true if the value is another JSON object (which you can decompose using another Json::Value value)
At any rate, our code uses asString to fetch the std::string value encoded as the result attribute, and writes it to the console.
The code then defines Json::StyledWriter and Json::FastWriter to create some pretty-printed JSON and unformatted JSON in strings, as well as a single Json::Value object to contain our new JSON. Assigning content to the JSON value is simple because it overrides the operator[] and operator[]= methods with the appropriate implementations to convert standard C++ types to JSON objects. So, the following line of code creates a single JSON attribute/value pair with the attribute set to result, and the value set to ok (although this code doesn't show it, you can create trees of JSON attribute-value pairs by assigning JSON objects to other JSON objects):
We first use StyledWriter and then FastWriter to encode the JSON value in newValue, writing each string to the console.
Of course, you can also pass single values to JsonCpp; there's no reason why you can't execute the following code if all you wanted to do was pass a double-precision number: