mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Documented the capabilities of tags
This commit is contained in:
@@ -81,23 +81,40 @@ It is also possible to set the tag explicitly:
|
||||
|
||||
```cpp
|
||||
struct Circle {
|
||||
using Tag = rfl::Literal<"Circle">;
|
||||
using Tag = rfl::Literal<"circle", "Circle">;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
using Tag = rfl::Literal<"Rectangle">;
|
||||
using Tag = rfl::Literal<"rectangle", "Rectangle">;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
using Tag = rfl::Literal<"Square">;
|
||||
using Tag = rfl::Literal<"square", "Square">;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
// The remainder is the same as above
|
||||
...
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
const auto json_string = rfl::json::write(r);
|
||||
|
||||
const auto r2 = rfl::json::read<Shapes>(json_string);
|
||||
```
|
||||
|
||||
The JSON generated by `rfl::json::write` looks like this:
|
||||
|
||||
```json
|
||||
{"shape":"rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
However, `rfl::json::read` would also accept this:
|
||||
|
||||
```json
|
||||
{"shape":"Rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
## `std::variant` or `rfl::Variant` (externally tagged)
|
||||
|
||||
Reference in New Issue
Block a user