nlohmann::basic_json::operator T() - renxiaobo27/json GitHub Wiki

template<typename T>
operator T() const;

Description

Implicitly converts a value of a JSON container.

Parameters

None.

Return value

The return value is get<T>().

Complexity

Amortized linear in the size of the JSON type.

Exceptions

std::std::logic_error if types are not compatible.

Example

#include <json.hpp>
#include <vector>
#include <list>
#include <unordered_set>
#include <map>
#include <string>

int main()
{
    nlohmann::json j1 = {1, 2, 3.2, 4.3, 5.4};
    std::vector<float> v11 = j1;
    std::list<int> v12 = j1;
    std::unordered_set<float> v13 = j1;

    nlohmann::json j2 = {{"one", 1}, {"two", 2}, {"three", 3}};
    std::map<std::string, size_t> v21 = j2;
    std::multimap<std::string, double> v22 = j2;

    nlohmann::json j3 = "Hello, world";
    std::string v31 = j3;
}

See also

  • get
    explicit value conversion
⚠️ **GitHub.com Fallback** ⚠️