|
◆ 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>
Implicit type conversion between the JSON value and a compatible value. The call is realized by calling get() const.
- Template Parameters
-
ValueType | non-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
-
std::domain_error | in case passed type ValueType is incompatible to JSON, thrown by get() const |
- Complexity\n Linear in the size of the JSON value.
- Example\n 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> .
2#include <unordered_map>
15 { "floating-point", 17.23}
18 { "string", "Hello, world!"},
19 { "array", {1, 2, 3, 4, 5}},
34 std::cout << v1 << '\n';
35 std::cout << v2 << ' ' << v3 << '\n';
36 std::cout << v4 << ' ' << v5 << '\n';
37 std::cout << v6 << '\n';
41 std::cout << i << ' ';
47 std::cout << i.first << ": " << i.second << '\n';
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >()) explicitly create an array from an initializer list
a class to store JSON values
basic_json<> json default JSON class
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]
The example code above can be translated with g++ -std=c++11 -Isrc doc/examples/operator__ValueType.cpp -o operator__ValueType
- Since
- version 1.0.0
Definition at line 3492 of file json.hpp.
|