diff --git a/README.md b/README.md index 1753cb1..29b926f 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ The following table shows the mapping of supported C++ types to supported JSON t | `float` and `double` | number | | `enum` and `enum class` | number | | `std::string` | string | -| `const char *` | string | +| `std::string_view` | string/null | +| `const char *` | string/null | | iteratable lists (`std::vector`, `std::list`, ...) | array | | sets (`std::set`, `std::unordered_set`, `std::multiset`, ...) | array | | `std::pair`, `std::tuple` | array | @@ -69,7 +70,7 @@ The following table shows the mapping of supported C++ types to supported JSON t ### Remarks * Raw pointer are not supported. This prevents forgetting to free memory which would have to be allocated when deserializing. -* For the same reason `const char *` strings are only supported for serialization. +* For the same reason `const char *` and `std::string_view` 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. * 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 8b21452..3de7f37 100644 --- a/lib/json/reflector.h +++ b/lib/json/reflector.h @@ -169,7 +169,24 @@ inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_ template > * = nullptr> inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) { - value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable), allocator); + if (reflectable) { + value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable), allocator); + } else { + value.SetNull(); + } +} + +/*! + * \brief Pushes the specified C-string to the specified value. + */ +template > * = nullptr> +inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator) +{ + if (reflectable.data()) { + value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable.data(), reflectable.size()), allocator); + } else { + value.SetNull(); + } } /*! @@ -570,7 +587,8 @@ inline void pull( * \brief Checks whether the specified value contains a string. * \remarks Does not actually store the value since the ownership would not be clear (see README.md). */ -template , std::is_same> * = nullptr> +template , std::is_same, std::is_same> * = nullptr> inline void pull(Type &, const RAPIDJSON_NAMESPACE::GenericValue> &value, JsonDeserializationErrors *errors) { if (!value.IsString()) { @@ -1008,6 +1026,17 @@ RAPIDJSON_NAMESPACE::Document toJsonDocument(const char *reflectable) return document; } +/*! + * \brief Serializes the specified \a reflectable which is a C-string. + */ +template > * = nullptr> +RAPIDJSON_NAMESPACE::Document toJsonDocument(std::string_view reflectable) +{ + RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kStringType); + document.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable.data(), reflectable.size()), document.GetAllocator()); + return document; +} + /*! * \brief Serializes the specified \a reflectable which can be mapped to an array. */ diff --git a/lib/traits.h b/lib/traits.h index aa921a0..c608d1b 100644 --- a/lib/traits.h +++ b/lib/traits.h @@ -54,14 +54,16 @@ template using IsMultiSet = Traits::Any, Traits::IsSpecializationOf, TreatAsMultiSet>; template -using IsArrayOrSet = Traits::Any, Traits::Not>, - Traits::Not>, Traits::Not>>, +using IsArrayOrSet = Traits::Any< + Traits::All, Traits::Not>, + Traits::Not>, Traits::Not>, Traits::Not>>, TreatAsSet, TreatAsMultiSet>; template using IsArray = Traits::All, Traits::Not>, Traits::Not>, Traits::Not>, Traits::Not>, Traits::Not>>; template -using IsIteratableExceptString = Traits::All, Traits::Not>>; +using IsIteratableExceptString = Traits::All, Traits::Not>, + Traits::Not>>; template using IsVariant = Traits::All>; } // namespace ReflectiveRapidJSON