diff --git a/docs/optional_fields.md b/docs/optional_fields.md index ba7f91cf..fcad5e8f 100644 --- a/docs/optional_fields.md +++ b/docs/optional_fields.md @@ -13,7 +13,8 @@ But Bart doesn't have any children. After all, he's only 10 years old. To indica we can use either `std::optional`, `std::shared_ptr` or `std::unique_ptr`. If `std::optional` is `std::nullopt` or any of the smart pointers is a `nullptr`, -then the field will not be written to the JSON string. Likewise, when you read the JSON string, the fields will not be required. +then the field will not be written to the JSON string. Likewise, when you read the JSON string, the fields will not be required. If you +want the fields to be required, you can use the `rfl::NoOptionals` processor, please refer to the section on processors. So this is how we could rewrite the previous example: diff --git a/docs/processors.md b/docs/processors.md index b6528aa7..28650e6d 100644 --- a/docs/processors.md +++ b/docs/processors.md @@ -29,21 +29,16 @@ The resulting JSON string looks like this: {"firstName":"Homer","lastName":"Simpson","age":45} ``` -If you want `PascalCase` instead, you can use the appropriate processor: +## Supported processors -```cpp -const auto json_string = - rfl::json::write(homer); +reflect-cpp currently supports the following processors: -const auto homer2 = - rfl::json::read(json_string).value(); -``` +- `rfl::AddStructName` +- `rfl::NoOptionals` +- `rfl::SnakeCaseToCamelCase` +- `rfl::SnakeCaseToPascalCase` -The resulting JSON string looks like this: - -```json -{"FirstName":"Homer","LastName":"Simpson","Age":45} -``` +### `rfl::AddStructName` It is also possible to add the struct name as an addtional field, like this: @@ -61,7 +56,64 @@ The resulting JSON string looks like this: {"type":"Person","first_name":"Homer","last_name":"Simpson","age":45} ``` -You can also combine several processors: +### `rfl::NoOptionals` + +As we have seen in the section on optional fields, when a `std::optional` is +`std::nullopt`, it is usually not written at all. But if you want them to be explicitly +written as `null`, you can use this processor. The same thing applies to `std::shared_ptr` and +`std::unique_ptr`. + +```cpp +struct Person { + std::string first_name; + std::string last_name = "Simpson"; + std::optional town = std::nullopt; +}; + +const auto homer = Person{.first_name = "Homer"}; + +rfl::json::write(homer); +``` + +The resulting JSON string looks like this: + +```json +{"first_name":"Homer","last_name":"Simpson","town":null} +``` + +By default, `rfl::json::read` will accept both `"town":null` and just +leaving out the field `town`. However, if you want to require the field +`town` to be included, you can add `rfl::NoOptionals` to `read`: + +``` +rfl::json::read(json_string); +``` + +### `rfl::SnakeCaseToCamelCase` + +Please refer to the example above. + +### `rfl::SnakeCaseToPascalCase` + +If you want `PascalCase` instead of `camelCase`, you can use the appropriate processor: + +```cpp +const auto json_string = + rfl::json::write(homer); + +const auto homer2 = + rfl::json::read(json_string).value(); +``` + +The resulting JSON string looks like this: + +```json +{"FirstName":"Homer","LastName":"Simpson","Age":45} +``` + +## Combining several processors + +You can combine several processors: ```cpp const auto json_string = @@ -103,4 +155,4 @@ struct MyOwnProcessor { template static auto process(auto&& _named_tuple) {...} }; -``` \ No newline at end of file +``` diff --git a/include/rfl.hpp b/include/rfl.hpp index 4a53a0a7..4ba2f7cc 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -17,6 +17,7 @@ #include "rfl/Flatten.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" +#include "rfl/NoOptionals.hpp" #include "rfl/OneOf.hpp" #include "rfl/Pattern.hpp" #include "rfl/PatternValidator.hpp" diff --git a/include/rfl/NoOptionals.hpp b/include/rfl/NoOptionals.hpp new file mode 100644 index 00000000..9a7b3cd0 --- /dev/null +++ b/include/rfl/NoOptionals.hpp @@ -0,0 +1,18 @@ +#ifndef RFL_NOOPTIONALS_HPP_ +#define RFL_NOOPTIONALS_HPP_ + +namespace rfl { + +/// This is a "fake" processor - it doesn't do much in itself, but its +/// inclusion instructs the parsers to require the inclusion of all fields. +struct NoOptionals { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Processors.hpp b/include/rfl/Processors.hpp index 879e2220..91e08e5a 100644 --- a/include/rfl/Processors.hpp +++ b/include/rfl/Processors.hpp @@ -1,6 +1,10 @@ #ifndef RFL_INTERNAL_PROCESSORS_HPP_ #define RFL_INTERNAL_PROCESSORS_HPP_ +#include + +#include "internal/is_no_optionals_v.hpp" + namespace rfl { template @@ -8,6 +12,8 @@ struct Processors; template <> struct Processors<> { + static constexpr bool all_required_ = false; + template static auto process(NamedTupleType&& _named_tuple) { return _named_tuple; @@ -16,6 +22,10 @@ struct Processors<> { template struct Processors { + static constexpr bool all_required_ = + std::disjunction_v, + internal::is_no_optionals...>; + template static auto process(NamedTupleType&& _named_tuple) { return Processors::template process( diff --git a/include/rfl/internal/is_no_optionals_v.hpp b/include/rfl/internal/is_no_optionals_v.hpp new file mode 100644 index 00000000..1f5861b1 --- /dev/null +++ b/include/rfl/internal/is_no_optionals_v.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_INTERNAL_ISNOOPTIONALS_HPP_ +#define RFL_INTERNAL_ISNOOPTIONALS_HPP_ + +#include +#include +#include + +#include "../NoOptionals.hpp" + +namespace rfl { +namespace internal { + +template +class is_no_optionals; + +template +class is_no_optionals : public std::false_type {}; + +template <> +class is_no_optionals : public std::true_type {}; + +template +constexpr bool is_no_optionals_v = + is_no_optionals>>::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_named_tuple.hpp b/include/rfl/parsing/Parser_named_tuple.hpp index c8c7d7fc..0251fe0c 100644 --- a/include/rfl/parsing/Parser_named_tuple.hpp +++ b/include/rfl/parsing/Parser_named_tuple.hpp @@ -11,8 +11,8 @@ template requires AreReaderAndWriter> struct Parser, ProcessorsType> : public NamedTupleParser { + /*_all_required=*/ProcessorsType::all_required_, + ProcessorsType, FieldTypes...> { }; } // namespace parsing diff --git a/include/rfl/parsing/Parser_tuple.hpp b/include/rfl/parsing/Parser_tuple.hpp index 822d5907..fd7fdc79 100644 --- a/include/rfl/parsing/Parser_tuple.hpp +++ b/include/rfl/parsing/Parser_tuple.hpp @@ -11,7 +11,8 @@ template requires AreReaderAndWriter> struct Parser, ProcessorsType> : public TupleParser { + /*_all_required=*/ProcessorsType::all_required_, + ProcessorsType, Ts...> { }; } // namespace parsing diff --git a/include/rfl/xml/Parser.hpp b/include/rfl/xml/Parser.hpp index f74786c9..687f751e 100644 --- a/include/rfl/xml/Parser.hpp +++ b/include/rfl/xml/Parser.hpp @@ -21,8 +21,8 @@ struct Parser, ProcessorsType> : public NamedTupleParser { + /*_all_required=*/ProcessorsType::all_required_, + ProcessorsType, FieldTypes...> { }; } // namespace parsing diff --git a/tests/json/test_no_optionals.cpp b/tests/json/test_no_optionals.cpp new file mode 100644 index 00000000..60e1cd95 --- /dev/null +++ b/tests/json/test_no_optionals.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_no_optionals { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + rfl::Rename<"children", std::optional>> children; +}; + +TEST(json, test_no_optionals) { + const auto bart = Person{.first_name = "Bart"}; + + const auto lisa = Person{.first_name = "Lisa"}; + + const auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":null},{"firstName":"Lisa","lastName":"Simpson","children":null},{"firstName":"Maggie","lastName":"Simpson","children":null}]})"); +} +} // namespace test_no_optionals diff --git a/tests/json/test_optional_fields.cpp b/tests/json/test_optional_fields.cpp index 53895a52..677b400a 100644 --- a/tests/json/test_optional_fields.cpp +++ b/tests/json/test_optional_fields.cpp @@ -15,7 +15,7 @@ struct Person { rfl::Rename<"children", std::optional>> children; }; -TEST(json, insert_name_here) { +TEST(json, test_optional_fields) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"};