A library to use JSON objects in C++.
This library supports all the basic JSON types: null, boolean, number, string, array and object.
The types used in this library to store the datas are defined in the src/TypesDef.hpp
file as follows:
null -> nullptr
boolean -> bool
integer -> int
float -> float
string -> std::string
array -> std::vector<json>
object -> std::map<std::string, json>
To test the library, you can first navigate to the tests directory:
cd tests
Then, you can build the project with:
mkdir build
cd build
cmake ..
make
cd ..
Finally, you can run the tests with:
./build/my_app
If you want to modify the tests to test your own code or view some examples, you can edit the main.cpp
file.
The CMakelists.txt file in the tests/
directory is a great example of how to compile the library in your own project.
Here are some examples of how to use the library.
For all the examples, you first need to include the library:
#include <json/json.hpp>
using namespace jsoncpp;
json test;
By default, the json object is set to null.
json test = true;
// or
json test = 42;
// or
json test = 42.42;
// or
json test = "Hello World!";
If you want to set it to an empty object, you can do:
json test = Object();
Or to an empty array:
json test = Array();
test["key1"] = "value1";
test["key2"] = "value2";
test["key3"] = "value3";
test.push("value1");
test.push("value2");
test.push("value3");
json test = json::parse(R"(
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
)");
std::ifstream input("test.json");
json test = json::parse(input);
std::ofstream output("test_write.json");
output << test;
Result:
{"key1": "value1","key2": "value2","key3": "value3"}
Or with indentation:
std::ofstream output("test_write.json");
output << test.toString(4);
Result:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
std::string str = test.toString();
Or with indentation:
std::string str = test.toString(4);
bool &value = test;
// or
int &value = test;
// or
float &value = test;
// or
std::string &value = test;
// or
std::vector<json> &value = test;
// or
std::map<std::string, json> &value = test;
bool &value = test["key1"];
// or
int &value = test["key1"];
// or
float &value = test["key1"];
// or
std::string &value = test["key1"];
// or
std::vector<json> &value = test["key1"];
// or
std::map<std::string, json> &value = test["key1"];
bool &value = test[0];
// or
int &value = test[0];
// or
float &value = test[0];
// or
std::string &value = test[0];
// or
std::vector<json> &value = test[0];
// or
std::map<std::string, json> &value = test[0];
A lot of methods are available to manipulate JSON objects. Here are some examples:
json test = Object();
test["key1"] = "value1";
test.insert("key2", "value2");
if (test.contains("key1"))
std::cout << "key1 exists" << std::endl;
test.erase("key1");
test.at("key2") = "value3";
std::cout << test.size() << std::endl;
test.clear();
if (test.empty())
std::cout << "test is empty" << std::endl;
A lot of methods are available to manipulate JSON arrays. Here are some examples:
json test = Array();
test.push("value1");
test.push("value2");
test.insert(1, "value4");
test.erase(1);
test.at(1) = "value5";
test.front() = "front";
test.back() = "back";
std::cout << test.size() << std::endl;
test.clear();
if (test.empty())
std::cout << "test is empty" << std::endl;
This project is licensed under the MIT License - see the LICENSE file for details.