json library in c++
Single header only c++. Parsing is stream friendly to minimize memory footprint for large json file. Support both char and wide char. Provide a "Value" type to handle underlying json type (null, bool, number, string, array and object) making it very easy manipulation in code.
Parsing is separated in 2 layers, making it very easy to customize and implement json serialization for any rtti library:
read json from string:
std::wstring input = L"{\"name\":\"John\"}";
ejson::Value value;
ejson::Read(input, value);
std::wcout << "value name is : " << value[L"name"].AsString() << std::endl;
value name is : John
write it back to a string:
std::wstring output;
ejson::Write(value, output);
if (input == output)
std::wcout << "input and output are the same: " << std::endl;;
std::wcout << output << std::endl << std::endl;
input and output are the same:
{"name":"John"}
write prettify json to string:
std::wstring prettifyOutput;
ejson::Write(value, prettifyOutput, true);
std::wcout << prettifyOutput << std::endl << std::endl;
{
"name": "John"
}
create json in typed code
Value json;
json[L"FirstName"] = L"John";
json[L"LastName"] = L"Doe";
json[L"Age"] = 71;
json[L"Music"][0] = L"punk";
json[L"Music"][1] = L"country";
json[L"Music"][2] = L"folk";
std::wstring str;
Write(json, str);
std::wcout << str << std::endl << std::endl;
{"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}
std::wifstream input("..\\data\\john_doe.json");
ejson::Value value;
ejson::Read(input, value);
std::wstring output;
ejson::Write(value, output);
std::wcout << output;
{"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}
read from file and write back to another file in pretty format:
// read
std::wifstream input("..\\data\\john_doe.json");
ejson::Value value;
ejson::Read(input, value);
// write
std::wofstream output("..\\data\\john_doe_output.json");
ejson::Write(value, output, true);
john_doe_output.json:
{
"FirstName": "John",
"LastName": "Doe",
"Age": 71,
"Music": [
"punk",
"country",
"folk"
]
}
create json in typed code
ejson::Value json;
json[L"FirstName"] = L"John";
json[L"LastName"] = L"Doe";
json[L"Age"] = 71;
json[L"Music"][0] = L"punk";
json[L"Music"][1] = L"country";
json[L"Music"][2] = L"folk";
std::wstring str;
ejson::Write(json, str);
std::wcout << str << std::endl << std::endl;
{"FirstName":"John","LastName":"Doe","Age":71,"Music":["punk","country","folk"]}
could be build composed:
ejson::Value json;
json[L"FirstName"] = L"John";
json[L"LastName"] = L"Doe";
json[L"Age"] = 71;
ejson::Value& music = json[L"Music"];
music[0] = L"punk";
music[1] = L"country";
music[2] = L"folk";
when loading data from file in read only, to make sure to not change input Value, use const Value& for your queries and make validation like this:
// read
std::wifstream input("..\\data\\john_doe.json");
ejson::Value fileValue;
ejson::Read(input, fileValue);
// use const& here to prevent appending value to fileValue
ejson::Value& value = fileValue;
if (value[L"FirstName"].IsString())
{
std::wstring firstName = value[L"FirstName"].AsString();
//...
}
read with error:
std::wifstream input("..\\data\\john_doe_err.json");
ejson::Value value;
ejson::ParserError error;
if (ejson::Read(input, value, error))
{
// no error ...
}
else
{
// error
std::wcout << "error at line/column " << error.Line << "/" << error.Column << ": " << error.Error;
}
error at line/column 3/6: invalid token
modify configuration at the beginning of ejson.h
#define EJSON_WCHAR 1 // wchar_t (default)
#define EJSON_WCHAR 0 // char
#define EJSON_ASSERT // (default impl use cassert)
#define EJSON_ERROR
using number = double; // (default)
using number = float;
#define EJSON_MAP_ORDERED 1 // (default, keep load/write ordered)
#define EJSON_MAP_ORDERED 0 // (faster, don't keep ordered, ex: suitable for final build that only read)
eti use awesome great unit tests framework: doctest