mirror of
https://github.com/nlohmann/json.git
synced 2026-06-18 18:34:17 +00:00
Clean up and document project files (#4560)
This commit is contained in:
@@ -20,7 +20,7 @@ Checks whether the input is valid JSON.
|
||||
The value_type of the iterator must be an integral type with size of 1, 2 or 4 bytes, which will be interpreted
|
||||
respectively as UTF-8, UTF-16 and UTF-32.
|
||||
|
||||
Unlike the [`parse`](parse.md) function, this function neither throws an exception in case of invalid JSON input
|
||||
Unlike the [`parse()`](parse.md) function, this function neither throws an exception in case of invalid JSON input
|
||||
(i.e., a parse error) nor creates diagnostic information.
|
||||
|
||||
## Template parameters
|
||||
@@ -29,7 +29,7 @@ Unlike the [`parse`](parse.md) function, this function neither throws an excepti
|
||||
: A compatible input, for instance:
|
||||
|
||||
- an `std::istream` object
|
||||
- a `FILE` pointer (throws if null)
|
||||
- a `#!c FILE` pointer (throws if null)
|
||||
- a C-style array of characters
|
||||
- a pointer to a null-terminated string of single byte characters (throws if null)
|
||||
- a `std::string`
|
||||
@@ -66,7 +66,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
|
||||
## Exceptions
|
||||
|
||||
Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an empty input like a null `FILE*` or `char*` pointer.
|
||||
Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an empty input like a null `#!c FILE*` or `#!c char*` pointer.
|
||||
|
||||
## Complexity
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# <small>nlohmann::basic_json::</small>end_pos
|
||||
|
||||
```cpp
|
||||
#if JSON_DIAGNOSTIC_POSITIONS
|
||||
constexpr std::size_t end_pos() const noexcept;
|
||||
#endif
|
||||
```
|
||||
|
||||
Returns the position immediately following the last character of the JSON string from which the value was parsed from.
|
||||
|
||||
| JSON type | return value |
|
||||
|-----------|-----------------------------------|
|
||||
| object | position after the closing `}` |
|
||||
| array | position after the closing `]` |
|
||||
| string | position after the closing `"` |
|
||||
| number | position after the last character |
|
||||
| boolean | position after `e` |
|
||||
| null | position after `l` |
|
||||
|
||||
## Return value
|
||||
|
||||
the position of the character _following_ the last character of the given value in the parsed JSON string, if the
|
||||
value was created by the [`parse`](parse.md) function, or `std::string::npos` if the value was constructed otherwise
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this member function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! note "Note"
|
||||
|
||||
The function is only available if macro [`JSON_DIAGNOSTIC_POSITIONS`](../macros/json_diagnostic_positions.md) has
|
||||
been defined to `#!cpp 1` before including the library header.
|
||||
|
||||
!!! warning "Invalidation"
|
||||
|
||||
The returned positions are only valid as long as the JSON value is not changed. The positions are *not* updated
|
||||
when the JSON value is changed.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example"
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/diagnostic_positions.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
--8<-- "examples/diagnostic_positions.output"
|
||||
```
|
||||
|
||||
The output shows the start/end positions of all the objects and fields in the JSON string.
|
||||
|
||||
## See also
|
||||
|
||||
- [start_pos](start_pos.md) to access the start position
|
||||
- [JSON_DIAGNOSTIC_POSITIONS](../macros/json_diagnostic_positions.md) for an overview of the diagnostic positions
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.12.0.
|
||||
@@ -156,9 +156,9 @@ The class satisfies the following concept requirements:
|
||||
- [(constructor)](basic_json.md)
|
||||
- [(destructor)](~basic_json.md)
|
||||
- [**operator=**](operator=.md) - copy assignment
|
||||
- [**array**](array_t.md) (_static_) - explicitly create an array
|
||||
- [**array**](array.md) (_static_) - explicitly create an array
|
||||
- [**binary**](binary.md) (_static_) - explicitly create a binary array
|
||||
- [**object**](object_t.md) (_static_) - explicitly create an object
|
||||
- [**object**](object.md) (_static_) - explicitly create an object
|
||||
|
||||
### Object inspection
|
||||
|
||||
@@ -181,6 +181,11 @@ Functions to inspect the type of a JSON value.
|
||||
- [**is_binary**](is_binary.md) - return whether value is a binary array
|
||||
- [**is_discarded**](is_discarded.md) - return whether value is discarded
|
||||
|
||||
Optional functions to access the [diagnostic positions](../macros/json_diagnostic_positions.md).
|
||||
|
||||
- [**start_pos**](start_pos.md) - return the start position of the value
|
||||
- [**end_pos**](end_pos.md) - return the one past the end position of the value
|
||||
|
||||
### Value access
|
||||
|
||||
Direct access to the stored value of a JSON value.
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# <small>nlohmann::basic_json::</small>start_pos
|
||||
|
||||
```cpp
|
||||
#if JSON_DIAGNOSTIC_POSITIONS
|
||||
constexpr std::size_t start_pos() const noexcept;
|
||||
#endif
|
||||
```
|
||||
|
||||
Returns the position of the first character in the JSON string from which the value was parsed from.
|
||||
|
||||
| JSON type | return value |
|
||||
|-----------|------------------------------------------------|
|
||||
| object | position of the opening `{` |
|
||||
| array | position of the opening `[` |
|
||||
| string | position of the opening `"` |
|
||||
| number | position of the first character |
|
||||
| boolean | position of `t` for `true` and `f` for `false` |
|
||||
| null | position of `n` |
|
||||
|
||||
## Return value
|
||||
|
||||
the position of the first character of the value in the parsed JSON string, if the value was created by the
|
||||
[`parse`](parse.md) function, or `std::string::npos` if the value was constructed otherwise
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee: this member function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
Constant.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! note "Note"
|
||||
|
||||
The function is only available if macro [`JSON_DIAGNOSTIC_POSITIONS`](../macros/json_diagnostic_positions.md) has
|
||||
been defined to `#!cpp 1` before including the library header.
|
||||
|
||||
!!! warning "Invalidation"
|
||||
|
||||
The returned positions are only valid as long as the JSON value is not changed. The positions are *not* updated
|
||||
when the JSON value is changed.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example"
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/diagnostic_positions.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
--8<-- "examples/diagnostic_positions.output"
|
||||
```
|
||||
|
||||
The output shows the start/end positions of all the objects and fields in the JSON string.
|
||||
|
||||
## See also
|
||||
|
||||
- [end_pos](end_pos.md) to access the end position
|
||||
- [JSON_DIAGNOSTIC_POSITIONS](../macros/json_diagnostic_positions.md) for an overview of the diagnostic positions
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.12.0.
|
||||
@@ -11,6 +11,7 @@ header. See also the [macro overview page](../../features/macros.md).
|
||||
|
||||
- [**JSON_CATCH_USER(exception)**<br>**JSON_THROW_USER(exception)**<br>**JSON_TRY_USER**](json_throw_user.md) - control exceptions
|
||||
- [**JSON_DIAGNOSTICS**](json_diagnostics.md) - control extended diagnostics
|
||||
- [**JSON_DIAGNOSTIC_POSITIONS**](json_diagnostic_positions.md) - access positions of elements
|
||||
- [**JSON_NOEXCEPTION**](json_noexception.md) - switch off exceptions
|
||||
|
||||
## Language support
|
||||
@@ -49,27 +50,34 @@ header. See also the [macro overview page](../../features/macros.md).
|
||||
|
||||
## Serialization/deserialization macros
|
||||
|
||||
- Enum: [**NLOHMANN_JSON_SERIALIZE_ENUM**](nlohmann_json_serialize_enum.md)
|
||||
- Class/struct:
|
||||
- Do you need to serialize private variables?
|
||||
- Yes? Do you only need serialization?
|
||||
- Yes? `NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE`
|
||||
- No? Allow deserialization of JSON values with missing values?
|
||||
- Yes? `NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT`
|
||||
- No? `NLOHMANN_DEFINE_TYPE_INTRUSIVE`
|
||||
- No? Do you only need serialization?
|
||||
- Yes? `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE`
|
||||
- No? Allow deserialization of JSON values with missing values?
|
||||
- Yes? `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT`
|
||||
- No? `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE`
|
||||
### Enums
|
||||
|
||||
- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE(type, member...)**<br>**NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(type, member...)**
|
||||
<br>**NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(type, member...)**][DefInt]
|
||||
\- serialization/deserialization of types _with_ access to private variables
|
||||
- [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(type, member...)**<br>**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(type, member...)**
|
||||
<br>**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(type, member...)**][DefNonInt]
|
||||
\- serialization/deserialization of types _without_ access to private variables
|
||||
- [**NLOHMANN_JSON_SERIALIZE_ENUM(type, ...)**](nlohmann_json_serialize_enum.md) - serialization/deserialization of enum types
|
||||
- [**NLOHMANN_JSON_SERIALIZE_ENUM**](nlohmann_json_serialize_enum.md) - serialize/deserialize an enum
|
||||
|
||||
[DefInt]: nlohmann_define_type_intrusive.md
|
||||
[DefNonInt]: nlohmann_define_type_non_intrusive.md
|
||||
### Classes and structs
|
||||
|
||||
- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE**](nlohmann_define_type_intrusive.md) - serialize/deserialize a non-derived class
|
||||
with private members
|
||||
- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT**](nlohmann_define_type_intrusive.md) - serialize/deserialize a
|
||||
non-derived class with private members; uses default values
|
||||
- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE**](nlohmann_define_type_intrusive.md) - serialize a non-derived class
|
||||
with private members
|
||||
- [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE**](nlohmann_define_type_non_intrusive.md) - serialize/deserialize a non-derived
|
||||
class
|
||||
- [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT**](nlohmann_define_type_non_intrusive.md) - serialize/deserialize a
|
||||
non-derived class; uses default values
|
||||
- [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE**](nlohmann_define_type_non_intrusive.md) - serialize a
|
||||
non-derived class
|
||||
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE**](nlohmann_define_derived_type.md) - serialize/deserialize a derived class
|
||||
with private members
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT**](nlohmann_define_derived_type.md) - serialize/deserialize a
|
||||
derived class with private members; uses default values
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE**](nlohmann_define_derived_type.md) - serialize a derived
|
||||
class with private members
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE**](nlohmann_define_derived_type.md) - serialize/deserialize a derived
|
||||
class
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT**](nlohmann_define_derived_type.md) - serialize/deserialize
|
||||
a derived class; uses default values
|
||||
- [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE**](nlohmann_define_derived_type.md) - serialize a derived
|
||||
class
|
||||
|
||||
@@ -6,22 +6,34 @@
|
||||
|
||||
This macro enables position diagnostics for generated JSON objects.
|
||||
|
||||
When enabled, two new properties: `start_pos()` and `end_pos()` are added to `nlohmann::basic_json` objects and fields. `start_pos()` returns the start
|
||||
position of that JSON object/field in the original string the object was parsed from. Likewise, `end_pos()` returns the end position of that JSON
|
||||
object/field in the original string the object was parsed from.
|
||||
When enabled, two new member functions [`start_pos()`](../basic_json/start_pos.md) and
|
||||
[`end_pos()`](../basic_json/end_pos.md) are added to [`basic_json`](../basic_json/index.md) values. If the value was
|
||||
created by calling the[`parse`](../basic_json/parse.md) function, then these functions allow to query the byte positions
|
||||
of the value in the input it was parsed from. In case the value was constructed by other means, `std::string::npos` is
|
||||
returned.
|
||||
|
||||
`start_pos()` returns the first character of a given element in the original JSON string, while `end_pos()` returns the character following the last
|
||||
character. For objects and arrays, the first and last characters correspond to the opening or closing braces/brackets, respectively. For fields, the first
|
||||
and last character represent the opening and closing quotes or the first and last character of the field's numerical or predefined value
|
||||
(true/false/null), respectively.
|
||||
[`start_pos()`](../basic_json/start_pos.md) returns the position of the first character of a given value in the original
|
||||
JSON string, while [`end_pos()`](../basic_json/end_pos.md) returns the position of the character _following_ the last
|
||||
character. For objects and arrays, the first and last characters correspond to the opening or closing braces/brackets,
|
||||
respectively. For primitive values, the first and last character represent the opening and closing quotes (strings) or
|
||||
the first and last character of the field's numerical or predefined value (`true`, `false`, `null`), respectively.
|
||||
|
||||
Given the above, `end_pos() - start_pos()` for an object or field provides the length of the string representation for that object or field, including the
|
||||
opening or closing braces, brackets, or quotes.
|
||||
| JSON type | return value [`start_pos()`](../basic_json/start_pos.md) | return value [`end_pos()`](../basic_json/end_pos.md) |
|
||||
|-----------|----------------------------------------------------------|------------------------------------------------------|
|
||||
| object | position of the opening `{` | position after the closing `}` |
|
||||
| array | position of the opening `[` | position after the closing `]` |
|
||||
| string | position of the opening `"` | position after the closing `"` |
|
||||
| number | position of the first character | position after the last character |
|
||||
| boolean | position of `t` for `true` and `f` for `false` | position after `e` |
|
||||
| null | position of `n` | position after `l` |
|
||||
|
||||
`start_pos()` and `end_pos()` are only set if the JSON object was parsed using `parse()`. For all other cases, `std::string::npos` will be returned.
|
||||
Given the above, [`end_pos()`](../basic_json/end_pos.md)` - `[`start_pos()`](../basic_json/start_pos.md) for a JSON
|
||||
value provides the length of the parsed JSON string for that value, including the opening or closing braces, brackets,
|
||||
or quotes.
|
||||
|
||||
Note that enabling this macro increases the size of every JSON value by two `std::size_t` fields and adds
|
||||
slight runtime overhead.
|
||||
Note that enabling this macro increases the size of every JSON value by two `std::size_t` fields and adds slight runtime
|
||||
overhead to parsing, copying JSON value objects, and the generation of error messages for exceptions. It also causes
|
||||
these values to be reported in those error messages.
|
||||
|
||||
## Default definition
|
||||
|
||||
@@ -35,15 +47,27 @@ When the macro is not defined, the library will define it to its default value.
|
||||
|
||||
## Notes
|
||||
|
||||
!!! hint "CMake option"
|
||||
!!! note "CMake option"
|
||||
|
||||
Diagnostic messages can also be controlled with the CMake option
|
||||
Diagnostic positions can also be controlled with the CMake option
|
||||
[`JSON_Diagnostic_Positions`](../../integration/cmake.md#json_diagnostic_positions) (`OFF` by default)
|
||||
which defines `JSON_DIAGNOSTIC_POSITIONS` accordingly.
|
||||
|
||||
!!! note "Availability"
|
||||
|
||||
Diagnostic positions are only available if the value was created by the [`parse`](../basic_json/parse.md) function.
|
||||
The [`sax_parse`](../basic_json/sax_parse.md) function or all other means to create a JSON value **do not** set the
|
||||
diagnostic positions and [`start_pos()`](../basic_json/start_pos.md) and [`end_pos()`](../basic_json/end_pos.md)
|
||||
will only return `std::string::npos` for these values.
|
||||
|
||||
!!! warning "Invalidation"
|
||||
|
||||
The returned positions are only valid as long as the JSON value is not changed. The positions are *not* updated
|
||||
when the JSON value is changed.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example 1: retrieving positions"
|
||||
??? example "Example: retrieving positions"
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/diagnostic_positions.cpp"
|
||||
@@ -59,3 +83,4 @@ When the macro is not defined, the library will define it to its default value.
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.12.0.
|
||||
|
||||
@@ -3,13 +3,19 @@
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE</h1>
|
||||
|
||||
```cpp
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(type, base_type, member...) // (1)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(type, base_type, member...) // (2)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...) // (3)
|
||||
// (1)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(type, base_type, member...)
|
||||
// (2)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(type, base_type, member...)
|
||||
// (3)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...)
|
||||
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(type, base_type, member...) // (4)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(type, base_type, member...) // (5)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...) // (6)
|
||||
// (4)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(type, base_type, member...)
|
||||
// (5)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(type, base_type, member...)
|
||||
// (6)
|
||||
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...)
|
||||
```
|
||||
|
||||
These macros can be used to simplify the serialization/deserialization of derived types if you want to use a JSON
|
||||
@@ -25,12 +31,23 @@ The first parameter is the name of the derived class/struct,
|
||||
the second parameter is the name of the base class/struct and all remaining parameters name the members.
|
||||
The base type **must** be already serializable/deserializable.
|
||||
|
||||
- Macros 1 and 3 will use [`at`](../basic_json/at.md) during deserialization and will throw
|
||||
- Macros 1 and 4 will use [`at`](../basic_json/at.md) during deserialization and will throw
|
||||
[`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a key is missing in the JSON object.
|
||||
- Macros 2 and 4 will use [`value`](../basic_json/value.md) during deserialization and fall back to the default value for the
|
||||
- Macros 2 and 5 will use [`value`](../basic_json/value.md) during deserialization and fall back to the default value for the
|
||||
respective type of the member variable if a key in the JSON object is missing. The generated `from_json()` function
|
||||
default constructs an object and uses its values as the defaults when calling the `value` function.
|
||||
|
||||
Summary:
|
||||
|
||||
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro |
|
||||
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|---------------------------------------------------------------|
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE** |
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT** |
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE** |
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE** |
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT** |
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE** |
|
||||
|
||||
## Parameters
|
||||
|
||||
`type` (in)
|
||||
@@ -64,7 +81,7 @@ Macros 3 and 6 add one function to the namespace which take care of the serializ
|
||||
void to_json(nlohmann::json&, const type&);
|
||||
```
|
||||
|
||||
In first two cases cases they call the `to_json`/`from_json` functions of the base type
|
||||
In first two cases, they call the `to_json`/`from_json` functions of the base type
|
||||
before serializing/deserializing the members of the derived type:
|
||||
|
||||
```cpp
|
||||
@@ -82,7 +99,7 @@ void from_json(const nlohmann::json& j, B& b) {
|
||||
}
|
||||
```
|
||||
|
||||
In the third case only `to_json` will be called:
|
||||
In the third case, only `to_json` will be called:
|
||||
|
||||
```cpp
|
||||
class A { /* ... */ };
|
||||
@@ -98,40 +115,55 @@ void to_json(nlohmann::json& j, const B& b) {
|
||||
|
||||
!!! info "Prerequisites"
|
||||
|
||||
- Macros 1, 2 and 3 have the same prerequisites of NLOHMANN_DEFINE_TYPE_INTRUSIVE.
|
||||
- Macros 4, 5 and 6 have the same prerequisites of NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
|
||||
- Macros 1, 2, and 3 have the same prerequisites of [NLOHMANN_DEFINE_TYPE_INTRUSIVE](nlohmann_define_type_intrusive.md).
|
||||
- Macros 4, 5, and 6 have the same prerequisites of [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE](nlohmann_define_type_non_intrusive.md).
|
||||
- Serialization/deserialization of base types must be defined.
|
||||
|
||||
!!! warning "Implementation limits"
|
||||
|
||||
- See Implementation limits for NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
|
||||
See Implementation limits for [NLOHMANN_DEFINE_TYPE_INTRUSIVE](nlohmann_define_type_intrusive.md) and
|
||||
[NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE](nlohmann_define_type_non_intrusive.md), respectively.
|
||||
|
||||
## Examples
|
||||
|
||||
Example of `NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE` usage:
|
||||
??? example "NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE"
|
||||
|
||||
```cpp
|
||||
class A {
|
||||
double Aa;
|
||||
double Ab;
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(A, Aa, Ab)
|
||||
};
|
||||
Consider the following complete example:
|
||||
|
||||
class B : public A {
|
||||
int Ba;
|
||||
int Bb;
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(B, A, Ba, Bb)
|
||||
};
|
||||
```
|
||||
```cpp hl_lines="28"
|
||||
--8<-- "examples/nlohmann_define_derived_type_intrusive_macro.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/nlohmann_define_derived_type_intrusive_macro.output"
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- `A` and `B` are default-constructible. This is a requirement for using the macro.
|
||||
- `A` has private members and is not a derived class. Hence, macro `NLOHMANN_DEFINE_TYPE_INTRUSIVE` is used.
|
||||
- As `B` is a derived class, `NLOHMANN_DEFINE_TYPE_INTRUSIVE` is not applicable, but
|
||||
`NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE` must be used.
|
||||
- The macro `NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE` is used _inside_ the class use as
|
||||
`NLOHMANN_DEFINE_TYPE_INTRUSIVE`.
|
||||
|
||||
## See also
|
||||
|
||||
- [NLOHMANN_DEFINE_TYPE_INTRUSIVE / NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT](nlohmann_define_type_intrusive.md)
|
||||
- [NLOHMANN_DEFINE_TYPE_INTRUSIVE / NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT /
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_intrusive.md)
|
||||
for similar macros that can be defined _inside_ a non-derived type.
|
||||
- [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT](nlohmann_define_type_non_intrusive.md)
|
||||
- [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT /
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_non_intrusive.md)
|
||||
for similar macros that can be defined _outside_ a non-derived type.
|
||||
- [Arbitrary Type Conversions](../../features/arbitrary_types.md) for an overview.
|
||||
|
||||
## Version history
|
||||
|
||||
1. Added in version 3.11.x.
|
||||
2. Added in version 3.11.x.
|
||||
3. Added in version 3.11.x.
|
||||
4. Added in version 3.11.x.
|
||||
5. Added in version 3.11.x.
|
||||
6. Added in version 3.11.x.
|
||||
|
||||
@@ -19,6 +19,14 @@ parameter is the name of the class/struct, and all remaining parameters name the
|
||||
default constructs an object and uses its values as the defaults when calling the `value` function.
|
||||
3. Only defines the serialization. Useful in cases when the type does not have a default constructor and only serialization in required.
|
||||
|
||||
Summary:
|
||||
|
||||
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro |
|
||||
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------|
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE** |
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT** |
|
||||
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE** |
|
||||
|
||||
## Parameters
|
||||
|
||||
`type` (in)
|
||||
@@ -145,12 +153,18 @@ See examples below for the concrete generated code.
|
||||
|
||||
## See also
|
||||
|
||||
- [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE{_WITH_DEFAULT, _ONLY_SERIALIZE}](nlohmann_define_type_non_intrusive.md)
|
||||
- [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_non_intrusive.md)
|
||||
for a similar macro that can be defined _outside_ the type.
|
||||
- [NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_derived_type.md) for similar macros for
|
||||
derived types
|
||||
- [Arbitrary Type Conversions](../../features/arbitrary_types.md) for an overview.
|
||||
|
||||
## Version history
|
||||
|
||||
1. Added in version 3.9.0.
|
||||
2. Added in version 3.11.0.
|
||||
3. Added in version TODO.
|
||||
3. Added in version 3.11.3.
|
||||
|
||||
@@ -19,6 +19,14 @@ parameter is the name of the class/struct, and all remaining parameters name the
|
||||
default constructs an object and uses its values as the defaults when calling the `value` function.
|
||||
3. Only defines the serialization. Useful in cases when the type does not have a default constructor and only serialization in required.
|
||||
|
||||
Summary:
|
||||
|
||||
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro |
|
||||
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------|
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE** |
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT** |
|
||||
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE** |
|
||||
|
||||
## Parameters
|
||||
|
||||
`type` (in)
|
||||
@@ -146,12 +154,18 @@ See examples below for the concrete generated code.
|
||||
|
||||
## See also
|
||||
|
||||
- [NLOHMANN_DEFINE_TYPE_INTRUSIVE{_WITH_DEFAULT, _ONLY_SERIALIZE}](nlohmann_define_type_intrusive.md)
|
||||
- [NLOHMANN_DEFINE_TYPE_INTRUSIVE, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_intrusive.md)
|
||||
for a similar macro that can be defined _inside_ the type.
|
||||
- [NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT,
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_derived_type.md) for similar macros for
|
||||
derived types
|
||||
- [Arbitrary Type Conversions](../../features/arbitrary_types.md) for an overview.
|
||||
|
||||
## Version history
|
||||
|
||||
1. Added in version 3.9.0.
|
||||
2. Added in version 3.11.0.
|
||||
3. Added in version TODO.
|
||||
3. Added in version 3.11.3.
|
||||
|
||||
Reference in New Issue
Block a user