Compare commits

...

1 Commits

Author SHA1 Message Date
Martchus 2933a3a29a WIP: Add toJsonAs() 2017-10-27 15:36:09 +02:00
2 changed files with 11 additions and 1 deletions

View File

@ -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<const JSONSerializable<TestStruct> &>(test).toJson().GetString()));
CPPUNIT_ASSERT_EQUAL(expectedJSONForBase, string(test.toJsonAs<TestStruct>().GetString()));
CPPUNIT_ASSERT_EQUAL(expectedJSONForDerived, string(static_cast<const JSONSerializable<DerivedTestStruct> &>(test).toJson().GetString()));
// test deserialization

View File

@ -29,6 +29,9 @@ template <typename Type> struct JSONSerializable {
static Type fromJson(const char *json);
static Type fromJson(const std::string &json);
template<typename ViewType, Traits::EnableIf<std::is_same<ViewType, Type>>...>
RAPIDJSON_NAMESPACE::StringBuffer toJsonAs() const;
static constexpr const char *qualifiedName = "ReflectiveRapidJSON::JSONSerializable";
};
@ -81,6 +84,13 @@ template <typename Type> Type JSONSerializable<Type>::fromJson(const std::string
return Reflector::fromJson<Type>(json.data(), json.size());
}
template<typename Type>
template<typename ViewType, Traits::EnableIf<std::is_same<ViewType, Type>>...>
RAPIDJSON_NAMESPACE::StringBuffer JSONSerializable<Type>::toJsonAs() const
{
return static_cast<const ViewType *>(this)->toJson();
}
} // namespace ReflectiveRapidJSON
#endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H