ClikaRT::json::Json
class
Header: ClikaRT/json/json.h
A JSON value: null, boolean, number, string, array, or object. Copyable (deep) and movable. (A JSON object is the string-keyed map, what Python calls a dict at the boundary.)
Read a parsed value with the is_* predicates and the typed getters (as_string / as_int64 / as_double / as_bool), walk an object with for_each_member, and index an array or object with at. Build a value with the factories (object / array: nullary, or from a braced literal: Json::object({{"model", "m"}, {"n", 1}})), operator[] (auto-vivifies an object member), push_back, and assignment (v["k"] = 42). Serialize with dump.
Nested types
| Name | Description |
|---|---|
Element | An element inside a Json::array({...}) literal. Built implicitly from the bare value (1, "two", true, Json::object({...})); the implicit constructors are deliberate: the braced literal is this type's whole entry point. |
Member | A key/value pair inside a Json::object({...}) literal. Built implicitly from the braced pair ({"key", value}); the implicit constructors are deliberate: the braced literal is this type's whole entry point. key is a non-owning view; keep its backing alive for the call (a string literal is fine). |
Static member functions
parse()
static Json parse(std::string_view text)
Parse JSON text. Malformed input raises ClikaRT::Error. text is read during the call, not retained.
Declared in ClikaRT/json/json.h, line 71
read_file()
static Json read_file(std::string_view path)
Read and parse a JSON file. A missing/unreadable file or malformed JSON raises ClikaRT::Error. path is copied, not retained as a view.
Declared in ClikaRT/json/json.h, line 75
object()
static Json object()
An empty object / array, ready to populate.
Declared in ClikaRT/json/json.h, line 82
array()
static Json array()
An empty array value.
Declared in ClikaRT/json/json.h, line 84
object(initializer_list<Member>)
An object built from a braced member list: Json::object({{"model", "m"}, {"n", 1}}). Scalar values deduce their JSON type exactly as assignment (obj["k"] = v) does; nest with the factories themselves ({"cfg", Json::object({...})}). Members are deep-copied out of the list (an initializer_list's elements are const by construction), sized for config-scale payloads, per this file's focused-surface scope. An empty list yields Json::object().
Declared in ClikaRT/json/json.h, line 99
array(initializer_list<Element>)
An array built from a braced element list: Json::array({1, "two", true}). Same scalar deduction and deep-copy behavior as object({...}); an empty list yields Json::array().
Declared in ClikaRT/json/json.h, line 103
Member functions
Json()
Json()
A JSON null.
Declared in ClikaRT/json/json.h, line 46
Json(bool)
explicit Json(bool value)
Scalar values. Explicit so a bare scalar never silently becomes a Json in an unrelated overload; build one deliberately (Json(std::int64_t{0})) or assign through a member (obj["n"] = 0).
Declared in ClikaRT/json/json.h, line 51
Json(int64_t)
explicit Json(std::int64_t value)
An integer value.
Declared in ClikaRT/json/json.h, line 53
Json(double)
explicit Json(double value)
A floating-point value.
Declared in ClikaRT/json/json.h, line 55
Json(string_view)
explicit Json(std::string_view value)
A string value (copied).
Declared in ClikaRT/json/json.h, line 57
Json(char)
explicit Json(const char* value)
A string value (copied); disambiguates string literals.
Declared in ClikaRT/json/json.h, line 59
Json(Json)
Json(const Json& other)
Copy: a deep, independent copy of the document.
Declared in ClikaRT/json/json.h, line 62
Json(Json)
Json(Json&& other) noexcept
Move: transfers the document; other is left null.
Declared in ClikaRT/json/json.h, line 64
~Json()
~Json()
Releases the document.
Declared in ClikaRT/json/json.h, line 66
write_file()
void write_file(std::string_view path, int indent = 2) const
Serialize to path (parent directories are created). indent < 0 writes a compact single line. path is copied, not retained.
Declared in ClikaRT/json/json.h, line 79
operator[]()
Json& operator[](std::string_view key)
Object member by key, auto-vivifying: turns a null into an object and inserts an absent key as null. The returned reference writes through into this value, so obj["a"]["b"] = 1 and obj["k"] = "v" build nested structure. Valid while this Json is alive. (For a read-only lookup that does not mutate, use at(key).)
Declared in ClikaRT/json/json.h, line 112
push_back()
void push_back(Json value)
Append value to an array, turning a null into an array first.
Declared in ClikaRT/json/json.h, line 115
operator=(Json)
Assign a JSON value into this slot (write-through for an operator[] reference).
Declared in ClikaRT/json/json.h, line 119
operator=(Json)
Move-assign: transfers the document; other is left null.
Declared in ClikaRT/json/json.h, line 121
operator=(T)
template <``typename T, std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, Json>, int> = 0``>Json& operator=(T&& value)
Assign a scalar (v = true, v = 42, v = 3.14, v = "text"). Deduces the JSON type from the argument; an unsupported type fails to compile.
Declared in ClikaRT/json/json.h, line 127
is_null()
bool is_null() const
True for the null value.
Declared in ClikaRT/json/json.h, line 144
is_boolean()
bool is_boolean() const
True for a boolean.
Declared in ClikaRT/json/json.h, line 146
is_number()
bool is_number() const
integer or floating-point
Declared in ClikaRT/json/json.h, line 147
is_string()
bool is_string() const
True for a string.
Declared in ClikaRT/json/json.h, line 149
is_array()
bool is_array() const
True for an array.
Declared in ClikaRT/json/json.h, line 151
is_object()
bool is_object() const
True for an object.
Declared in ClikaRT/json/json.h, line 153
contains()
bool contains(std::string_view key) const
True if this is an object containing key.
Declared in ClikaRT/json/json.h, line 156
size()
std::size_t size() const
Number of members (object) / elements (array); 0 for a scalar or null.
Declared in ClikaRT/json/json.h, line 158
as_string()
std::string as_string() const
The value as the requested type. A type mismatch (e.g. as_string on a number) raises ClikaRT::Error.
Declared in ClikaRT/json/json.h, line 163
as_int64()
std::int64_t as_int64() const
The value as a signed 64-bit integer (any JSON number reads through).
Throws
ClikaRT::Error: (InvalidArgument) when the value is not a number.
Declared in ClikaRT/json/json.h, line 167
as_double()
double as_double() const
The value as a double (any JSON number reads through).
Throws
ClikaRT::Error: (InvalidArgument) when the value is not a number.
Declared in ClikaRT/json/json.h, line 171
as_bool()
bool as_bool() const
The value as a boolean.
Throws
ClikaRT::Error: (InvalidArgument) when the value is not a boolean.
Declared in ClikaRT/json/json.h, line 175
for_each_member()
void for_each_member(
const std::function<void(std::string_view key, const Json&value)>& visit
) const
Visit each member of an object in insertion order. key and value are valid only during the call (do not retain them). A no-op on a non-object.
A throw from visit is contained HERE, in your own TU / runtime, never unwinding through the library frame (a cross-runtime unwind into the Release .so's static C++ runtime terminates), and stops the walk, and surfaces as the raised ClikaRT::Error / returned failure. To stop the walk early WITHOUT an error, use for_each_member_while.
Declared in ClikaRT/json/json.h, line 186
for_each_member_while()
void for_each_member_while(
const std::function<bool(std::string_view key, const Json&value)>& visit
) const
The abort-capable sibling: visit returns false to stop the walk early (a clean stop, not an error). A throw is contained exactly as in for_each_member.
Declared in ClikaRT/json/json.h, line 196
at(string_view)
Json at(std::string_view key) const
Object member by key (a read-only copy). Raises ClikaRT::Error (NotFound) when this is not an object or the key is absent.
Declared in ClikaRT/json/json.h, line 212
at(size_t)
Json at(std::size_t index) const
Object member by key (a read-only copy). Raises ClikaRT::Error (NotFound) when this is not an object or the key is absent.
Declared in ClikaRT/json/json.h, line 216
dump()
std::string dump(int indent = -1) const
JSON text. indent < 0 (the default) is compact (single line); indent >= 0 pretty-prints with that many spaces per nesting level.
Declared in ClikaRT/json/json.h, line 222