You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`key_pressed_t` will now be serialized as follows:
112
+
113
+
```json
114
+
{"your_custom_tag":{"key":99}}
115
+
```
116
+
64
117
### `rfl::AllowRawPtrs`
65
118
66
119
By default, reflect-cpp does not allow *reading into* raw pointers. (*Writing from* raw pointers is never a problem.) This is because reading into raw pointers means that the library will allocate memory that the user then has to manually delete. This can lead to misunderstandings and memory leaks.
Copy file name to clipboardExpand all lines: docs/variants_and_tagged_unions.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,9 +45,21 @@ several problems with this:
45
45
2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read.
46
46
3) It is dangerous. Imagine we had written `rfl::Variant<Circle, Square, Rectangle>` instead of `rfl::Variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
47
47
48
+
## Automatic tags
49
+
50
+
The easiest way to solve this problem is to simply add tags automatically. You can do so by using `rfl::AddTagsToVariants`:
51
+
52
+
```cpp
53
+
const auto json_string = rfl::json::write<rfl::AddTagsToVariants>(r);
54
+
55
+
const auto r2 = rfl::json::read<Shapes, rfl::AddTagsToVariants>(json_string);
56
+
```
57
+
58
+
Please refer to the section on processors in this documentation for more information.
59
+
48
60
## `rfl::TaggedUnion` (internally tagged)
49
61
50
-
One way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
62
+
Another way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
51
63
52
64
TaggedUnions use the name of the struct as an identifying tag. It will then try to take that field from the JSON object, match it to the correct alternative and then only parse the correct alternative.
0 commit comments