1 #ifndef REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H 2 #define REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H 10 #include <c++utilities/conversion/types.h> 11 #include <c++utilities/misc/traits.h> 13 #include <rapidjson/document.h> 14 #include <rapidjson/rapidjson.h> 15 #include <rapidjson/stringbuffer.h> 16 #include <rapidjson/writer.h> 24 #include <unordered_map> 25 #include <unordered_set> 37 static constexpr
const char *
name =
"AdaptedJsonSerializable";
38 static constexpr
const char *
qualifiedName =
"ReflectiveRapidJSON::AdaptedJsonSerializable";
44 namespace JsonReflector {
51 return size > std::numeric_limits<RAPIDJSON_NAMESPACE::SizeType>::max() ? std::numeric_limits<RAPIDJSON_NAMESPACE::SizeType>::max()
52 :
static_cast<RAPIDJSON_NAMESPACE::SizeType
>(size);
60 RAPIDJSON_NAMESPACE::StringBuffer buffer;
61 RAPIDJSON_NAMESPACE::Writer<RAPIDJSON_NAMESPACE::StringBuffer> writer(buffer);
62 document.Accept(writer);
71 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kObjectType);
72 const RAPIDJSON_NAMESPACE::ParseResult parseRes = document.Parse(json, jsonSize);
73 if (parseRes.IsError()) {
80 template <
typename Type>
81 using IsBuiltInType = Traits::Any<std::is_integral<Type>, std::is_floating_point<Type>, std::is_pointer<Type>, std::is_enum<Type>,
82 Traits::IsSpecializationOf<Type, std::tuple>, Traits::IsIteratable<Type>, Traits::IsSpecializationOf<Type, std::unique_ptr>,
83 Traits::IsSpecializationOf<Type, std::shared_ptr>, Traits::IsSpecializationOf<Type, std::weak_ptr>>;
84 template <
typename Type>
using IsCustomType = Traits::Not<IsBuiltInType<Type>>;
88 template <
typename Type>
93 template <
typename Type>
94 using IsMapOrHash = Traits::Any<Traits::IsSpecializationOf<Type, std::map>, Traits::IsSpecializationOf<Type, std::unordered_map>>;
95 template <
typename Type>
using IsSet = Traits::Any<Traits::IsSpecializationOf<Type, std::set>, Traits::IsSpecializationOf<Type, std::unordered_set>>;
96 template <
typename Type>
97 using IsMultiSet = Traits::Any<Traits::IsSpecializationOf<Type, std::multiset>, Traits::IsSpecializationOf<Type, std::unordered_multiset>>;
98 template <
typename Type>
100 = Traits::All<Traits::IsIteratable<Type>, Traits::Not<Traits::IsSpecializationOf<Type, std::basic_string>>, Traits::Not<IsMapOrHash<Type>>>;
101 template <
typename Type>
102 using IsArray = Traits::All<Traits::IsIteratable<Type>, Traits::Not<Traits::IsSpecializationOf<Type, std::basic_string>>,
103 Traits::Not<IsMapOrHash<Type>>, Traits::Not<IsSet<Type>>, Traits::Not<IsMultiSet<Type>>>;
110 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>* =
nullptr>
111 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
116 template <
typename Type, Traits::DisableIf<IsJsonSerializable<Type>>* =
nullptr>
117 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
122 template <
typename Type, Traits::EnableIf<IsJsonSerializable<Type>>* =
nullptr>
123 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
128 template <
typename Type, Traits::EnableIf<IsJsonSerializable<Type>>* =
nullptr>
130 const Type &reflectable,
const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
135 template <
typename Type, Traits::DisableIf<IsJsonSerializable<Type>>* =
nullptr>
137 const Type &reflectable,
const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
143 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>* =
nullptr>
144 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator);
149 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>*>
150 inline void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
153 RAPIDJSON_NAMESPACE::Value::Object obj(value.GetObject());
154 push(reflectable, obj, allocator);
160 template <
typename Type, Traits::EnableIfAny<std::is_
integral<Type>, std::is_
floating_po
int<Type>>* =
nullptr>
161 inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
163 value.Set(reflectable, allocator);
169 template <
typename Type, Traits::EnableIfAny<std::is_enum<Type>>* =
nullptr>
170 inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
172 value.Set(
static_cast<Traits::Conditional<std::is_unsigned<typename std::underlying_type<Type>::type
>, uint64, int64>>(reflectable), allocator);
178 template <
typename Type, Traits::EnableIf<std::is_same<Type, const
char *>>* =
nullptr>
179 inline void push(Type reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
181 value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable), allocator);
187 template <
typename Type, Traits::EnableIf<std::is_same<Type, const
char *const &>>* =
nullptr>
188 inline void push(
const char *
const &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
190 value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable), allocator);
196 template <
typename Type, Traits::EnableIf<std::is_same<Type, std::
string>>* =
nullptr>
197 inline void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
199 value.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable.data(), reflectable.size()), allocator);
205 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::HasSize<Type>>* =
nullptr>
206 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
209 RAPIDJSON_NAMESPACE::Value::Array array(value.GetArray());
210 array.Reserve(reflectable.size(), allocator);
211 for (
const auto &item : reflectable) {
212 push(item, array, allocator);
219 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::Not<Traits::HasSize<Type>>>* =
nullptr>
220 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
223 RAPIDJSON_NAMESPACE::Value::Array array(value.GetArray());
224 for (
const auto &item : reflectable) {
225 push(item, array, allocator);
232 template <
typename Type, Traits::EnableIf<IsMapOrHash<Type>>* =
nullptr>
233 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
236 RAPIDJSON_NAMESPACE::Value::Object object(value.GetObject());
237 for (
const auto &item : reflectable) {
238 push(item.second, item.first.data(), object, allocator);
248 static void push(
const Tuple &tuple, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
256 static void push(
const Tuple &tuple, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
266 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::tuple>>* =
nullptr>
267 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
270 RAPIDJSON_NAMESPACE::Value::Array array(value.GetArray());
271 array.Reserve(std::tuple_size<Type>::value, allocator);
272 Detail::TuplePushHelper<Type, std::tuple_size<Type>::value>
::push(reflectable, array, allocator);
278 template <
typename Type,
279 Traits::EnableIfAny<Traits::IsSpecializationOf<Type, std::unique_ptr>, Traits::IsSpecializationOf<Type, std::shared_ptr>,
280 Traits::IsSpecializationOf<Type, std::weak_ptr>>* =
nullptr>
281 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
287 push(*reflectable, value, allocator);
293 template <
typename Type, Traits::EnableIf<IsJsonSerializable<Type>>*>
294 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
296 RAPIDJSON_NAMESPACE::Value objectValue(RAPIDJSON_NAMESPACE::kObjectType);
297 RAPIDJSON_NAMESPACE::Value::Object object(objectValue.GetObject());
298 push(reflectable,
object, allocator);
299 value.PushBack(objectValue, allocator);
305 template <
typename Type, Traits::DisableIf<IsJsonSerializable<Type>>*>
306 void push(
const Type &reflectable, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
308 RAPIDJSON_NAMESPACE::Value genericValue;
309 push(reflectable, genericValue, allocator);
310 value.PushBack(genericValue, allocator);
316 template <
typename Type, Traits::EnableIf<IsJsonSerializable<Type>>*>
318 const Type &reflectable,
const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
320 RAPIDJSON_NAMESPACE::Value objectValue(RAPIDJSON_NAMESPACE::kObjectType);
321 RAPIDJSON_NAMESPACE::Value::Object object(objectValue.GetObject());
322 push(reflectable,
object, allocator);
323 value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), objectValue, allocator);
329 template <
typename Type, Traits::DisableIf<IsJsonSerializable<Type>>*>
331 const Type &reflectable,
const char *name, RAPIDJSON_NAMESPACE::Value::Object &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
333 RAPIDJSON_NAMESPACE::Value genericValue;
334 push(reflectable, genericValue, allocator);
335 value.AddMember(RAPIDJSON_NAMESPACE::StringRef(name), genericValue, allocator);
344 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>* =
nullptr>
345 void pull(Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value,
346 JsonDeserializationErrors *errors);
351 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>* =
nullptr>
352 void pull(Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
357 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::Not<Traits::IsReservable<Type>>>* =
nullptr>
358 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
363 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::IsReservable<Type>>* =
nullptr>
364 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
369 template <
typename Type, Traits::EnableIf<IsArray<Type>>* =
nullptr>
370 void pull(Type &reflectable, rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstArray array, JsonDeserializationErrors *errors);
375 template <
typename Type, Traits::EnableIf<IsSet<Type>>* =
nullptr>
376 void pull(Type &reflectable, rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstArray array, JsonDeserializationErrors *errors);
381 template <
typename Type, Traits::EnableIf<IsMultiSet<Type>>* =
nullptr>
382 void pull(Type &reflectable, rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstArray array, JsonDeserializationErrors *errors);
387 template <
typename Type, Traits::EnableIf<IsMapOrHash<Type>>* =
nullptr>
388 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
393 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::tuple>>* =
nullptr>
394 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
399 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::unique_ptr>>* =
nullptr>
400 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
405 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::shared_ptr>>* =
nullptr>
406 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
411 template <
typename Type>
413 Type &reflectable, rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ValueIterator &value, JsonDeserializationErrors *errors);
420 template <
typename Type>
421 inline void pull(Type &reflectable,
const char *name,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value,
422 JsonDeserializationErrors *errors);
427 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>*>
428 void pull(Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors);
433 template <
typename Type,
434 Traits::EnableIf<Traits::Not<std::is_same<Type, bool>>, Traits::Any<std::is_integral<Type>, std::is_floating_point<Type>>>* =
nullptr>
436 Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value,
JsonDeserializationErrors *errors)
438 if (!value.IsNumber()) {
444 reflectable = value.Is<Type>() ? value.Get<Type>() :
static_cast<Type
>(value.GetDouble());
450 template <
typename Type, Traits::EnableIf<std::is_same<Type,
bool>>* =
nullptr>
452 Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value,
JsonDeserializationErrors *errors)
454 if (!value.IsBool()) {
460 reflectable = value.GetBool();
467 template <
typename Type, Traits::EnableIfAny<std::is_enum<Type>>* =
nullptr>
469 Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
471 using ExpectedType = Traits::Conditional<std::is_unsigned<typename std::underlying_type<Type>::type>, uint64, int64>;
472 if (!value.Is<ExpectedType>()) {
474 errors->reportTypeMismatch<ExpectedType>(value.GetType());
478 reflectable =
static_cast<Type
>(value.Get<ExpectedType>());
484 template <
typename Type, Traits::EnableIf<std::is_same<Type, std::
string>>* =
nullptr>
486 Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
488 if (!value.IsString()) {
490 errors->reportTypeMismatch<std::string>(value.GetType());
494 reflectable = value.GetString();
501 template <
typename Type, Traits::EnableIfAny<std::is_same<Type, const
char *>, std::is_same<Type, const
char *const &>>* =
nullptr>
502 inline void pull(Type &,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
504 if (!value.IsString()) {
506 errors->reportTypeMismatch<std::string>(value.GetType());
515 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::Not<Traits::IsReservable<Type>>>*>
518 if (!value.IsArray()) {
524 pull(reflectable, value.GetArray(), errors);
530 template <
typename Type, Traits::EnableIf<IsArrayOrSet<Type>, Traits::IsReservable<Type>>*>
533 if (!value.IsArray()) {
539 auto array = value.GetArray();
540 reflectable.reserve(array.Size());
541 pull(reflectable, array, errors);
547 template <
typename Type, Traits::EnableIf<IsArray<Type>>*>
554 std::size_t index = 0;
555 for (
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &item : array) {
561 reflectable.emplace_back();
562 pull(reflectable.back(), item, errors);
574 template <
typename Type, Traits::EnableIf<IsMultiSet<Type>>*>
581 std::size_t index = 0;
582 for (
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &item : array) {
588 typename Type::value_type itemObj;
589 pull(itemObj, item, errors);
590 reflectable.emplace(move(itemObj));
602 template <
typename Type, Traits::EnableIf<IsSet<Type>>*>
603 void pull(Type &reflectable, rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstArray array, JsonDeserializationErrors *errors)
609 std::size_t index = 0;
610 for (
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &item : array) {
613 errors->currentIndex = index;
616 typename Type::value_type itemObj;
617 pull(itemObj, item, errors);
618 if (!reflectable.emplace(move(itemObj)).second) {
632 template <
typename Type, Traits::EnableIf<IsMapOrHash<Type>>*>
633 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
635 if (!value.IsObject()) {
637 errors->reportTypeMismatch<Type>(value.GetType());
641 auto obj = value.GetObject();
642 for (
auto i = obj.MemberBegin(), end = obj.MemberEnd(); i != end; ++i) {
643 pull(reflectable[i->name.GetString()], i->value, errors);
657 JsonReflector::pull<
typename std::tuple_element<N - 1, Tuple>::type>(std::get<N - 1>(tuple), value[N - 1], errors);
664 JsonReflector::pull<typename std::tuple_element<0, Tuple>::type>(std::get<0>(tuple), value[0], errors);
672 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::tuple>>*>
675 if (!value.IsArray()) {
681 auto array = value.GetArray();
682 if (array.Size() != std::tuple_size<Type>::value) {
689 Detail::TuplePullHelper<Type, std::tuple_size<Type>::value>
::pull(reflectable, array, errors);
695 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::unique_ptr>>*>
696 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
698 if (value.IsNull()) {
702 reflectable = std::make_unique<typename Type::element_type>();
703 pull(*reflectable, value, errors);
709 template <
typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::shared_ptr>>*>
710 void pull(Type &reflectable,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value, JsonDeserializationErrors *errors)
712 if (value.IsNull()) {
716 reflectable = std::make_shared<typename Type::element_type>();
717 pull(*reflectable, value, errors);
723 template <
typename Type>
726 pull<Type>(reflectable, *value, errors);
735 template <
typename Type>
736 inline void pull(Type &reflectable,
const char *name,
const rapidjson::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value,
740 const auto member = value.FindMember(name);
741 if (member == value.MemberEnd()) {
746 const char *previousMember;
753 pull<Type>(reflectable, member->value, errors);
764 template <
typename Type, Traits::DisableIf<IsBuiltInType<Type>>*>
765 void pull(Type &reflectable,
const RAPIDJSON_NAMESPACE::GenericValue<RAPIDJSON_NAMESPACE::UTF8<char>> &value,
JsonDeserializationErrors *errors)
767 if (!value.IsObject()) {
773 pull(reflectable, value.GetObject(), errors);
781 template <
typename Type, Traits::EnableIfAny<IsJsonSerializable<Type>, IsMapOrHash<Type>>* =
nullptr>
782 RAPIDJSON_NAMESPACE::StringBuffer
toJson(
const Type &reflectable)
784 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kObjectType);
785 RAPIDJSON_NAMESPACE::Document::Object object(document.GetObject());
786 push(reflectable,
object, document.GetAllocator());
793 template <
typename Type, Traits::EnableIfAny<std::is_
integral<Type>, std::is_
floating_po
int<Type>>* =
nullptr>
794 RAPIDJSON_NAMESPACE::StringBuffer
toJson(Type reflectable)
796 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kNumberType);
797 document.Set(reflectable, document.GetAllocator());
804 template <
typename Type, Traits::EnableIf<std::is_same<Type, std::
string>>* =
nullptr>
805 RAPIDJSON_NAMESPACE::StringBuffer
toJson(
const std::string &reflectable)
807 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kStringType);
808 document.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable.data(), reflectable.size()), document.GetAllocator());
815 template <
typename Type, Traits::EnableIf<std::is_same<Type, const
char *>>* =
nullptr> RAPIDJSON_NAMESPACE::StringBuffer
toJson(
const char *reflectable)
817 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kStringType);
818 document.SetString(RAPIDJSON_NAMESPACE::StringRef(reflectable), document.GetAllocator());
825 template <
typename Type, Traits::EnableIf<IsArray<Type>>* =
nullptr> RAPIDJSON_NAMESPACE::StringBuffer
toJson(
const Type &reflectable)
827 RAPIDJSON_NAMESPACE::Document document(RAPIDJSON_NAMESPACE::kArrayType);
828 push(reflectable, document, document.GetAllocator());
837 template <
typename Type, Traits::EnableIfAny<IsJsonSerializable<Type>, IsMapOrHash<Type>>* =
nullptr>
841 if (!doc.IsObject()) {
849 pull<Type>(res, doc.GetObject(), errors);
856 template <
typename Type, Traits::EnableIfAny<std::is_
integral<Type>, std::is_
floating_po
int<Type>>* =
nullptr>
860 if (!doc.Is<Type>()) {
867 return doc.Get<Type>();
873 template <
typename Type, Traits::EnableIf<std::is_same<Type, std::
string>>* =
nullptr>
874 Type
fromJson(
const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors =
nullptr)
877 if (!doc.IsString()) {
884 return doc.GetString();
890 template <
typename Type, Traits::EnableIf<IsArray<Type>>* =
nullptr>
891 Type
fromJson(
const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors =
nullptr)
894 if (!doc.IsArray()) {
902 pull<Type>(res, doc.GetArray(), errors);
911 return fromJson<Type>(json, std::strlen(json), errors);
919 return fromJson<Type>(json.data(), json.size(), errors);
925 #endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H Contains helper for error handling when deserializing JSON files.
RAPIDJSON_NAMESPACE::StringBuffer serializeJsonDocToString(RAPIDJSON_NAMESPACE::Document &document)
Serializes the specified JSON document.
static void pull(Tuple &tuple, const RAPIDJSON_NAMESPACE::Value::ConstArray value, JsonDeserializationErrors *errors)
static void push(const Tuple &tuple, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
RAPIDJSON_NAMESPACE::Document parseJsonDocFromString(const char *json, std::size_t jsonSize)
Parses the specified JSON string.
The AdaptedJsonSerializable class allows considering 3rd party classes as serializable.
Traits::Any< Traits::Not< Traits::IsComplete< Type > >, std::is_base_of< JsonSerializable< Type >, Type >, AdaptedJsonSerializable< Type > > IsJsonSerializable
const char * currentMember
The name of the member (in currentRecord) which is currently being processed.
constexpr RAPIDJSON_NAMESPACE::SizeType rapidJsonSize(std::size_t size)
Casts the specified size to the size type used by RapidJSON ensuring no overflow happens.
Traits::Any< Traits::IsSpecializationOf< Type, std::set >, Traits::IsSpecializationOf< Type, std::unordered_set > > IsSet
static constexpr const char * qualifiedName
static void push(const Tuple &tuple, RAPIDJSON_NAMESPACE::Value::Array &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
Traits::Any< Traits::IsSpecializationOf< Type, std::multiset >, Traits::IsSpecializationOf< Type, std::unordered_multiset > > IsMultiSet
Traits::Any< Traits::IsSpecializationOf< Type, std::map >, Traits::IsSpecializationOf< Type, std::unordered_map > > IsMapOrHash
RAPIDJSON_NAMESPACE::StringBuffer toJson(const Type &reflectable)
Serializes the specified reflectable which has a custom type or can be mapped to and object...
static constexpr const char * name
Traits::All< Traits::IsIteratable< Type >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > >, Traits::Not< IsMapOrHash< Type > >, Traits::Not< IsSet< Type > >, Traits::Not< IsMultiSet< Type > >> IsArray
std::size_t currentIndex
The index in the array which is currently processed.
Traits::Not< IsBuiltInType< Type > > IsCustomType
static void pull(Tuple &tuple, const RAPIDJSON_NAMESPACE::Value::ConstArray value, JsonDeserializationErrors *errors)
void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
Reports a type mismatch between.
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
The TuplePullHelper class helps deserializing tuples from JSON arrays.
The TuplePushHelper class helps serializing tuples to JSON arrays.
static constexpr std::size_t noIndex
Indicates no array was being processed when the error occured.
Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Deserializes the specified JSON to.
Traits::All< Traits::IsIteratable< Type >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > >, Traits::Not< IsMapOrHash< Type > >> IsArrayOrSet
void reportArraySizeMismatch()
Reports an array size mismatch.
void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue< RAPIDJSON_NAMESPACE::UTF8< char >>::ConstObject &value, JsonDeserializationErrors *errors)
Pulls the reflectable which has a custom type from the specified object.
Traits::Any< std::is_integral< Type >, std::is_floating_point< Type >, std::is_pointer< Type >, std::is_enum< Type >, Traits::IsSpecializationOf< Type, std::tuple >, Traits::IsIteratable< Type >, Traits::IsSpecializationOf< Type, std::unique_ptr >, Traits::IsSpecializationOf< Type, std::shared_ptr >, Traits::IsSpecializationOf< Type, std::weak_ptr > > IsBuiltInType
void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
Pushes the specified reflectable to the specified value.
The JsonSerializable class provides the CRTP-base for (de)serializable objects.