Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cppfw/jsondom

C++ library for JSON parsing and DOM
https://github.com/cppfw/jsondom

cpp dom json

Last synced: 7 days ago
JSON representation

C++ library for JSON parsing and DOM

Awesome Lists containing this project

README

        

:name: jsondom

= {name}

|====
| link:https://github.com/cppfw/{name}/releases[image:https://img.shields.io/github/tag/cppfw/{name}.svg[releases]] | link:https://github.com/cppfw/{name}/actions[image:https://github.com/cppfw/{name}/workflows/ci/badge.svg[ci status]]
|====

JSON parser and DOM library in C++.

== Istallation
:package_name: jsondom

. Setup your OS-preferred package system repo following link:https://github.com/cppfw/wiki/blob/main/EnableRepo.adoc[this manual]
. Install package
+
- **deb** (Linux): `lib{package_name}`
- **homebrew** (MacOS X): `lib{package_name}`
- **pacman** (Windows, Msys2): `mingw-w64-i686-{package_name}`, `mingw-w64-x86_64-{package_name}`
- **Nuget** (Windows, Visual Studio): `lib{package_name}`
- **cocoapods** (iOS, xcode): `{package_name}`
- **gradle** (Android): `{package_name}`
- **conan** (multi-OS): `{package_name}`

== Tutorial

[source,cpp]
....
#include
#include

#include

const char* json = R"qwertyuiop(
{
"key1": "value1",
"key2": "value2"
}
)qwertyuiop";

int main(int c, const char** v){
// parse the JSON to DOM
auto dom = jsondom::read(json);

// check that root node is a JSON object, just to be sure
assert(dom.is_object());

// let's check if there is an expected key1-value1 pair,
// but do not fail if there is no such key or value is not of an expected type

auto value1_i = dom.object().find("key1");
if(value1_i != dom.object().end()){
auto& value1 = value1_i->second;
if(value1.is_string()){
std::cout << "value1 = " << value1.string() << std::endl;
}else{
// this never happens in this example, but just to show how
// to handle cases when JSON value type is not as expected
std::cout << "value1 is not a string" << std::endl;
}
}else{
// this never happens in this example, but just to show how
// to handle cases when JSON key is not found
std::cout << "key1 is not found" << std::endl;
}

// Try to use key2-value2 pair, relying on that it exists.
// An exception will be thrown if something goes wrong, like
// no 'key2' key is found or value is of unexpected type.

try{
std::cout << "value2 = " << dom.object().at("key2").string() << std::endl;
}catch(std::logic_error& e){
// handle exception here if needed
std::cout << e.what() << std::endl;
throw;
}

return 0;
}
....