- Make the iterator range illustrations legible in dark mode (prefers-color-scheme rules inside the SVGs). - Add a "Byte layout" section with a color-coded byte table to each binary format page (BJData, BON8, BSON, CBOR, MessagePack, UBJSON), and highlight the smallest size per file in the comparison table. - Add "See also" sections to the overview pages that lacked one. - Split the release notes into one page per release, grouped by minor version on the overview page, with an "API changes" section per release generated from the API pages' version histories (hooks/api_changes.py). - Fact-check the release notes and version histories against the release tags and correct wrong or misleading entries (e.g., json_serializer 2.1.0, merge_patch 3.1.0, items, count, operator[], to_bjdata, std::string_view key support). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.5 KiB
nlohmann::basic_json::get_to
template<typename ValueType>
ValueType& get_to(ValueType& v) const noexcept(
noexcept(JSONSerializer<ValueType>::from_json(
std::declval<const basic_json_t&>(), v)));
Explicit type conversion between the JSON value and a compatible value. The value is filled into the input parameter by
calling the json_serializer<ValueType> from_json() method.
The function is equivalent to executing
ValueType v;
JSONSerializer<ValueType>::from_json(*this, v);
This overload is chosen if:
ValueTypeis notbasic_json,json_serializer<ValueType>has afrom_json()method of the formvoid from_json(const basic_json&, ValueType&)
v must not be const. Passing a const object is a compile-time error. For types such as arithmetic types, enums,
and C arrays, the error is a static_assert that names the problem. For other types, the overload is not viable, and
the compiler reports that no matching get_to was found.
Template parameters
ValueType- the value type to return
Return value
the input parameter, allowing chaining calls
Exception safety
Depends on what json_serializer<ValueType> from_json() method throws; the JSON value itself is never modified,
since get_to() is a #!cpp const member function.
Exceptions
Depends on what json_serializer<ValueType> from_json() method throws
Complexity
Depends on the json_serializer<ValueType>::from_json() implementation.
Examples
??? 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
`#!cpp std::vector<short>`, (3) A JSON object can be converted to C++ associative containers such as
`#!cpp std::map<std::string, json>`.
```cpp
--8<-- "examples/get_to.cpp"
```
Output:
```json
--8<-- "examples/get_to.output"
```
See also
- get get a value (explicit conversion)
- get_ref get a reference to the stored value
- get_ptr get a pointer to the stored value
- Converting values - the type conversions article
- basic_json_view::get_to - the same conversion on a zero-copy view
Version history
- Added in version 3.3.0.
- Added a
static_assertwith a clear message forconstarguments in version 3.13.0.