Added rfl::Skip; resolves #99 (#103)

This commit is contained in:
liuzicheng1987
2024-05-21 18:13:42 +02:00
committed by GitHub
parent 579ec6e919
commit adf8463527
12 changed files with 389 additions and 5 deletions
+4 -2
View File
@@ -24,9 +24,11 @@
1.10) [rfl::Timestamp](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps.
1.11) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions.
1.11) [rfl::Skip](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_skip.md) - For skipping fields during serialization and/or deserialization.
1.12) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library.
1.12) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions.
1.13) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library.
## 2) Validation
+57
View File
@@ -0,0 +1,57 @@
# `rfl::Skip`
It is possible to skip the serialization and deserialization of fields using `rfl::Skip`.
```cpp
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Skip<std::string> town;
Age age;
};
```
This means that the field `town` will be ignored:
```json
{"firstName":"Homer","lastName":"Simpson","age":45}
```
If you only want to skip the deserialization, but not the serialization, you can use `rfl::SkipDeserialization`:
```cpp
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::SkipDeserialization<std::string> town;
Age age;
};
```
If you only want to skip the serialization, but not the deserialization, you can use `rfl::SkipSerialization`.
Note that this implies that the serializied data cannot be deserialized, because the field `town`
is expected during deserialization, but not serialized.
```cpp
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::SkipSerialization<std::string> town;
Age age;
};
```
You can access the underlying value in the field `town` using any of the following operators:
```cpp
person.town();
person.town.get();
person.town.value();
```
You can assign the underlying field just like any other field:
```cpp
person.town = "Springfield";
```