std::tuple can also be flattened

This commit is contained in:
liuzicheng1987
2023-11-15 21:55:46 +01:00
committed by GitHub
parent f3cd5d04b4
commit 330c24959f
+53
View File
@@ -19,4 +19,57 @@ However, there are a two things you have to keep in mind:
1) If you annotate *some* of the fields using either `rfl::Field`, then you must annotate *all* of them using one of these two.
2) If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
## Struct flattening with anonymous fields
Struct flattening also works with anonymous fields:
```cpp
struct Person {
std::string first_name;
std::string last_name;
int age;
};
struct Employee {
rfl::Flatten<Person> person;
std::string employer;
float salary;
};
const auto employee = Employee{
.person = Person{.first_name = "Homer",
.last_name = "Simpson",
.age = 45},
.employer = "Mr. Burns",
.salary = 60000.0};
```
This will result in the following JSON:
```json
["Homer","Simpson",45,"Mr. Burns",60000.0]
```
You can even include `std::tuple` (but `std::tuple` itself cannot contain `rfl::Flatten`):
```cpp
using Person = std::tuple<std::string, std::string, int>;
struct Employee {
rfl::Flatten<Person> person;
std::string employer;
float salary;
};
const auto employee = Employee{
.person = Person{"Homer", "Simpson", 45},
.employer = "Mr. Burns",
.salary = 60000.0};
```
This will result in the same JSON:
```json
["Homer","Simpson",45,"Mr. Burns",60000.0]
```