diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index 2dc80766..0e2c3005 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -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(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)