A header-only C++14 JSON library
MIT License
rana is a C++ library for reading and writing JavaScript Object Notation (JSON) files with an emphasis on ease-of-use.
To try out the program, there are two options:
git clone https://github.com/tfussell/rana
.To start using rana in your project, just include rana.hpp. All of the objects and functions are in the namespace rana. You don't need to compile or link anything.
Load a JSON file like this:
rana::value json = rana::from_file("file.json");
You can treat this object much like an object in JavaScript:
//Keyed access
int item1 = json["item1"];
bool item2 = json["item2"];
//Indexed access
std::string item3 = json[3];
float item4 = json[4];
If your JSON object contains an array, you can iterate over all of the values:
for(const auto &element : json[5].iter_array())
{
std::cout << element << std::endl;
}
Likewise for an object:
for(const auto &member : json[5].iter_object())
{
std::cout << member.first << " " << member.second << std::endl;
}
You can append an item to your array:
rana::value a = rana::value::array;
a.append(3);
a.append(false);
// Convenience notation for the same
rana::value a = rana::value::array(3)(false);
Finally, stringify your object:
std::cout << rana::to_string(a) << std::endl;
This is assuming we have an example file, file.json, which looks like this:
{
"item1" : 42,
"item2" : false,
"item3" : "this is a string",
"item4" : 3.141592,
"item5" : [0, 1, 2],
}