mirror of
https://github.com/nlohmann/json.git
synced 2024-12-16 23:17:49 +08:00
3.0 KiB
3.0 KiB
basic_json::get
// (1)
template<typename ValueType>
ValueType get() const noexcept(
noexcept(JSONSerializer<ValueType>::from_json(
std::declval<const basic_json_t&>(), std::declval<ValueType&>())));
// (2)
template<typename BasicJsonType>
BasicJsonType get() const;
-
Explicit type conversion between the JSON value and a compatible value which is CopyConstructible and DefaultConstructible. The value is converted by calling the
json_serializer<ValueType>
from_json()
method.The function is equivalent to executing
ValueType ret; JSONSerializer<ValueType>::from_json(*this, ret); return ret;
This overloads is chosen if:
ValueType
is notbasic_json
,json_serializer<ValueType>
has afrom_json()
method of the formvoid from_json(const basic_json&, ValueType&)
, andjson_serializer<ValueType>
does not have afrom_json()
method of the formValueType from_json(const basic_json&)
If the type is not CopyConstructible and not DefaultConstructible, the value is converted by calling the
json_serializer<ValueType>
from_json()
method.The function is then equivalent to executing
return JSONSerializer<ValueTypeCV>::from_json(*this);
This overloads is chosen if:
ValueType
is notbasic_json
andjson_serializer<ValueType>
has afrom_json()
method of the formValueType from_json(const basic_json&)
If
json_serializer<ValueType>
has both overloads offrom_json()
, the latter one is chosen. -
Overload for
basic_json
specializations. The function is equivalent to executingreturn *this;
Template parameters
ValueType
- the value type to return
BasicJsonType
- a specialization of
basic_json
Return value
- copy of the JSON value, converted to
ValueType
- a copy of
#!cpp *this
, converted intoBasicJsonType
Exceptions
Depends on what json_serializer<ValueType>
from_json()
method throws
Example
??? 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>`.
```cpp
--8<-- "examples/get__ValueType_const.cpp"
```
Output:
```json
--8<-- "examples/get__ValueType_const.output"
```
Version history
- Since version 2.1.0.
- Since version 2.1.0. Extended to work with other specializations of
basic_json
in version 3.2.0.