@@ -45,8 +45,7 @@ reflect-cpp and sqlgen fill important gaps in C++ development. They reduce boile
4545 - [ JSON schema] ( #json-schema )
4646 - [ Enums] ( #enums )
4747 - [ Algebraic data types] ( #algebraic-data-types )
48- - [ Extra fields] ( #extra-fields )
49- - [ std::expected] ( #stdexpected )
48+ - [ Extra fields] ( #extra-fields )
5049 - [ Reflective programming] ( #reflective-programming )
5150 - [ Standard Library Integration] ( #support-for-containers )
5251 - [ The team behind reflect-cpp] ( #the-team-behind-reflect-cpp )
@@ -510,52 +509,6 @@ This results in the following JSON string:
510509{"firstName":"Homer","lastName":"Simpson","age":45,"email":"homer@simpson.com","town":"Springfield"}
511510```
512511
513- ### std::expected
514-
515- reflect-cpp also supports C++-23's ` std::expected ` :
516-
517- ``` cpp
518- #include < expected>
519- #include < rfl/json.hpp>
520-
521- // A success value is serialized like the value itself:
522- const std::expected<int , std::string> age = 45 ;
523- const std::string json_string = rfl::json::write(age);
524- // -> 45
525-
526- // An error is serialized as an object with a single "error" field:
527- const std::expected<int , std::string> no_age = std::unexpected(" unknown age" );
528- const std::string json_string2 = rfl::json::write(no_age);
529- // -> {"error":"unknown age"}
530-
531- const auto age2 =
532- rfl::json::read<std::expected<int , std::string>>(json_string).value();
533- const auto no_age2 =
534- rfl::json::read<std::expected<int , std::string>>(json_string2).value();
535- ```
536-
537- ` std::expected ` can be used anywhere other types can be used, for example as a
538- field of a struct or inside a container:
539-
540- ``` cpp
541- struct Person {
542- std::string first_name;
543- std::vector<std::expected<int, std::string>> ages;
544- };
545-
546- const auto homer =
547- Person{.first_name = "Homer",
548- .ages = {42, std::unexpected("unknown age")}};
549-
550- const std::string json_string3 = rfl::json::write(homer);
551- // -> {"first_name":"Homer","ages":[ 42,{"error":"unknown age"}] }
552- ```
553-
554- `std::expected` requires a standard library that provides the C++-23 feature
555- (feature-test macro `__cpp_lib_expected`). Note that `std::expected<void, E>` and
556- `std::expected<T, T>` with an identical value and error type are not supported.
557- Refer to the [documentation](https://rfl.getml.com/expected) for details.
558-
559512### Reflective programming
560513
561514Beyond serialization and deserialization, reflect-cpp also supports reflective programming in general.
0 commit comments