* docs: new design for the documentation site Apply the brand to the Material for MkDocs site: Inter and JetBrains Mono served by the site (upright and real italic), brand colours, blue header band with the syntax pattern, wordmark in the header, new home page (hero, cards, animated feature tour), 404 page, polish for tabs, search, footer, focus rings, tables and code blocks, and theme features (code copy, previous/next footer, TOC follow, search suggestions). Draw the number railroad diagram, the parser callback events and the iterator illustrations as inline SVGs that follow the light and dark scheme. The generator scripts are in docs/mkdocs/scripts/figures; the feature slideshow (json-light.svg, json-dark.svg, replacing json.gif on the home page) is built by docs/mkdocs/scripts/slideshow. Decision nodes in five flowcharts become hexagons, which need far less room. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: add figures, table captions and a macro chooser - Figures with numbered captions: SAX stepper, json_view layout and edit stepper, exception map, JSON pointer and patch walkers, QA pipeline, scaling and binary size charts, roadmap lanes, release timeline, integration decision tree and namespace builder - Table captions (nj-table-caption) on 9 pages - Compile times: modules and PCH charts, filesystem section, re-measured Figure 1, links between features/modules.md and compile_times.md - Interactive macro chooser for features/arbitrary_types.md - Link check: exclude URLs that answer the plugin with 400/401/419 or are dead (customers.md) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: macro chooser generates adl_serializer for non-default-constructible types Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: cross-link concept pages and API pages in See also sections Add See also links in both directions between the concept pages (features, home, integration) and the API pages, write API links in code font, give every entry a comment after a dash, and show the callback event figure on the parser callbacks page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: address review comments on the new design - Home: add a "Latest release" button - Sponsors: update the list and group it by tier - Features: list all feature pages on the index page - Releases: add highlights per release, link the 4.0 roadmap, add the first commit to the timeline - Exceptions: prefix all example messages with the exception name - API: no code font for format names in the See also links of the to_/from_ pages - Merge Patch: show the document before, the patch, and the result - Replace "--" with typographic dashes, mark external links with an icon, center Mermaid diagrams Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
6.9 KiB
nlohmann::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;
// (3)
template<typename PointerType>
PointerType get() noexcept;
template<typename PointerType>
const PointerType get() const noexcept; // constexpr since C++14
-
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 overload is chosen if:
ValueTypeis 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 overload is chosen if:
ValueTypeis notbasic_jsonandjson_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_jsonspecializations. The function is equivalent to executingreturn *this; -
Explicit pointer access to the internally stored JSON value. No copies are made.
Template parameters
ValueType- the value type to return
BasicJsonType- a specialization of
basic_json PointerType- pointer type; must be a pointer to
array_t,object_t,string_t,boolean_t,number_integer_t, ornumber_unsigned_t,number_float_t, orbinary_t. Other types will not compile.
Return value
- copy of the JSON value, converted to
ValueType - a copy of
#!cpp *this, converted intoBasicJsonType - pointer to the internally stored JSON value if the requested pointer type fits to the JSON value;
#!cpp nullptrotherwise
Exception safety
Depends on what json_serializer<ValueType> from_json() method throws for overloads (1) and (2); the JSON value
itself is never modified, since get() is a #!cpp const member function. No-throw guarantee for overload (3): this
function never throws exceptions.
Exceptions
Depends on what json_serializer<ValueType> from_json() method throws
Complexity
Depends on the json_serializer<ValueType>::from_json() implementation for overloads (1) and (2); constant for
overload (3).
Notes
!!! danger "Undefined behavior for pointers"
Writing data to the pointee (overload 3) of the result yields an undefined state.
!!! danger "Undefined behavior for numeric conversions"
Conversions between numeric types are performed by the corresponding
`from_json()` implementation using the target C++ type. When converting
between numeric types, the library does not check whether the source
value is representable by the target type.
If the source value is outside the range of the target type, the behavior
is the same as the corresponding C++ conversion. In particular, converting
a floating-point value to an integer type that cannot represent the value
results in undefined behavior.
See [Number conversion](../../features/types/number_handling.md#number-conversion)
for more information.
!!! note "std::optional conversions"
Prior to version 3.13.0, `#!cpp get<std::optional<T>>()` (and other conversions to `std::optional<T>`) failed to
compile in every configuration, due to an internal implementation bug that made the `from_json` overload for
`std::optional` unreachable regardless of the [`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md)
setting. This has been fixed.
Examples
??? example "Example: (1) explicit conversion to compatible types"
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::map<std::string, json>`.
```cpp
--8<-- "examples/get__ValueType_const.cpp"
```
Output:
```json
--8<-- "examples/get__ValueType_const.output"
```
??? example "Example: (2) explicit conversion to another basic_json specialization"
The example below shows how a `json` value is converted to an `ordered_json` value using `get<BasicJsonType>()`.
```cpp
--8<-- "examples/get__BasicJsonType.cpp"
```
Output:
```json
--8<-- "examples/get__BasicJsonType.output"
```
??? example "Example: (3) explicit pointer access to the stored value"
The example below shows how pointers to internal values of a JSON value can be requested. Note that no type
conversions are made and a `#cpp nullptr` is returned if the value and the requested pointer type does not match.
```cpp
--8<-- "examples/get__PointerType.cpp"
```
Output:
```json
--8<-- "examples/get__PointerType.output"
```
See also
get_to- convert and write into a passed valueget_ptr- get a pointer to the stored valueget_ref- get a reference to the stored valueoperator ValueType- get a value via implicit conversion- Converting values - the type conversions article
basic_json_view::get- the same conversion on a zero-copy view (many types are converted without ever building abasic_jsonvalue)- Arbitrary Type Conversions - make your own types usable with
get
Version history
- Added in version 2.1.0.
- Added in version 2.1.0. Extended to work with other specializations of
basic_jsonin version 3.1.2. - Added in version 1.0.0.