From 7df42fd05c8c0e13ce3c46eb0c7a89fc66ccef95 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Tue, 7 Nov 2023 19:00:51 +0100 Subject: [PATCH] Included anonymous fields in README --- README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/README.md b/README.md index 68ac3d7d..650c85d6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Design principles for reflect-cpp include: - Simple extendability to custom classes - No macros +## Example + ```cpp #include #include @@ -107,6 +109,8 @@ std::cout << "Hello, my name is " << homer2.first_name() << " " << homer2.last_name() << "." << std::endl; ``` +## Error messages + reflect-cpp returns clear and comprehensive error messages: ```cpp @@ -126,6 +130,58 @@ Found 5 errors: 5) Field named 'children' not found. ``` +## Anonymous fields + +`rfl::Field` is only necessary if you want to serialize field names. If you don't (possibly because you are using a binary format), you do not need to annotate your fields: + +```cpp +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + std::string first_name; + std::string last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + +const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + +const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + +const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; +``` + +This results in the following JSON string: + +```json +["Homer","Simpson","1987-04-19",45,"homer@simpson.com",[["Bart","Simpson","1987-04-19",10,"bart@simpson.com",[]],["Lisa","Simpson","1987-04-19",8,"lisa@simpson.com",[]],["Maggie","Simpson","1987-04-19",0,"maggie@simpson.com",[]]]] +``` + + + ## Support for containers ### C++ standard library