JSON for Modern C++ 3.10.4

◆ operator ValueType()

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<typename ValueType , typename std::enable_if< detail::conjunction< detail::negation< std::is_pointer< ValueType > >, detail::negation< std::is_same< ValueType, detail::json_ref< basic_json > > >, detail::negation< std::is_same< ValueType, typename string_t::value_type > >, detail::negation< detail::is_basic_json< ValueType > >, detail::negation< std::is_same< ValueType, std::initializer_list< typename string_t::value_type > > >, detail::is_detected_lazy< detail::get_template_function, const basic_json_t &, ValueType > >::value, int >::type = 0>
JSON_EXPLICIT nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::operator ValueType ( ) const
inline

Implicit type conversion between the JSON value and a compatible value. The call is realized by calling get() const.

Template Parameters
ValueTypenon-pointer type compatible to the JSON value, for instance int for JSON integer numbers, bool for JSON booleans, or std::vector types for JSON arrays. The character type of string_t as well as an initializer list of this type is excluded to avoid ambiguities as these types implicitly convert to std::string.
Returns
copy of the JSON value, converted to type ValueType
Exceptions
type_error.302in case passed type ValueType is incompatible to the JSON value type (e.g., the JSON value is of type boolean, but a string is requested); see example below
Complexity
Linear in the size of the JSON value.
Example
The example below shows several conversions from JSON values to other types. There a few things to note: (1) Floating-point numbers can be converted to integers, (2) A JSON array can be converted to a standard std::vector<short>, (3) A JSON object can be converted to C++ associative containers such as std::unordered_map<std::string, json>.
1#include <iostream>
2#include <unordered_map>
3#include <nlohmann/json.hpp>
4
5using json = nlohmann::json;
6
7int main()
8{
9 // create a JSON value with different types
10 json json_types =
11 {
12 {"boolean", true},
13 {
14 "number", {
15 {"integer", 42},
16 {"floating-point", 17.23}
17 }
18 },
19 {"string", "Hello, world!"},
20 {"array", {1, 2, 3, 4, 5}},
21 {"null", nullptr}
22 };
23
24 // use implicit conversions
25 bool v1 = json_types["boolean"];
26 int v2 = json_types["number"]["integer"];
27 short v3 = json_types["number"]["integer"];
28 float v4 = json_types["number"]["floating-point"];
29 int v5 = json_types["number"]["floating-point"];
30 std::string v6 = json_types["string"];
31 std::vector<short> v7 = json_types["array"];
32 std::unordered_map<std::string, json> v8 = json_types;
33
34 // print the conversion results
35 std::cout << v1 << '\n';
36 std::cout << v2 << ' ' << v3 << '\n';
37 std::cout << v4 << ' ' << v5 << '\n';
38 std::cout << v6 << '\n';
39
40 for (auto i : v7)
41 {
42 std::cout << i << ' ';
43 }
44 std::cout << "\n\n";
45
46 for (auto i : v8)
47 {
48 std::cout << i.first << ": " << i.second << '\n';
49 }
50
51 // example for an exception
52 try
53 {
54 bool v1 = json_types["string"];
55 }
56 catch (json::type_error& e)
57 {
58 std::cout << e.what() << '\n';
59 }
60}
detail::type_error type_error
exception indicating executing a member function with a wrong type
Definition: json.hpp:17769
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
1
42 42
17.23 17
Hello, world!
1 2 3 4 5 

string: "Hello, world!"
number: {"floating-point":17.23,"integer":42}
null: null
boolean: true
array: [1,2,3,4,5]
[json.exception.type_error.302] type must be boolean, but is string
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/operator__ValueType.cpp -o operator__ValueType 
Since
version 1.0.0

Definition at line 20914 of file json.hpp.