C++ is a versatile language that has a set of libraries to deal with JSON data. JavaScript Object Notation (JSON) is a format for data interchange that is easy for humans to write and read and machines can easily generate and parse. It is used for the transmission of data between web applications and servers, and for storing data, as well as the configuration of files.
How to Read and Write JSON Files in C++
JSON is a data format that is a preferred way of storing and transferring structured data on the web. It is lightweight, which makes the transfer of data easy and results in improved efficiency and processing of data transfer. In C++ rapidjson is a library that has functions to parse and generate the JSON. This library not only enables one to read and write the JSON data but also allows the manipulation and validation of objects of JSON. It also supports functions for handling large datasets of JSON in a streaming fashion.
To read and write the JSON data using rapidjson, the rapidjson::Document class is used. This class provides a high-level API for generating and parsing JSON data from a file and makes manipulation of data possible too.
Example to Read Data from JSON File in C++
This is an example code to read data from a JSON file in C++ using the rapidjson library:
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include <fstream>
#include <iostream>
using namespace rapidjson;
int main()
{
// Open the file for reading
FILE* fp = fopen("example.json", "r");
// Use a FileReadStream to
// read the data from the file
char readBuffer[65536];
rapidjson::FileReadStream is(fp, readBuffer,
sizeof(readBuffer));
// Parse the JSON data
// using a Document object
rapidjson::Document d;
d.ParseStream(is);
// Close the file
fclose(fp);
// Access the data in the JSON document
std::cout << d["Name"].GetString() << std::endl;
std::cout << d["Age"].GetInt() << std::endl;
return 0;
}
The fopen() function is used to open the file. The header file <<#include “rapidjson/filereadstream.h”>> reads the data of the file in a string which is named as json. The document doc is created to hold the data of the file. The string is parsed, and its errors are checked, if errors are found it returns an error otherwise the Document is used to access the data of the JSON.
Example to Write Data to JSON File in C++
The rapidjson library can be used to write the data in JSON files by creating a Document class. This is an example code to write data to the JSON file in C++:
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/writer.h"
using namespace rapidjson;
int main()
{
// Read JSON file
FILE* fp
= fopen("example.json", "rb");
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);
fclose(fp);
Value& s = d["Name"];
s.SetString("Simon", d.GetAllocator());
// Write JSON file
FILE* fp2 = fopen("example_modified.json",
"wb");
char writeBuffer[65536];
FileWriteStream os(fp2, writeBuffer,
sizeof(writeBuffer));
Writer<FileWriteStream> writer(os);
d.Accept(writer);
fclose(fp2);
return 0;
}
This program uses the standard header files to include the rapidjson. The Document object holds the data of the JSON file. The name and age of the member is added to the JSON object. An output file stream is created and prepared for writing the data to the file.
Name and age both are written in the file and displayed on the output terminal.
Conclusion
JSON is a data format that makes it possible to reduce the size of data for the transfer and results in improved efficiency and processing of data transfer. To read and write the JSON data using rapidjson, the rapidjson::Document class is used. This class provides a high-level API for generating and parsing JSON data from a file and makes manipulation of data possible too.