JSON module

json

JSON value aliases, serialization helpers, and file utilities built on nlohmann/json.

Classes

NameDescription
ConfigurationJSON configuration file
ISerializableAbstract interface for JSON-serializable objects.

Typedefs

ReturnNameDescription
nlohmann::jsonValuePrimary JSON value type used throughout the library.

Value

using Value = nlohmann::json

Defined in src/json/include/icy/json/json.h:40

Primary JSON value type used throughout the library.

Functions

ReturnNameDescription
boolserialize inlineSerializes pObj to a pretty-printed JSON string.
booldeserialize inlineDeserializes pObj from a JSON string.
voidloadFile inlineLoad a JSON file into a value. Throws on missing file or parse error.
voidsaveFile inlineSave a JSON value to a file. Throws on write error.
voidassertMember inlineAssert that a required member exists. Throws if missing.
voidcountNestedKeys inlineCount how many nested objects contain the given key.
boolhasNestedKey inlineReturn true if any nested object contains the given key.
boolfindNestedObjectWithProperty inlineFind a nested object whose property matches the given key/value.

serialize

inline

inline bool serialize(ISerializable * pObj, std::string & output)

Defined in src/json/include/icy/json/iserializable.h:43

Serializes pObj to a pretty-printed JSON string.

Returns

true on success, false if pObj is null.

Parameters

ParameterTypeDescription
pObjISerializable *Object to serialize; must not be null.
outputstd::string &Receives the 4-space indented JSON string.

deserialize

inline

inline bool deserialize(ISerializable * pObj, std::string & input)

Defined in src/json/include/icy/json/iserializable.h:59

Deserializes pObj from a JSON string.

Returns

true on success, false if pObj is null or parsing fails.

Parameters

ParameterTypeDescription
pObjISerializable *Object to populate; must not be null.
inputstd::string &JSON string to parse.

loadFile

inline

inline void loadFile(const std::string & path, json::Value & root)

Defined in src/json/include/icy/json/json.h:44

Load a JSON file into a value. Throws on missing file or parse error.


saveFile

inline

inline void saveFile(const std::string & path, const json::Value & root, int indent = 4)

Defined in src/json/include/icy/json/json.h:56

Save a JSON value to a file. Throws on write error.


assertMember

inline

inline void assertMember(const json::Value & root, const std::string & name)

Defined in src/json/include/icy/json/json.h:71

Assert that a required member exists. Throws if missing.


countNestedKeys

inline

inline void countNestedKeys(const json::Value & root, const std::string & key, int & count)

Defined in src/json/include/icy/json/json.h:79

Count how many nested objects contain the given key.


hasNestedKey

inline

inline bool hasNestedKey(const json::Value & root, const std::string & key)

Defined in src/json/include/icy/json/json.h:92

Return true if any nested object contains the given key.


findNestedObjectWithProperty

inline

inline bool findNestedObjectWithProperty(json::Value & root, json::Value *& result, std::string_view key, std::string_view value, bool partial = true, int index = 0)

Defined in src/json/include/icy/json/json.h:113

Find a nested object whose property matches the given key/value.

Key or value may be empty for wildcard matching. If partial is true, substring matches are accepted for string values. The index parameter selects the Nth match (0 = first).

Returns true if found, with result pointing to the matching object.