diff --git a/lib/json/reflector-boosthana.h b/lib/json/reflector-boosthana.h index df0467e..8dd8892 100644 --- a/lib/json/reflector-boosthana.h +++ b/lib/json/reflector-boosthana.h @@ -23,7 +23,7 @@ namespace Reflector { // define functions to "push" values to a RapidJSON array or object template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) { @@ -35,7 +35,7 @@ void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Object &value, RA // define functions to "pull" values from a RapidJSON array or object template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void pull( Type &reflectable, RAPIDJSON_NAMESPACE::GenericValue>::ValueIterator &value, JsonDeserializationErrors *errors) @@ -46,7 +46,7 @@ void pull( } template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue>::ConstObject &value, JsonDeserializationErrors *errors) diff --git a/lib/json/reflector.h b/lib/json/reflector.h index 4cbbb0b..c0267e3 100644 --- a/lib/json/reflector.h +++ b/lib/json/reflector.h @@ -55,7 +55,7 @@ namespace Reflector { * \brief Pushes the \a reflectable which has a custom type to the specified array. */ template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator); @@ -64,7 +64,7 @@ void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAP * \remarks The definition of this function must be provided by the code generator or Boost.Hana. */ template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator); @@ -77,6 +77,15 @@ inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAP value.PushBack(reflectable, allocator); } +/*! + * \brief Pushes the specified enumeration item to the specified array. + */ +template >...> +inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) +{ + value.PushBack(static_cast::type>(reflectable), allocator); +} + /*! * \brief Pushes the specified C-string to the specified array. */ @@ -147,6 +156,16 @@ inline void push( value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), reflectable, allocator); } +/*! + * \brief Pushes the specified enumeration item as member to the specified object. + */ +template >...> +inline void push( + Type reflectable, const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) +{ + value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), static_cast::type>(reflectable), allocator); +} + /*! * \brief Pushes the specified C-string as member to the specified object. */ @@ -215,7 +234,7 @@ void push( * \brief Pushes the \a reflectable which has a custom type to the specified array. */ template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) { @@ -231,7 +250,7 @@ void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAP * \brief Pulls the \a reflectable which has a custom type from the specified value. */ template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void pull( Type &reflectable, RAPIDJSON_NAMESPACE::GenericValue>::ValueIterator &value, JsonDeserializationErrors *errors); @@ -241,7 +260,7 @@ void pull( * \remarks The definition of this function must be provided by the code generator or Boost.Hana. */ template , std::is_floating_point, std::is_pointer, + Traits::DisableIfAny, std::is_floating_point, std::is_pointer, std::is_enum, Traits::All, Traits::Not>>>...> void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue>::ConstObject &value, JsonDeserializationErrors *errors); diff --git a/lib/tests/jsonreflector.cpp b/lib/tests/jsonreflector.cpp index 47170fe..b8fffbd 100644 --- a/lib/tests/jsonreflector.cpp +++ b/lib/tests/jsonreflector.cpp @@ -48,6 +48,18 @@ struct NestingArray : public JsonSerializable { vector testObjects; }; +enum SomeEnum { + SomeEnumItem1, + SomeEnumItem2, + SomeEnumItem3, +}; + +enum class SomeEnumClass { + Item1, + Item2, + Item3, +}; + // pretend serialization code for structs has been generated namespace ReflectiveRapidJSON { namespace Reflector { @@ -188,6 +200,10 @@ void JsonReflectorTests::testSerializePrimitives() // number Reflector::push(25, array, alloc); Reflector::push(12.5, array, alloc); + // enum + Reflector::push(SomeEnumItem2, array, alloc); + Reflector::push(SomeEnumClass::Item2, array, alloc); + Reflector::push(SomeEnumClass::Item3, array, alloc); // array Reflector::push>({ "foo1", "bar1" }, array, alloc); Reflector::push>({ "foo2", "bar2" }, array, alloc); @@ -200,7 +216,7 @@ void JsonReflectorTests::testSerializePrimitives() Writer jsonWriter(strbuf); doc.Accept(jsonWriter); CPPUNIT_ASSERT_EQUAL( - "[\"foo\",\"bar\",25,12.5,[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"],[\"foo3\",\"bar3\"],true,false]"s, string(strbuf.GetString())); + "[\"foo\",\"bar\",25,12.5,1,1,2,[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"],[\"foo3\",\"bar3\"],true,false]"s, string(strbuf.GetString())); } /*!