This project is a full-fledged JSON parser that is capable of parsing any .json file following the RFC4627 standard into an easily traversable C++ data structure.
- Parse a JSON file.
auto parser = new Json::Parser();
auto json = parser.parse("path_to_json");- Traverse the returned JSON structure
auto node = json->at("users")->at(0)->at("age");- Convert node to value
int age = node->getAs<int>();- Iterate through a list or an object
for (auto user : json->at("users"))
{
std::string userName = user->at("name")->getAs<std::string>();
}- The
getAs<T>()method only accepts types that can be stored in a JSON node, such types are:bool,int,double,std::string,std::nullptr_t,Json::ListandJson::Object. Json::ListandJson::Objecthide anstd::vectorand anstd::mapofJson::Nodeshared pointers respectively.- Trying to perform an unsupported conversion using the
getAs<T>()function (i.e the user tries to convert a string node toint) throws an exception. - Trying to use the
at(int)function on a non-list node, as well as trying to use theat(std::string)function on a non-object node throws an exception. - The parser will throw an exception if the JSON object found in the provided .json file contains serious formatting errors.