libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
JsonObject Class Reference

JSON object container wrapping nlohmann::json. More...

#include <json.h>

Public Types

using Ptr = SharedPtr< JsonObject >
 Shared pointer type for JsonObject.
 

Public Member Functions

 JsonObject ()
 Constructs an empty JSON object.
 
int size () const
 Returns the number of key-value pairs in the object.
 
bool isValid () const
 Returns true if the object contains at least one key-value pair.
 
bool valueIsNull (const String &key) const
 Returns true if the value for the given key is null.
 
bool valueIsObject (const String &key) const
 Returns true if the value for the given key is a JSON object.
 
bool valueIsArray (const String &key) const
 Returns true if the value for the given key is a JSON array.
 
bool contains (const String &key) const
 Returns true if the object contains the given key.
 
bool getBool (const String &key, Error *err=nullptr) const
 Returns the boolean value for the given key.
 
int64_t getInt (const String &key, Error *err=nullptr) const
 Returns the signed 64-bit integer value for the given key.
 
uint64_t getUInt (const String &key, Error *err=nullptr) const
 Returns the unsigned 64-bit integer value for the given key.
 
double getDouble (const String &key, Error *err=nullptr) const
 Returns the double-precision floating-point value for the given key.
 
String getString (const String &key, Error *err=nullptr) const
 Returns the string value for the given key.
 
JsonObject getObject (const String &key, Error *err=nullptr) const
 Returns the nested JsonObject for the given key.
 
JsonArray getArray (const String &key, Error *err=nullptr) const
 Returns the nested JsonArray for the given key.
 
String toString (unsigned int indent=0) const
 Serializes the object to a JSON string.
 
void clear ()
 Removes all key-value pairs from the object.
 
void setNull (const String &key)
 Sets the value for the given key to null.
 
void set (const String &key, const JsonObject &val)
 Sets a nested JsonObject value for the given key.
 
void set (const String &key, const JsonArray &val)
 Sets a nested JsonArray value for the given key.
 
void set (const String &key, bool val)
 Sets a boolean value for the given key.
 
void set (const String &key, int val)
 Sets an int value for the given key.
 
void set (const String &key, unsigned int val)
 Sets an unsigned int value for the given key.
 
void set (const String &key, int64_t val)
 Sets a signed 64-bit integer value for the given key.
 
void set (const String &key, uint64_t val)
 Sets an unsigned 64-bit integer value for the given key.
 
void set (const String &key, float val)
 Sets a float value for the given key.
 
void set (const String &key, double val)
 Sets a double value for the given key.
 
void set (const String &key, const char *val)
 Sets a C-string value for the given key.
 
void set (const String &key, const String &val)
 Sets a String value for the given key.
 
void set (const String &key, const UUID &val)
 Sets a UUID value (stored as its string representation) for the given key.
 
void setFromVariant (const String &key, const Variant &val)
 Sets a value from a Variant, automatically selecting the JSON type.
 
template<typename Func >
void forEach (Func &&func) const
 Iterates over all key-value pairs in the object.
 
bool operator== (const JsonObject &other) const
 Returns true if both JSON objects have identical contents.
 

Static Public Member Functions

static JsonObject parse (const String &str, Error *err=nullptr)
 Parses a JSON object from a string.
 

Friends

class JsonArray
 

Detailed Description

JSON object container wrapping nlohmann::json.

Provides a type-safe interface for building and querying JSON objects.

Example
// Build a JSON object
obj.set("name", "clip001");
obj.set("width", 1920);
obj.set("hdr", true);
String json = obj.toString(2); // pretty-printed
// Parse and query
String name = parsed.getString("name");
int w = parsed.getInt("width");
JSON object container wrapping nlohmann::json.
Definition json.h:44
static JsonObject parse(const String &str, Error *err=nullptr)
Parses a JSON object from a string.
Definition json.h:56
Dynamic array container wrapping std::vector.
Definition list.h:40
bool set(size_t index, const T &val)
Sets an item in the list by index.
Definition list.h:541
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Values can be accessed by key with typed getters that perform safe conversions. Supports nesting via JsonObject and JsonArray values.

Member Function Documentation

◆ contains()

bool JsonObject::contains ( const String key) const
inline

Returns true if the object contains the given key.

Parameters
keyThe key to look up.
Returns
true if the key exists in the object.

◆ forEach()

template<typename Func >
void JsonObject::forEach ( Func &&  func) const
inline

Iterates over all key-value pairs in the object.

Template Parameters
FuncCallable with signature void(const String &key, const Variant &val).
Parameters
funcThe function to invoke for each key-value pair.

◆ getArray()

JsonArray JsonObject::getArray ( const String key,
Error err = nullptr 
) const
inline

Returns the nested JsonArray for the given key.

Parameters
keyThe key to look up.
errOptional error output; set to Error::Invalid if the key is missing or not an array.
Returns
The nested JsonArray, or an empty array on failure.

◆ getBool()

bool JsonObject::getBool ( const String key,
Error err = nullptr 
) const
inline

Returns the boolean value for the given key.

Parameters
keyThe key to look up.
errOptional error output; set to Error::Invalid if the key is missing or not convertible.
Returns
The boolean value, or false on failure.

◆ getDouble()

double JsonObject::getDouble ( const String key,
Error err = nullptr 
) const
inline

Returns the double-precision floating-point value for the given key.

Parameters
keyThe key to look up.
errOptional error output.
Returns
The double value, or 0.0 on failure.

◆ getInt()

int64_t JsonObject::getInt ( const String key,
Error err = nullptr 
) const
inline

Returns the signed 64-bit integer value for the given key.

Parameters
keyThe key to look up.
errOptional error output.
Returns
The integer value, or 0 on failure.

◆ getObject()

JsonObject JsonObject::getObject ( const String key,
Error err = nullptr 
) const
inline

Returns the nested JsonObject for the given key.

Parameters
keyThe key to look up.
errOptional error output; set to Error::Invalid if the key is missing or not an object.
Returns
The nested JsonObject, or an empty object on failure.

◆ getString()

String JsonObject::getString ( const String key,
Error err = nullptr 
) const
inline

Returns the string value for the given key.

Parameters
keyThe key to look up.
errOptional error output.
Returns
The string value, or an empty String on failure.

◆ getUInt()

uint64_t JsonObject::getUInt ( const String key,
Error err = nullptr 
) const
inline

Returns the unsigned 64-bit integer value for the given key.

Parameters
keyThe key to look up.
errOptional error output.
Returns
The unsigned integer value, or 0 on failure.

◆ parse()

static JsonObject JsonObject::parse ( const String str,
Error err = nullptr 
)
inlinestatic

Parses a JSON object from a string.

Parameters
strThe JSON string to parse.
errOptional error output; set to Error::Invalid on failure.
Returns
The parsed JsonObject, or an empty object on failure.

◆ set() [1/2]

void JsonObject::set ( const String key,
const JsonArray val 
)
inline

Sets a nested JsonArray value for the given key.

Parameters
keyThe key to set.
valThe JsonArray value.

◆ set() [2/2]

void JsonObject::set ( const String key,
const JsonObject val 
)
inline

Sets a nested JsonObject value for the given key.

Parameters
keyThe key to set.
valThe JsonObject value.

◆ setFromVariant()

void JsonObject::setFromVariant ( const String key,
const Variant val 
)
inline

Sets a value from a Variant, automatically selecting the JSON type.

Parameters
keyThe key to set.
valThe Variant whose value and type determine the stored JSON value.

◆ setNull()

void JsonObject::setNull ( const String key)
inline

Sets the value for the given key to null.

Parameters
keyThe key to set.

◆ toString()

String JsonObject::toString ( unsigned int  indent = 0) const
inline

Serializes the object to a JSON string.

Parameters
indentNumber of spaces per indentation level. Zero produces compact output.
Returns
The serialized JSON string.

◆ valueIsArray()

bool JsonObject::valueIsArray ( const String key) const
inline

Returns true if the value for the given key is a JSON array.

Parameters
keyThe key to check.
Returns
true if the key exists and its value is an array.

◆ valueIsNull()

bool JsonObject::valueIsNull ( const String key) const
inline

Returns true if the value for the given key is null.

Parameters
keyThe key to check.
Returns
true if the key exists and its value is null.

◆ valueIsObject()

bool JsonObject::valueIsObject ( const String key) const
inline

Returns true if the value for the given key is a JSON object.

Parameters
keyThe key to check.
Returns
true if the key exists and its value is an object.

The documentation for this class was generated from the following file: