Documented the capabilities of tags

This commit is contained in:
Patrick Urbanke
2023-11-29 19:54:28 +01:00
parent 72b2f9dcc8
commit 9f1ee3fe9b
+22 -5
View File
@@ -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)