A C++ Library providing a std::string
parser for JavaScript Object Notation aka JSON.
The STL containers are in favor to represent the Json entities: Object, Array and Value.
C++17 standard is required to compile the library. Also cmake is recommended for building and installation.
Use the default cmake procedure to build and install the library on your system.
git clone https://github.com/ccdMuro/JsonPP.git
cmake -B bld_jsonpp -S JsonPP
cmake --build bld_jsonpp
sudo cmake --install bld_jsonpp
Add these lines to your CMakeLists.txt (maybe adjust the target signature):
find_package(ccdJsonpp 1.0 REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC ccdJsonpp)
Serialisation into std::iostream
build in.
std::cout << jsonObject["userData"] << "\n";
Verify type.
if ( jsonObject["userData"].type() == Ccd::Json::ValueType::Object)
{
// ...
}
Access the object values with the subscript operator[], with the access method or an explicit cast
auto msgId_a = jsonObject["messageID"].toInt();
auto msgId_b = static_cast<int>(jsonObject["messageID"]);
Write/Assign values.
jsonObject["timeStamp"] = secondsSinceEpoch;
STL container std::vector
is used as Ccd::Json::Array backend.
Write loops as usual.
for ( const auto& attribute : jsonObject["attributes"].toArray() ) {
// ...
}
auto jsonObject = Ccd::Json::Object {};
jsonObject["colors"] = { "red", "green", "blue" };
jsonObject["user"] = Ccd::Json::Object{
{"name","alice"},
{"age",20},
{"size",1.90},
{"hobbies", { "reading", "rock music" }}
};
auto jsonObject = Ccd::Json::objectFromString(JSONstring);
Some working examples can be found in the example
folder.
This software is licensed under the LGPL v3 license. © 2021 Karl Herbig
This project is currently hosted on GitHub. This is not ideal; GitHub is a proprietary, trade-secret system that is not Free and Open Souce Software (FOSS). We are deeply concerned about using a proprietary system like GitHub to develop our FOSS project.
This project has been migrated to codeberg.org.
We urge you to read about the Give up GitHub campaign from the Software Freedom Conservancy to understand some of the reasons why GitHub is not a good place to host FOSS projects.
Any use of this project's code by GitHub Copilot, past or present, is done without our permission. We do not consent to GitHub's use of this project's code in Copilot.