mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
dropped anonymous_fields.md and nav entry
This commit is contained in:
@@ -1,75 +0,0 @@
|
|||||||
# Anonymous fields
|
|
||||||
|
|
||||||
If you do not want to serialize field names, you do not have to use `rfl::Field`. You can just write your struct like this:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
struct Person {
|
|
||||||
std::string first_name;
|
|
||||||
std::string last_name;
|
|
||||||
std::vector<Person> children;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
This is referred to as anonymous fields. It is particularly relevant if you are using a binary format.
|
|
||||||
|
|
||||||
This can be fully serialized and deserialized using `rfl::json::read` and `rfl::json::write`. No further annotations are needed.
|
|
||||||
|
|
||||||
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]
|
|
||||||
```
|
|
||||||
|
|
||||||
@@ -34,8 +34,6 @@
|
|||||||
|
|
||||||
[rfl::Binary, rfl::Hex and rfl::Oct](number_systems.md)- For expressing numbers in different formats.
|
[rfl::Binary, rfl::Hex and rfl::Oct](number_systems.md)- For expressing numbers in different formats.
|
||||||
|
|
||||||
[Anonymous fields](anonymous_fields.md) - For defining fields without using `rfl::Field`.
|
|
||||||
|
|
||||||
## Validation
|
## Validation
|
||||||
|
|
||||||
[Regex patterns](patterns.md) - For requiring that strings follow used-defined regex patterns.
|
[Regex patterns](patterns.md) - For requiring that strings follow used-defined regex patterns.
|
||||||
@@ -74,24 +72,6 @@
|
|||||||
|
|
||||||
[rfl::to_view](to_view.md) - For accessing fields of a struct by index or name.
|
[rfl::to_view](to_view.md) - For accessing fields of a struct by index or name.
|
||||||
|
|
||||||
<!-- ## 6) Supported formats
|
|
||||||
|
|
||||||
[JSON](json.md)
|
|
||||||
|
|
||||||
[BSON](bson.md)
|
|
||||||
|
|
||||||
[CBOR](cbor.md)
|
|
||||||
|
|
||||||
[flexbuffers](flexbuffers.md)
|
|
||||||
|
|
||||||
[msgpack](msgpack.md)
|
|
||||||
|
|
||||||
[TOML](toml.md)
|
|
||||||
|
|
||||||
[XML](xml.md)
|
|
||||||
|
|
||||||
[YAML](yaml.md) -->
|
|
||||||
|
|
||||||
## Advanced topics
|
## Advanced topics
|
||||||
|
|
||||||
[Supporting your own format](supported_formats/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
[Supporting your own format](supported_formats/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
||||||
|
|||||||
@@ -124,7 +124,6 @@ nav:
|
|||||||
- C arrays and inheritance: c_arrays_and_inheritance.md
|
- C arrays and inheritance: c_arrays_and_inheritance.md
|
||||||
- rfl::Bytestring: bytestring.md
|
- rfl::Bytestring: bytestring.md
|
||||||
- rfl::Binary, rfl::Hex and rfl::Oct: number_systems.md
|
- rfl::Binary, rfl::Hex and rfl::Oct: number_systems.md
|
||||||
- Anonymous fields: anonymous_fields.md
|
|
||||||
- Validation:
|
- Validation:
|
||||||
- Regex patterns: patterns.md
|
- Regex patterns: patterns.md
|
||||||
- Validating numbers: validating_numbers.md
|
- Validating numbers: validating_numbers.md
|
||||||
|
|||||||
Reference in New Issue
Block a user