Skip to content

Access with default value: value

Overview

In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default value was present. For this case, use value(key, default_value) which takes the key you want to access and a default value in case there is no value stored with that key.

Example

Example

Consider the following JSON value:

{
    "logOutput": "result.log",
    "append": true
}

Assume the value is parsed to a json variable j.

expression value
j {"logOutput": "result.log", "append": true}
j.value("logOutput", "logfile.log") "result.log"
j.value("append", true) true
j.value("append", false) true
j.value("logLevel", "verbose") "verbose"

Notes

Exceptions

Return type

The value function is a template, and the return type of the function is determined by the type of the provided default value unless otherwise specified. This can have unexpected effects. In the example below, we store a 64-bit unsigned integer. We get exactly that value when using operator[]. However, when we call value and provide 0 as default value, then -1 is returned. The occurs, because 0 has type int which overflows when handling the value 18446744073709551615.

To address this issue, either provide a correctly typed default value or use the template parameter to specify the desired return type. Note that this issue occurs even when a value is stored at the provided key, and the default value is not used as the return value.

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
    json j = json::parse(R"({"uint64": 18446744073709551615})");

    std::cout << "operator[]:                " << j["uint64"] << '\n'
              << "default value (int):       " << j.value("uint64", 0) << '\n'
              << "default value (uint64_t):  " << j.value("uint64", std::uint64_t(0)) << '\n'
              << "explict return value type: " << j.value<std::uint64_t>("uint64", 0) << '\n';
}

Output:

operator[]:                18446744073709551615
default value (int):       -1
default value (uint64_t):  18446744073709551615
explict return value type: 18446744073709551615

See also