1 # <small>nlohmann::basic_json::</small>get
5 template<typename ValueType>
6 ValueType get() const noexcept(
7 noexcept(JSONSerializer<ValueType>::from_json(
8 std::declval<const basic_json_t&>(), std::declval<ValueType&>())));
11 template<typename BasicJsonType>
12 BasicJsonType get() const;
15 template<typename PointerType>
16 PointerType get_ptr();
18 template<typename PointerType>
19 constexpr const PointerType get_ptr() const noexcept;
22 1. Explicit type conversion between the JSON value and a compatible value which is
23 [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) and
24 [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). The value is converted by
25 calling the `json_serializer<ValueType>` `from_json()` method.
27 The function is equivalent to executing
30 JSONSerializer<ValueType>::from_json(*this, ret);
34 This overload is chosen if:
36 - `ValueType` is not `basic_json`,
37 - `json_serializer<ValueType>` has a `from_json()` method of the form
38 `void from_json(const basic_json&, ValueType&)`, and
39 - `json_serializer<ValueType>` does not have a `from_json()` method of the form
40 `ValueType from_json(const basic_json&)`
42 If the type is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) and
43 **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible), the value is
44 converted by calling the `json_serializer<ValueType>` `from_json()` method.
46 The function is then equivalent to executing
48 return JSONSerializer<ValueTypeCV>::from_json(*this);
51 This overload is chosen if:
53 - `ValueType` is not `basic_json` and
54 - `json_serializer<ValueType>` has a `from_json()` method of the form
55 `ValueType from_json(const basic_json&)`
57 If `json_serializer<ValueType>` has both overloads of `from_json()`, the latter one is chosen.
59 2. Overload for `basic_json` specializations. The function is equivalent to executing
64 3. Explicit pointer access to the internally stored JSON value. No copies are made.
66 ## Template parameters
69 : the value type to return
72 : a specialization of `basic_json`
75 : pointer type; must be a pointer to [`array_t`](array_t.md), [`object_t`](object_t.md), [`string_t`](string_t.md),
76 [`boolean_t`](boolean_t.md), [`number_integer_t`](number_integer_t.md), or
77 [`number_unsigned_t`](number_unsigned_t.md), [`number_float_t`](number_float_t.md), or [`binary_t`](binary_t.md).
78 Other types will not compile.
82 1. copy of the JSON value, converted to `ValueType`
83 2. a copy of `#!cpp *this`, converted into `BasicJsonType`
84 3. pointer to the internally stored JSON value if the requested pointer type fits to the JSON value; `#!cpp nullptr`
89 Depends on what `json_serializer<ValueType>` `from_json()` method throws
95 Writing data to the pointee (overload 3) of the result yields an undefined state.
101 The example below shows several conversions from JSON values
102 to other types. There a few things to note: (1) Floating-point numbers can
103 be converted to integers, (2) A JSON array can be converted to a standard
104 `std::vector<short>`, (3) A JSON object can be converted to C++
105 associative containers such as `std::unordered_map<std::string, json>`.
108 --8<-- "examples/get__ValueType_const.cpp"
114 --8<-- "examples/get__ValueType_const.output"
119 The example below shows how pointers to internal values of a JSON value can be requested. Note that no type
120 conversions are made and a `#cpp nullptr` is returned if the value and the requested pointer type does not match.
123 --8<-- "examples/get__PointerType.cpp"
129 --8<-- "examples/get__PointerType.output"
134 1. Since version 2.1.0.
135 2. Since version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.2.0.
136 3. Since version 1.0.0.