JSON module

json

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

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

nlohmann::json Value()

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)

Serializes pObj to a pretty-printed JSON string.

Parameters

  • pObj Object to serialize; must not be null.

  • output Receives the 4-space indented JSON string.

Returns

true on success, false if pObj is null.


deserialize

inline

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

Deserializes pObj from a JSON string.

Parameters

  • pObj Object to populate; must not be null.

  • input JSON string to parse.

Returns

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


loadFile

inline

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

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)

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


assertMember

inline

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

Assert that a required member exists. Throws if missing.


countNestedKeys

inline

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

Count how many nested objects contain the given key.


hasNestedKey

inline

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

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, int index)

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.