mirror of
https://github.com/nlohmann/json.git
synced 2026-10-26 07:48:55 +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>
1.7 KiB
1.7 KiB
nlohmann::is_json_view
Defined in header <nlohmann/json_view.hpp>
template<typename T>
struct is_json_view;
A type trait: #!cpp is_json_view<T>::value is #!cpp true if T is a basic_json_view
of any BasicJsonType and any view_mode -- json_view,
json_editable_view, ordered_json_view, ... -- and
#!cpp false otherwise, for basic_json as well.
It is meant for constraining templates that accept views, in particular a from_json() that should read views
directly: get<T>() on a view calls a from_json() with the view only if that from_json()
accepts views but not arbitrary types.
Template parameters
T- the type to check (without cv-qualifiers or references)
Notes
A view is recognized by its member type basic_json_type (a basic_json specialization) and
its materialize() returning that type, so the trait needs no declaration of the
view class.
Examples
??? example
The example below shows the trait, and a `from_json()` constrained to `json` values and views: `get<T>()` on a view
reads the view directly.
```cpp
--8<-- "examples/is_json_view.cpp"
```
Output:
```
--8<-- "examples/is_json_view.output"
```
See also
- basic_json_view - a handle to one value of a document
- get - which conversions read the view directly
- view_mode - read-only or editable
Version history
Added in version 3.13.0.