WIP: Add toJsonAs()
This commit is contained in:
parent
d4662164fc
commit
2933a3a29a
|
@ -126,7 +126,7 @@ void OverallTests::testSingleInheritence()
|
||||||
"{\"someInt\":42,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\",\"someBool\":false}");
|
"{\"someInt\":42,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\",\"someBool\":false}");
|
||||||
|
|
||||||
// test serialization
|
// 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()));
|
CPPUNIT_ASSERT_EQUAL(expectedJSONForDerived, string(static_cast<const JSONSerializable<DerivedTestStruct> &>(test).toJson().GetString()));
|
||||||
|
|
||||||
// test deserialization
|
// test deserialization
|
||||||
|
|
|
@ -29,6 +29,9 @@ template <typename Type> struct JSONSerializable {
|
||||||
static Type fromJson(const char *json);
|
static Type fromJson(const char *json);
|
||||||
static Type fromJson(const std::string &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";
|
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());
|
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
|
} // namespace ReflectiveRapidJSON
|
||||||
|
|
||||||
#endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
|
#endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
|
||||||
|
|
Loading…
Reference in New Issue