namespace json_utils

Overview

namespace json_utils {

// global functions

nlohmann::json parseJSONFile(const std::string& fileName);
void jsonSchemaLoader(const json_uri& uri, json& schema);

void validateJson(
    nlohmann::json& instance,
    std::string schema_path,
    bool addPatch = true
);

template <typename VALUE_T>
static void setDefault(
    nlohmann::json& instance,
    const std::string key,
    const VALUE_T new_value
);

} // namespace json_utils

Detailed Documentation

Global Functions

nlohmann::json parseJSONFile(const std::string& fileName)

Parse a JSON File and return it’s values.

Parameters:

fileName

File Name

Returns:

Returns parsed JSON

void jsonSchemaLoader(const json_uri& uri, json& schema)

Loads schema from json file given its uri. To be passed to a nlohmann::json_schema::json_validator constructor.

The function is parametrized with two parameters: uri and schema. uri provides the path to the json schema within NRP_CONFIG_INSTALL_DIR directory. the schema will be loaded into “schema” json object.

void validateJson(
    nlohmann::json& instance,
    std::string schema_path,
    bool addPatch = true
)

Validates a json object using a given json schema.

Parameters: instance: json object to be validated schema_path: URI of the schema to use for validation addPatch: boolean attribute. If true, parameter default values defined in the schema are added to ‘instance’ after validation.

template <typename VALUE_T>
static void setDefault(
    nlohmann::json& instance,
    const std::string key,
    const VALUE_T new_value
)

Sets default value for a given parameter.

The function is parametrized with three parameters: instance, key, new_value. A value ‘new_value’ for parameter ‘key’ is set in ‘instance’. If ‘key’ already exists in ‘instance’ nothing is done