From 9a2c2cfef638889df581706b6b68c7d45a400078 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 22 Dec 2017 20:29:38 +0100 Subject: [PATCH] Deserialize enums as well --- README.md | 3 ++- lib/json/reflector.h | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba28ad2..6c8fb19 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ The following table shows the mapping of supported C++ types to supported JSON t * Raw pointer are not supported. This prevents forgetting to free memoery which would have to be allocated when deserializing. * For the same reason `const char *` strings are only supported for serialization. -* Enums are only supported for serialization. +* Enums are (de)serialized as their underlying integer value. When deserializing, it is currently *not* checked + whether the present integer value is a valid enumeration item. * For deserialization, iteratables must provide an `emplace_back` method. So deserialization of eg. `std::forward_list` is currently not supported. * The JSON type for smart pointer depends on the type the pointer refers to. It can also be `null`. diff --git a/lib/json/reflector.h b/lib/json/reflector.h index 09d33e4..aa3e22a 100644 --- a/lib/json/reflector.h +++ b/lib/json/reflector.h @@ -359,6 +359,24 @@ inline void pull( reflectable = value.Get(); } +/*! + * \brief Pulls the specified enumeration item from the specified value which is supposed and checked to be compatible with the underlying type. + * \remarks It is *not* checked, whether \a value is actually a valid enum item. + */ +template >...> +inline void pull( + Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue> &value, JsonDeserializationErrors *errors) +{ + using ExpectedType = Traits::Conditional::type>, uint64, int64>; + if (!value.Is()) { + if (errors) { + errors->reportTypeMismatch(value.GetType()); + } + return; + } + reflectable = static_cast(value.Get()); +} + /*! * \brief Pulls the std::string from the specified value which is supposed and checked to contain a string. */