Compare commits
1 Commits
master
...
wip/tojson
Author | SHA1 | Date |
---|---|---|
Martchus | 2933a3a29a |
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue