Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
errorhandling.h
Go to the documentation of this file.
1 #ifndef REFLECTIVE_RAPIDJSON_JSON_ERROR_HANDLING_H
2 #define REFLECTIVE_RAPIDJSON_JSON_ERROR_HANDLING_H
3 
9 #include <c++utilities/misc/traits.h>
10 
11 #include <rapidjson/rapidjson.h>
12 
13 #include <cstdint>
14 #include <limits>
15 #include <list>
16 #include <string>
17 #include <vector>
18 
19 namespace ReflectiveRapidJSON {
20 
24 enum class JsonDeserializationErrorKind : std::uint8_t {
25  TypeMismatch,
31 };
32 
37 enum class JsonType : std::uint8_t {
38  Null,
39  Number,
40  Bool,
41  String,
42  Array,
43  Object,
44 };
45 
46 // define helper functions which return the JsonType for the C++ type specified as template parameter
47 
48 template <typename Type,
49  Traits::EnableIf<Traits::Not<std::is_same<Type, bool>>, Traits::Any<std::is_integral<Type>, std::is_floating_point<Type>>> * = nullptr>
50 constexpr JsonType jsonType()
51 {
52  return JsonType::Number;
53 }
54 
55 template <typename Type, Traits::EnableIfAny<std::is_same<Type, bool>> * = nullptr> constexpr JsonType jsonType()
56 {
57  return JsonType::Bool;
58 }
59 
60 template <typename Type, Traits::EnableIfAny<Traits::IsString<Type>, Traits::IsCString<Type>> * = nullptr> constexpr JsonType jsonType()
61 {
62  return JsonType::String;
63 }
64 
65 template <typename Type,
66  Traits::EnableIf<Traits::IsIteratable<Type>,
67  Traits::Not<Traits::Any<Traits::IsString<Type>, Traits::IsSpecializationOf<Type, std::map>,
68  Traits::IsSpecializationOf<Type, std::unordered_map>>>> * = nullptr>
69 constexpr JsonType jsonType()
70 {
71  return JsonType::Array;
72 }
73 
74 template <typename Type,
75  Traits::DisableIfAny<std::is_integral<Type>, std::is_floating_point<Type>, Traits::IsString<Type>, Traits::IsCString<Type>,
76  Traits::All<Traits::IsIteratable<Type>,
77  Traits::Not<Traits::Any<Traits::IsString<Type>, Traits::IsSpecializationOf<Type, std::map>,
78  Traits::IsSpecializationOf<Type, std::unordered_map>>>>> * = nullptr>
79 constexpr JsonType jsonType()
80 {
81  return JsonType::Object;
82 }
83 
87 constexpr JsonType jsonType(RAPIDJSON_NAMESPACE::Type type)
88 {
89  switch (type) {
90  case RAPIDJSON_NAMESPACE::kFalseType:
91  case RAPIDJSON_NAMESPACE::kTrueType:
92  return JsonType::Bool;
93  case RAPIDJSON_NAMESPACE::kObjectType:
94  return JsonType::Object;
95  case RAPIDJSON_NAMESPACE::kArrayType:
96  return JsonType::Array;
97  case RAPIDJSON_NAMESPACE::kStringType:
98  return JsonType::String;
99  case RAPIDJSON_NAMESPACE::kNumberType:
100  return JsonType::Number;
101  default:
102  return JsonType::Null;
103  }
104 }
105 
111  const char *member = nullptr, std::size_t index = noIndex);
112 
120  const char *record;
122  const char *member;
124  std::size_t index;
125 
127  static constexpr std::size_t noIndex = std::numeric_limits<std::size_t>::max();
128 };
129 
135  JsonDeserializationErrorKind kind, JsonType expectedType, JsonType actualType, const char *record, const char *member, std::size_t index)
136  : kind(kind)
137  , expectedType(expectedType)
138  , actualType(actualType)
139  , record(record)
140  , member(member)
141  , index(index)
142 {
143 }
144 
154 struct JsonDeserializationErrors : public std::vector<JsonDeserializationError> {
156 
157  template <typename ExpectedType> void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType);
161 
163  const char *currentRecord;
165  const char *currentMember;
167  std::size_t currentIndex;
169  enum class ThrowOn : std::uint8_t {
170  None = 0,
171  TypeMismatch = 0x1,
172  ArraySizeMismatch = 0x2,
173  ConversionError = 0x4,
174  UnexpectedDuplicate = 0x8,
175  All = 0xFF,
177 
178 private:
179  void throwMaybe(ThrowOn on) const;
180 };
181 
186  : currentRecord("[document]")
187  , currentMember(nullptr)
188  , currentIndex(JsonDeserializationError::noIndex)
189  , throwOn(ThrowOn::None)
190 {
191 }
192 
197 {
198  return static_cast<JsonDeserializationErrors::ThrowOn>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
199 }
200 
206 inline void JsonDeserializationErrors::throwMaybe(ThrowOn on) const
207 {
208  if (static_cast<std::uint8_t>(throwOn) & static_cast<std::uint8_t>(on)) {
209  throw back();
210  }
211 }
212 
216 template <typename ExpectedType> inline void JsonDeserializationErrors::reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
217 {
218  emplace_back(
220  throwMaybe(ThrowOn::TypeMismatch);
221 }
222 
229 {
231  throwMaybe(ThrowOn::ArraySizeMismatch);
232 }
233 
240 {
242  throwMaybe(ThrowOn::ConversionError);
243 }
244 
251 {
253  throwMaybe(ThrowOn::UnexpectedDuplicate);
254 }
255 
256 } // namespace ReflectiveRapidJSON
257 
258 #endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition: errorhandling.h:154
ReflectiveRapidJSON::JsonDeserializationErrors::JsonDeserializationErrors
JsonDeserializationErrors()
Creates an empty JsonDeserializationErrors object with default context and no errors considered fatal...
Definition: errorhandling.h:185
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::ArraySizeMismatch
@ ArraySizeMismatch
ReflectiveRapidJSON::JsonDeserializationErrorKind::InvalidVariantIndex
@ InvalidVariantIndex
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::ConversionError
@ ConversionError
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::None
@ None
ReflectiveRapidJSON::jsonType
constexpr JsonType jsonType()
Definition: errorhandling.h:50
ReflectiveRapidJSON::JsonDeserializationErrors::currentRecord
const char * currentRecord
The name of the class or struct which is currently being processed.
Definition: errorhandling.h:163
ReflectiveRapidJSON::JsonDeserializationError::kind
JsonDeserializationErrorKind kind
Which kind of error occured.
Definition: errorhandling.h:114
ReflectiveRapidJSON::JsonDeserializationErrorKind::ConversionError
@ ConversionError
ReflectiveRapidJSON::JsonType
JsonType
The JsonType enum specifies the JSON data type.
Definition: errorhandling.h:37
ReflectiveRapidJSON::JsonDeserializationErrors::reportConversionError
void reportConversionError(JsonType jsonType)
Reports a conversion error.
Definition: errorhandling.h:239
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::UnexpectedDuplicate
@ UnexpectedDuplicate
ReflectiveRapidJSON::JsonDeserializationErrors::currentIndex
std::size_t currentIndex
The index in the array which is currently processed.
Definition: errorhandling.h:167
ReflectiveRapidJSON::JsonDeserializationError::expectedType
JsonType expectedType
The expected type (might not be relevant for all error kinds).
Definition: errorhandling.h:116
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn
ThrowOn
The list of fatal error types in form of flags.
Definition: errorhandling.h:169
ReflectiveRapidJSON
Definition: traits.h:13
ReflectiveRapidJSON::JsonDeserializationErrors::currentMember
const char * currentMember
The name of the member (in currentRecord) which is currently being processed.
Definition: errorhandling.h:165
ReflectiveRapidJSON::JsonDeserializationErrors::reportArraySizeMismatch
void reportArraySizeMismatch()
Reports an array size mismatch.
Definition: errorhandling.h:228
ReflectiveRapidJSON::JsonDeserializationError::member
const char * member
The name of the member which was being processed when the error was ascertained.
Definition: errorhandling.h:122
ReflectiveRapidJSON::JsonDeserializationError::noIndex
static constexpr std::size_t noIndex
Indicates no array was being processed when the error occured.
Definition: errorhandling.h:127
ReflectiveRapidJSON::JsonType::String
@ String
ReflectiveRapidJSON::JsonType::Array
@ Array
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::All
@ All
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::TypeMismatch
@ TypeMismatch
ReflectiveRapidJSON::JsonDeserializationError::index
std::size_t index
The index in the array which was being processed when the error was ascertained.
Definition: errorhandling.h:124
ReflectiveRapidJSON::JsonDeserializationErrorKind
JsonDeserializationErrorKind
The JsonDeserializationErrorKind enum specifies which kind of error happend when populating variables...
Definition: errorhandling.h:24
ReflectiveRapidJSON::JsonDeserializationErrorKind::InvalidVariantObject
@ InvalidVariantObject
ReflectiveRapidJSON::JsonDeserializationErrorKind::TypeMismatch
@ TypeMismatch
ReflectiveRapidJSON::JsonDeserializationError
The JsonDeserializationError struct describes any errors of fromJson() except such caused by invalid ...
Definition: errorhandling.h:109
ReflectiveRapidJSON::JsonDeserializationError::actualType
JsonType actualType
The actual type (might not be relevant for all error kinds).
Definition: errorhandling.h:118
ReflectiveRapidJSON::JsonDeserializationErrors::throwOn
enum ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn throwOn
ReflectiveRapidJSON::JsonDeserializationErrorKind::ArraySizeMismatch
@ ArraySizeMismatch
ReflectiveRapidJSON::JsonType::Bool
@ Bool
ReflectiveRapidJSON::JsonDeserializationError::record
const char * record
The name of the class or struct which was being processed when the error was ascertained.
Definition: errorhandling.h:120
ReflectiveRapidJSON::JsonDeserializationErrors::reportTypeMismatch
void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
Reports a type mismatch between.
Definition: errorhandling.h:216
ReflectiveRapidJSON::JsonType::Null
@ Null
ReflectiveRapidJSON::JsonDeserializationErrors::reportUnexpectedDuplicate
void reportUnexpectedDuplicate(JsonType jsonType)
Reports an unexpected duplicate.
Definition: errorhandling.h:250
ReflectiveRapidJSON::JsonDeserializationErrorKind::UnexpectedDuplicate
@ UnexpectedDuplicate
ReflectiveRapidJSON::JsonType::Number
@ Number
ReflectiveRapidJSON::JsonType::Object
@ Object
ReflectiveRapidJSON::JsonDeserializationError::JsonDeserializationError
JsonDeserializationError(JsonDeserializationErrorKind kind, JsonType expectedType, JsonType actualType, const char *record, const char *member=nullptr, std::size_t index=noIndex)
Constructs a new JsonDeserializationError.
Definition: errorhandling.h:134
ReflectiveRapidJSON::operator|
constexpr JsonDeserializationErrors::ThrowOn operator|(JsonDeserializationErrors::ThrowOn lhs, JsonDeserializationErrors::ThrowOn rhs)
Combines to ThrowOn values.
Definition: errorhandling.h:196