1- # NLOHMANN_DEFINE_TYPE_INTRUSIVE, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT
1+ # NLOHMANN_DEFINE_TYPE_INTRUSIVE, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE
22
33``` cpp
44#define NLOHMANN_DEFINE_TYPE_INTRUSIVE (type, member... ) // (1)
55#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT (type, member... ) // (2)
6+ #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(type, member...) // (3)
67```
78
89These macros can be used to simplify the serialization/deserialization of types if you want to use a JSON object as
@@ -16,6 +17,7 @@ parameter is the name of the class/struct, and all remaining parameters name the
16172. Will use [`value`](../basic_json/value.md) during deserialization and fall back to the default value for the
1718 respective type of the member variable if a key in the JSON object is missing. The generated `from_json()` function
1819 default constructs an object and uses its values as the defaults when calling the `value` function.
20+ 3. Only defines the serialization. Useful in cases when the type does not have a default constructor and only serialization in required.
1921
2022## Parameters
2123
@@ -31,7 +33,7 @@ The macros add two friend functions to the class which take care of the serializ
3133
3234```cpp
3335friend void to_json(nlohmann::json&, const type&);
34- friend void from_json(const nlohmann::json&, type&);
36+ friend void from_json(const nlohmann::json&, type&); // except (3)
3537```
3638
3739See examples below for the concrete generated code.
@@ -40,7 +42,7 @@ See examples below for the concrete generated code.
4042
4143!!! info " Prerequisites"
4244
43- 1 . The type `type` must be default constructible. See [How can I use `get()` for non-default
45+ 1 . The type `type` must be default constructible (except (3)) . See [ How can I use ` get() ` for non-default
4446 constructible/non-copyable types?] [ GetNonDefNonCopy ] for how to overcome this limitation.
4547 2. The macro must be used inside the type (class/struct).
4648
@@ -108,19 +110,47 @@ See examples below for the concrete generated code.
108110
109111 The macro is equivalent to:
110112
111- ```cpp hl_lines="21 22 23 24 25 26 27 28 29 30 31 32 33 34 35"
113+ ```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34 35"
112114 --8<-- "examples/nlohmann_define_type_intrusive_with_default_explicit.cpp"
113115 ```
114116
115117 Note how a default-initialized `person` object is used in the `from_json` to fill missing values.
116118
119+ ??? example "Example (3): NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE"
120+ Consider the following complete example:
121+
122+ ```cpp hl_lines="22"
123+ --8<-- "examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp"
124+ ```
125+
126+ Output:
127+
128+ ```json
129+ --8<-- "examples/nlohmann_define_type_intrusive_only_serialize_macro.output"
130+ ```
131+
132+ Notes:
133+
134+ - `ns::person` is non-default-constructible. This allows this macro to be used instead of
135+ `NLOHMANN_DEFINE_TYPE_INTRUSIVE` and `NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT`.
136+ - `ns::person` has private member variables. This makes `NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE` applicable, but not
137+ `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE`.
138+ - The macro `NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE` is used _inside_ the class.
139+
140+ The macro is equivalent to:
141+
142+ ```cpp hl_lines="22 22 23 24 25 26 27"
143+ --8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
144+ ```
145+
117146## See also
118147
119- - [ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT ] ( nlohmann_define_type_non_intrusive.md )
148+ - [ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE{ _ WITH_DEFAULT, _ ONLY_SERIALIZE} ] ( nlohmann_define_type_non_intrusive.md )
120149 for a similar macro that can be defined _ outside_ the type.
121150- [ Arbitrary Type Conversions] ( ../../features/arbitrary_types.md ) for an overview.
122151
123152## Version history
124153
1251541 . Added in version 3.9.0.
1261552 . Added in version 3.11.0.
156+ 3 . Added in version TODO.
0 commit comments