JsonObject is a C++23 library built on Boost.JSON for reading and modifying nested JSON values using slash-separated key paths.
- Core library:
include/json_object.hinclude/json_key_path.hinclude/json_types.hsrc/json_object.ccsrc/json_key_path.cc
- Unit tests with GoogleTest in
test/ - CMake build based on shared settings from
cmake-common/
- Access nested JSON values with string paths like
user/profile/name - Access array items with index keys like
[0],[1] - Use special array symbols:
[^]first element (or prepend onset)[$]last element (or append onset)
- Optional default values for safe reads
- Optional
force=truewrites to create compatible intermediate containers - File I/O helpers:
load(filename)write(filename, indent)
- Segments are separated by
/ - Object keys are plain strings, for example:
settings/theme - Array indices are bracketed, for example:
users/[0]/name - Valid index symbols:
[0],[1], ...,[^],[$] - Invalid string keys include empty strings, whitespace-only strings, numeric-only strings, or keys containing
[,],\n,\r
#include <dkyb/json_object.h>
using util::JsonObject;
JsonObject obj(R"({"user":{"name":"Ada","age":36}})");
obj.set("user/name", "Ada Lovelace");
auto name = obj.get("user/name");#include <dkyb/json_object.h>
using util::JsonObject;
JsonObject obj(R"({"user":{"name":"Ada"}})");
// Missing key in a compatible object path -> returns default
auto city = obj.get("user/city", "unknown");
// Incompatible path still throws (object vs array mismatch)
// obj.get("user/[0]", "fallback");#include <dkyb/json_object.h>
using util::JsonObject;
JsonObject arr(R"([1,2,3])");
arr.set("[^]", 0); // prepend -> [0,1,2,3]
arr.set("[$]", 4); // append -> [0,1,2,3,4]
auto first = arr.get("[^]");
auto last = arr.get("[$]");#include <dkyb/json_object.h>
using util::JsonObject;
JsonObject obj("{}");
// Without force this would throw due to missing path/container
obj.set("a/[0]/name", "node-0", true);
// Result is compatible structure created on demand#include <dkyb/json_key_path.h>
#include <dkyb/json_object.h>
util::JsonObject obj(R"({"items":[{"id":7}]})");
util::JsonKeyPath path("items/[0]/id");
auto id = obj.get(path);#include <dkyb/json_object.h>
util::JsonObject obj;
obj.load("input.json");
obj.set("meta/version", 2, true);
obj.write("output.json", 2); // pretty-print with 2-space indentation- CMake
- C++23 compiler
- Boost (Boost.JSON component)
- GoogleTest (for tests)
git clone --recurse-submodules -j "$(nproc)" git@github.com:kingkybel/JsonObject.git
cd JsonObject
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build --parallel "$(nproc)"ctest --test-dir build --output-on-failure
# or: ./build/Debug/bin/run_testsINSTALL_PREFIX=/usr
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${INSTALL_PREFIX}"
cmake --build build --parallel "$(nproc)"
sudo cmake --install buildHeaders are installed to ${INSTALL_PREFIX}/include/dkyb and the library to ${INSTALL_PREFIX}/lib.
Reduce the smells, keep on top of code-quality. Sonar Qube is run on every push to the main branch on GitHub.