diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 5a8e4de..0e2765c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,17 +5,16 @@ set(META_PROJECT_TYPE library) # add project files set(HEADER_FILES - reflect.h - reflectable.h + jsonreflector.h + jsonserializable.h ) set(SRC_FILES - reflect.cpp ) set(TEST_HEADER_FILES ) set(TEST_SRC_FILES tests/cppunit.cpp - tests/reflector.cpp + tests/jsonreflector.cpp ) set(DOC_FILES README.md diff --git a/lib/reflect.h b/lib/jsonreflector.h similarity index 86% rename from lib/reflect.h rename to lib/jsonreflector.h index 7034d40..37888ca 100644 --- a/lib/reflect.h +++ b/lib/jsonreflector.h @@ -1,5 +1,5 @@ -#ifndef REFLECTIVE_RAPIDJSON_REFLECT_H -#define REFLECTIVE_RAPIDJSON_REFLECT_H +#ifndef REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H +#define REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H #include #include @@ -27,7 +27,7 @@ constexpr ErrorFlags operator|(ErrorFlags lhs, ErrorFlags rhs) return static_cast(static_cast(lhs) | static_cast(rhs)); } -template struct Reflectable; +template struct JSONSerializable; inline RAPIDJSON_NAMESPACE::StringBuffer documentToString(RAPIDJSON_NAMESPACE::Document &document) { @@ -226,7 +226,7 @@ inline void pull(Type &reflectable, const char *name, const rapidjson::GenericVa // define functions providing high-level JSON serialization -template , Type>>...> +template , Type>>...> RAPIDJSON_NAMESPACE::StringBuffer toJson(const Type &reflectable) { RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kObjectType); @@ -260,7 +260,7 @@ template >.. // define functions providing high-level JSON deserialization -template , Type>>...> Type fromJson(const char *json, std::size_t jsonSize) +template , Type>>...> Type fromJson(const char *json, std::size_t jsonSize) { RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kObjectType); document.Parse(json, jsonSize); @@ -290,19 +290,6 @@ template Type fromJson(const std::string &json) } } // namespace Reflector - } // namespace ReflectiveRapidJSON -#define REFLECT(Type) \ - namespace Reflector { \ - template <> \ - void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator); \ - template <> \ - void add( \ - const Type &reflectable, const char *name, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator); \ - template <> std::string toJson(const Type &reflectable); \ - template <> Type fromJson(const char *json, std::size_t jsonSize); \ - template <> Type fromJson(const std::string &json); \ - } - -#endif // REFLECTIVE_RAPIDJSON_REFLECT_H +#endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H diff --git a/lib/jsonserializable.h b/lib/jsonserializable.h new file mode 100644 index 0000000..e692228 --- /dev/null +++ b/lib/jsonserializable.h @@ -0,0 +1,58 @@ +#ifndef REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H +#define REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H + +#include "./jsonreflector.h" + +#include + +#include + +namespace ReflectiveRapidJSON { + +template struct JSONSerializable { + // RapidJSON-level API + void push(RAPIDJSON_NAMESPACE::Value &container); + void push(RAPIDJSON_NAMESPACE::Value &container, const char *name); + + // high-level API + RAPIDJSON_NAMESPACE::StringBuffer toJson() const; + static Type fromJson(const char *json, std::size_t jsonSize); + static Type fromJson(const char *json); + static Type fromJson(const std::string &json); + + static constexpr const char *qualifiedName = "ReflectiveRapidJSON::JSONSerializable"; +}; + +template void JSONSerializable::push(RAPIDJSON_NAMESPACE::Value &container) +{ + return Reflector::push(*this, container); +} + +template void JSONSerializable::push(RAPIDJSON_NAMESPACE::Value &container, const char *name) +{ + return Reflector::push(*this, name, container); +} + +template RAPIDJSON_NAMESPACE::StringBuffer JSONSerializable::toJson() const +{ + return Reflector::toJson(static_cast(*this)); +} + +template Type JSONSerializable::fromJson(const char *json, std::size_t jsonSize) +{ + return Reflector::fromJson(json, jsonSize); +} + +template Type JSONSerializable::fromJson(const char *json) +{ + return Reflector::fromJson(json, std::strlen(json)); +} + +template Type JSONSerializable::fromJson(const std::string &json) +{ + return Reflector::fromJson(json.data(), json.size()); +} + +} // namespace ReflectiveRapidJSON + +#endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H diff --git a/lib/reflect.cpp b/lib/reflect.cpp deleted file mode 100644 index 3cf03e2..0000000 --- a/lib/reflect.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "./reflect.h" - -using namespace RAPIDJSON_NAMESPACE; - -namespace ReflectiveRapidJSON { - -namespace Reflector { -} - -} // namespace ReflectiveRapidJSON diff --git a/lib/reflectable.h b/lib/reflectable.h index 1ab1561..f5527fc 100644 --- a/lib/reflectable.h +++ b/lib/reflectable.h @@ -9,7 +9,7 @@ namespace ReflectiveRapidJSON { -template struct Reflectable { +template struct JSONSerializable { // RapidJSON-level API void push(RAPIDJSON_NAMESPACE::Value &container); void push(RAPIDJSON_NAMESPACE::Value &container, const char *name); @@ -21,32 +21,32 @@ template struct Reflectable { static Type fromJson(const std::string &json); }; -template void Reflectable::push(RAPIDJSON_NAMESPACE::Value &container) +template void JSONSerializable::push(RAPIDJSON_NAMESPACE::Value &container) { return Reflector::push(*this, container); } -template void Reflectable::push(RAPIDJSON_NAMESPACE::Value &container, const char *name) +template void JSONSerializable::push(RAPIDJSON_NAMESPACE::Value &container, const char *name) { return Reflector::push(*this, name, container); } -template RAPIDJSON_NAMESPACE::StringBuffer Reflectable::toJson() const +template RAPIDJSON_NAMESPACE::StringBuffer JSONSerializable::toJson() const { return Reflector::toJson(static_cast(*this)); } -template Type Reflectable::fromJson(const char *json, std::size_t jsonSize) +template Type JSONSerializable::fromJson(const char *json, std::size_t jsonSize) { return Reflector::fromJson(json, jsonSize); } -template Type Reflectable::fromJson(const char *json) +template Type JSONSerializable::fromJson(const char *json) { return Reflector::fromJson(json, std::strlen(json)); } -template Type Reflectable::fromJson(const std::string &json) +template Type JSONSerializable::fromJson(const std::string &json) { return Reflector::fromJson(json.data(), json.size()); } diff --git a/lib/tests/reflector.cpp b/lib/tests/jsonreflector.cpp similarity index 92% rename from lib/tests/reflector.cpp rename to lib/tests/jsonreflector.cpp index 33a1cd4..6915bfa 100644 --- a/lib/tests/reflector.cpp +++ b/lib/tests/jsonreflector.cpp @@ -1,6 +1,5 @@ -#include "../reflectable.h" - -#include "resources/config.h" +#include "../jsonreflector.h" +#include "../jsonserializable.h" #include #include @@ -31,7 +30,7 @@ using namespace ReflectiveRapidJSON; /// \cond // define some structs for testing serialization -struct TestObject : public Reflectable { +struct TestObject : public JSONSerializable { int number; double number2; vector numbers; @@ -39,12 +38,12 @@ struct TestObject : public Reflectable { bool boolean; }; -struct NestingObject : public Reflectable { +struct NestingObject : public JSONSerializable { string name; TestObject testObj; }; -struct NestingArray : public Reflectable { +struct NestingArray : public JSONSerializable { string name; vector testObjects; }; @@ -104,8 +103,8 @@ template <> inline void pull(NestingArray &reflectable, const Gene * \brief The ReflectorTests class tests RapidJSON wrapper which is used to ease code generation. * \remarks In this tests, no reflection or code generation is involved yet. */ -class ReflectorTests : public TestFixture { - CPPUNIT_TEST_SUITE(ReflectorTests); +class JSONReflectorTests : public TestFixture { + CPPUNIT_TEST_SUITE(JSONReflectorTests); CPPUNIT_TEST(experiment); CPPUNIT_TEST(testSerializePrimitives); CPPUNIT_TEST(testSerializeSimpleObjects); @@ -130,20 +129,20 @@ public: private: }; -CPPUNIT_TEST_SUITE_REGISTRATION(ReflectorTests); +CPPUNIT_TEST_SUITE_REGISTRATION(JSONReflectorTests); -void ReflectorTests::setUp() +void JSONReflectorTests::setUp() { } -void ReflectorTests::tearDown() +void JSONReflectorTests::tearDown() { } /*! * \brief Not a real test, just some assertions for experimenting with the RapidJSON library. */ -void ReflectorTests::experiment() +void JSONReflectorTests::experiment() { Document doc(kArrayType); Document::AllocatorType &alloc = doc.GetAllocator(); @@ -170,7 +169,7 @@ void ReflectorTests::experiment() /*! * \brief Tests serializing strings, numbers, arrays and boolean. */ -void ReflectorTests::testSerializePrimitives() +void JSONReflectorTests::testSerializePrimitives() { Document doc(kArrayType); Document::AllocatorType &alloc = doc.GetAllocator(); @@ -201,7 +200,7 @@ void ReflectorTests::testSerializePrimitives() /*! * \brief Tests serializing objects. */ -void ReflectorTests::testSerializeSimpleObjects() +void JSONReflectorTests::testSerializeSimpleObjects() { TestObject testObj; testObj.number = 42; @@ -216,7 +215,7 @@ void ReflectorTests::testSerializeSimpleObjects() /*! * \brief Tests serializing nested object and arrays. */ -void ReflectorTests::testSerializeNestedObjects() +void JSONReflectorTests::testSerializeNestedObjects() { NestingObject nestingObj; nestingObj.name = "nesting"; @@ -242,7 +241,7 @@ void ReflectorTests::testSerializeNestedObjects() /*! * \brief Tests deserializing strings, numbers (int, float, double) and boolean. */ -void ReflectorTests::testDeserializePrimitives() +void JSONReflectorTests::testDeserializePrimitives() { Document doc(kArrayType); @@ -274,7 +273,7 @@ void ReflectorTests::testDeserializePrimitives() /*! * \brief Tests deserializing simple objects. */ -void ReflectorTests::testDeserializeSimpleObjects() +void JSONReflectorTests::testDeserializeSimpleObjects() { const TestObject testObj( TestObject::fromJson("{\"number\":42,\"number2\":3.141592653589793,\"numbers\":[1,2,3,4],\"text\":\"test\",\"boolean\":false}")); @@ -289,7 +288,7 @@ void ReflectorTests::testDeserializeSimpleObjects() /*! * \brief Tests deserializing nested objects and arrays. */ -void ReflectorTests::testDeserializeNestedObjects() +void JSONReflectorTests::testDeserializeNestedObjects() { const NestingObject nestingObj(NestingObject::fromJson("{\"name\":\"nesting\",\"testObj\":{\"number\":42,\"number2\":3.141592653589793," "\"numbers\":[1,2,3,4],\"text\":\"test\",\"boolean\":false}}")); diff --git a/moc/CMakeLists.txt b/moc/CMakeLists.txt index 0b7697c..84b6556 100644 --- a/moc/CMakeLists.txt +++ b/moc/CMakeLists.txt @@ -45,5 +45,6 @@ include(WindowsResources) include(AppTarget) include(TestTarget) include(ShellCompletion) +include(Doxygen) include(ConfigHeader) diff --git a/moc/generator.cpp b/moc/generator.cpp index 23e70fa..846d597 100644 --- a/moc/generator.cpp +++ b/moc/generator.cpp @@ -1,6 +1,8 @@ #include "./generator.h" #include "./frontendaction.h" +#include "../lib/jsonserializable.h" + #include #include @@ -62,8 +64,8 @@ void JSONSerializationCodeGenerator::addDeclaration(clang::Decl *decl) if (!record->hasDefinition()) { return; } - // add classes derived from any instantiation of "ReflectiveRapidJSON::Reflectable" - if (inheritsFromInstantiationOf(record, "ReflectiveRapidJSON::Reflectable")) { + // add classes derived from any instantiation of "ReflectiveRapidJSON::JSONSerializable" + if (inheritsFromInstantiationOf(record, JSONSerializable::qualifiedName)) { m_relevantClasses.emplace_back(record->getQualifiedNameAsString(), record); } break; @@ -86,7 +88,7 @@ void JSONSerializationCodeGenerator::generate(ostream &os) const "namespace Reflector {\n"; // add push and pull functions for each class, for an example of the resulting - // output, see ../lib/tests/reflector.cpp (code under comment "pretend serialization code...") + // output, see ../lib/tests/jsonserializable.cpp (code under comment "pretend serialization code...") for (const RelevantClass &relevantClass : m_relevantClasses) { // print push method os << "template <> inline void push<::" << relevantClass.qualifiedName << ">(const " << relevantClass.qualifiedName diff --git a/moc/generator.h b/moc/generator.h index 32eaa3d..68ee589 100644 --- a/moc/generator.h +++ b/moc/generator.h @@ -36,7 +36,8 @@ inline CodeGenerator::CodeGenerator() } /*! - * \brief The JSONSerializationCodeGenerator class generates code for JSON (de)serialization. + * \brief The JSONSerializationCodeGenerator class generates code for JSON (de)serialization + * of objects inheriting from an instantiation of JSONSerializable. */ class JSONSerializationCodeGenerator : public CodeGenerator { public: diff --git a/moc/testfiles/some_structs.h b/moc/testfiles/some_structs.h index f063f5c..1a6bebb 100644 --- a/moc/testfiles/some_structs.h +++ b/moc/testfiles/some_structs.h @@ -2,13 +2,13 @@ #define SOME_STRUCTS_H //#include -#include "../../lib/reflectable.h" +#include "../../lib/jsonserializable.h" namespace TestNamespace1 { #define SOME_MACRO -struct Person : public ReflectiveRapidJSON::Reflectable +struct Person : public ReflectiveRapidJSON::JSONSerializable { SOME_MACRO //std::string name;