From 2933a3a29a229d21b61d90cb73e2b67a935b2076 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 27 Oct 2017 15:36:09 +0200 Subject: [PATCH] WIP: Add toJsonAs() --- generator/tests/jsongenerator.cpp | 2 +- lib/jsonserializable.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/generator/tests/jsongenerator.cpp b/generator/tests/jsongenerator.cpp index 274a9f9..e043ed6 100644 --- a/generator/tests/jsongenerator.cpp +++ b/generator/tests/jsongenerator.cpp @@ -126,7 +126,7 @@ void OverallTests::testSingleInheritence() "{\"someInt\":42,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\",\"someBool\":false}"); // test serialization - CPPUNIT_ASSERT_EQUAL(expectedJSONForBase, string(static_cast &>(test).toJson().GetString())); + CPPUNIT_ASSERT_EQUAL(expectedJSONForBase, string(test.toJsonAs().GetString())); CPPUNIT_ASSERT_EQUAL(expectedJSONForDerived, string(static_cast &>(test).toJson().GetString())); // test deserialization diff --git a/lib/jsonserializable.h b/lib/jsonserializable.h index f20cf01..6e43c8b 100644 --- a/lib/jsonserializable.h +++ b/lib/jsonserializable.h @@ -29,6 +29,9 @@ template struct JSONSerializable { static Type fromJson(const char *json); static Type fromJson(const std::string &json); + template>...> + RAPIDJSON_NAMESPACE::StringBuffer toJsonAs() const; + static constexpr const char *qualifiedName = "ReflectiveRapidJSON::JSONSerializable"; }; @@ -81,6 +84,13 @@ template Type JSONSerializable::fromJson(const std::string return Reflector::fromJson(json.data(), json.size()); } +template +template>...> +RAPIDJSON_NAMESPACE::StringBuffer JSONSerializable::toJsonAs() const +{ + return static_cast(this)->toJson(); +} + } // namespace ReflectiveRapidJSON #endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H