mirror of
https://github.com/nlohmann/json.git
synced 2026-11-15 03:48:21 +00:00
get<T>() and get_to() of a view call T's from_json() with the view itself when that from_json() accepts views but not an unrelated probe type, and fall back to materialize() otherwise. The from_json() functions of the NLOHMANN_DEFINE_TYPE_*, NLOHMANN_DEFINE_DERIVED_TYPE_* and NLOHMANN_JSON_SERIALIZE_ENUM(_STRICT) macros accept basic_json values and views; a derived type whose base reads only json reads the base part through materialize(). The library's own from_json() overloads are not called with views: from_json() of a view (and of the probe) only looks up functions by ADL. New public trait nlohmann::is_json_view<T>, and the member type basic_json_type of basic_json_view. With duplicate keys, the members of such a T are those the view's lookups find (the first ones). bench_decode.cpp measures decoding into structs (4-6x faster than materialize().get<T>() on twitter, citm_catalog, and synthetic records). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.8 KiB
2.8 KiB
nlohmann::basic_json_view::get_to
template<typename T>
T& get_to(T& v) const;
Converts the value to T and stores it in v, in one of three ways:
- For the types that
get<T>()converts directly -- arithmetic types,#!cpp bool,#!cpp std::nullptr_t,string_view_t,#!cpp std::basic_string,BasicJsonType,basic_json_view,#!cpp std::vector, and#!cpp std::map/#!cpp std::unordered_mapwith string keys --vis assigned:#!cpp v = get<T>(). - For a type whose
from_json()accepts views (seeget<T>()), thatfrom_json()is called with the view and the existingv, without building aBasicJsonTypevalue. - For every other type, the subtree is built into a
BasicJsonTypevalue and#!cpp materialize().get_to(v)is called, i.e.from_json()is invoked with the existingv, exactly asBasicJsonType::get_to()does.
Template parameters
T- the type to convert the value to
Parameters
v(out)- the variable to store the converted value in
Return value
v, allowing calls to chain
Exception safety
- For the directly converted types: strong exception safety. If an exception is thrown,
vis not modified. - For every other type: as for
BasicJsonType::get_to().materialize()throws beforevis touched, but afrom_json()that throws may leavevpartly modified.
Exceptions
- For the directly converted types: whatever
get<T>()throws for the same value andT. - For every other type: whatever
materialize()andBasicJsonType::get_to()throw.
Complexity
- For the directly converted types: whatever
get<T>()has for the sameT. - For every other type: that of
materialize(), plus that ofBasicJsonType::get_to().
Notes
The difference between the two ways matters for a from_json() that sets only the fields present in the JSON: since
it runs on the existing v, the other fields of v keep their values, whereas assigning get<T>() would replace
them with those of a default-constructed T.
Examples
??? example
The example below reads several fields of a service configuration directly into existing variables, then uses
the returned reference to fold the `#!cpp host`/`#!cpp port` pair into a single string in the same expression.
```cpp
--8<-- "examples/basic_json_view__get_to.cpp"
```
Output:
```json
--8<-- "examples/basic_json_view__get_to.output"
```
See also
- get - convert the value to a given type
BasicJsonType::get_to- the corresponding function ofbasic_json
Version history
- Added in version 3.13.0.