From 4d967ccde26ccce4184a510b1f28d20371d4aea7 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Wed, 25 Oct 2023 20:23:16 +0200 Subject: [PATCH] Added support custom constructors with results --- include/rfl/json/Reader.hpp | 6 ++++ include/rfl/json/read.hpp | 11 +++++-- tests/test_custom_constructor.hpp | 52 +++++++++++++++++++++++++++++++ tests/tests.cpp | 3 ++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/test_custom_constructor.hpp diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index e5ddf40e..bb091643 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -51,6 +51,12 @@ struct Reader { typename Reader::InputVarType>::value>> : std::true_type {}; + template + struct has_from_json_obj, decltype(T::from_json_obj), + typename Reader::InputVarType>::value>> + : std::true_type {}; + template static constexpr bool has_custom_constructor = has_from_json_obj::value; diff --git a/include/rfl/json/read.hpp b/include/rfl/json/read.hpp index 82a041e2..10f67666 100644 --- a/include/rfl/json/read.hpp +++ b/include/rfl/json/read.hpp @@ -14,15 +14,20 @@ namespace json { using InputObjectType = typename Reader::InputObjectType; using InputVarType = typename Reader::InputVarType; +/// Parses an object from a JSON var. +template +Result read(const InputVarType& _obj) { + const auto r = Reader(); + return Parser::read(r, _obj); +} + /// Parses an object from JSON using reflection. template Result read(const std::string& _json_str) { using PtrType = std::unique_ptr; yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0); const auto ptr = PtrType(doc, yyjson_doc_free); - const auto root = InputVarType(yyjson_doc_get_root(doc)); - const auto r = Reader(); - return Parser::read(r, root); + return read(InputVarType(yyjson_doc_get_root(doc))); } } // namespace json diff --git a/tests/test_custom_constructor.hpp b/tests/test_custom_constructor.hpp new file mode 100644 index 00000000..4ca9c657 --- /dev/null +++ b/tests/test_custom_constructor.hpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that the example in the README works as expected. +// It would be embarrassing if it didn't... +void test_custom_constructor() { + std::cout << "test_custom_constructor" << std::endl; + + struct Person { + static rfl::Result from_json_obj( + typename rfl::json::Reader::InputVarType _obj) { + /// This only exists for the purpose of the test. + const auto change_first_name = [](auto&& _person) { + return rfl::replace( + std::move(_person), + rfl::Field<"firstName", std::string>("Bart")); + }; + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::json::read>(_obj) + .transform(from_nt) + .transform(change_first_name); + } + + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + }; + + const auto res = rfl::json::read( + R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})"); + + if (!res) { + std::cout << "Test failed on read. Error: " + << res.error().value().what() << std::endl; + return; + } + + if (res.value().first_name() != "Bart") { + std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" + << std::endl + << std::endl; + } else { + std::cout << "OK" << std::endl << std::endl; + } +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 871285c4..ebb3de95 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -6,6 +6,7 @@ #include "test_custom_class2.hpp" #include "test_custom_class3.hpp" #include "test_custom_class4.hpp" +#include "test_custom_constructor.hpp" #include "test_default_values.hpp" #include "test_deque.hpp" #include "test_error_messages.hpp" @@ -77,5 +78,7 @@ int main() { test_as(); test_as_flatten(); + test_custom_constructor(); + return 0; }