Reflection for RapidJSON  0.0.10
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,
29 };
30 
35 enum class JsonType : std::uint8_t {
36  Null,
37  Number,
38  Bool,
39  String,
40  Array,
41  Object,
42 };
43 
44 // define helper functions which return the JsonType for the C++ type specified as template parameter
45 
46 template <typename Type,
47  Traits::EnableIf<Traits::Not<std::is_same<Type, bool>>, Traits::Any<std::is_integral<Type>, std::is_floating_point<Type>>> * = nullptr>
48 constexpr JsonType jsonType()
49 {
50  return JsonType::Number;
51 }
52 
53 template <typename Type, Traits::EnableIfAny<std::is_same<Type, bool>> * = nullptr> constexpr JsonType jsonType()
54 {
55  return JsonType::Bool;
56 }
57 
58 template <typename Type, Traits::EnableIfAny<Traits::IsString<Type>, Traits::IsCString<Type>> * = nullptr> constexpr JsonType jsonType()
59 {
60  return JsonType::String;
61 }
62 
63 template <typename Type,
64  Traits::EnableIf<Traits::IsIteratable<Type>,
65  Traits::Not<Traits::Any<Traits::IsString<Type>, Traits::IsSpecializationOf<Type, std::map>,
66  Traits::IsSpecializationOf<Type, std::unordered_map>>>> * = nullptr>
67 constexpr JsonType jsonType()
68 {
69  return JsonType::Array;
70 }
71 
72 template <typename Type,
73  Traits::DisableIfAny<std::is_integral<Type>, std::is_floating_point<Type>, Traits::IsString<Type>, Traits::IsCString<Type>,
74  Traits::All<Traits::IsIteratable<Type>,
75  Traits::Not<Traits::Any<Traits::IsString<Type>, Traits::IsSpecializationOf<Type, std::map>,
76  Traits::IsSpecializationOf<Type, std::unordered_map>>>>> * = nullptr>
77 constexpr JsonType jsonType()
78 {
79  return JsonType::Object;
80 }
81 
85 constexpr JsonType jsonType(RAPIDJSON_NAMESPACE::Type type)
86 {
87  switch (type) {
88  case RAPIDJSON_NAMESPACE::kFalseType:
89  case RAPIDJSON_NAMESPACE::kTrueType:
90  return JsonType::Bool;
91  case RAPIDJSON_NAMESPACE::kObjectType:
92  return JsonType::Object;
93  case RAPIDJSON_NAMESPACE::kArrayType:
94  return JsonType::Array;
95  case RAPIDJSON_NAMESPACE::kStringType:
96  return JsonType::String;
97  case RAPIDJSON_NAMESPACE::kNumberType:
98  return JsonType::Number;
99  default:
100  return JsonType::Null;
101  }
102 }
103 
109  const char *member = nullptr, std::size_t index = noIndex);
110 
118  const char *record;
120  const char *member;
122  std::size_t index;
123 
125  static constexpr std::size_t noIndex = std::numeric_limits<std::size_t>::max();
126 };
127 
133  JsonDeserializationErrorKind kind, JsonType expectedType, JsonType actualType, const char *record, const char *member, std::size_t index)
134  : kind(kind)
135  , expectedType(expectedType)
136  , actualType(actualType)
137  , record(record)
138  , member(member)
139  , index(index)
140 {
141 }
142 
152 struct JsonDeserializationErrors : public std::vector<JsonDeserializationError> {
154 
155  template <typename ExpectedType> void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType);
159 
161  const char *currentRecord;
163  const char *currentMember;
165  std::size_t currentIndex;
167  enum class ThrowOn : std::uint8_t {
168  None = 0,
169  TypeMismatch = 0x1,
170  ArraySizeMismatch = 0x2,
171  ConversionError = 0x4,
172  UnexpectedDuplicate = 0x8
173  } throwOn;
174 
175 private:
176  void throwMaybe(ThrowOn on) const;
177 };
178 
183  : currentRecord("[document]")
184  , currentMember(nullptr)
185  , currentIndex(JsonDeserializationError::noIndex)
186  , throwOn(ThrowOn::None)
187 {
188 }
189 
194 {
195  return static_cast<JsonDeserializationErrors::ThrowOn>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
196 }
197 
203 inline void JsonDeserializationErrors::throwMaybe(ThrowOn on) const
204 {
205  if (static_cast<std::uint8_t>(throwOn) & static_cast<std::uint8_t>(on)) {
206  throw back();
207  }
208 }
209 
213 template <typename ExpectedType> inline void JsonDeserializationErrors::reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
214 {
215  emplace_back(
217  throwMaybe(ThrowOn::TypeMismatch);
218 }
219 
226 {
228  throwMaybe(ThrowOn::ArraySizeMismatch);
229 }
230 
237 {
239  throwMaybe(ThrowOn::ConversionError);
240 }
241 
248 {
250  throwMaybe(ThrowOn::UnexpectedDuplicate);
251 }
252 
253 } // namespace ReflectiveRapidJSON
254 
255 #endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition: errorhandling.h:152
ReflectiveRapidJSON::JsonDeserializationErrors::JsonDeserializationErrors
JsonDeserializationErrors()
Creates an empty JsonDeserializationErrors object with default context and no errors considered fatal...
Definition: errorhandling.h:182
ReflectiveRapidJSON::JsonDeserializationErrors::throwOn
enum ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn throwOn
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::ArraySizeMismatch
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::ConversionError
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::None
ReflectiveRapidJSON::jsonType
constexpr JsonType jsonType()
Definition: errorhandling.h:48
ReflectiveRapidJSON::JsonDeserializationErrors::currentRecord
const char * currentRecord
The name of the class or struct which is currently being processed.
Definition: errorhandling.h:161
ReflectiveRapidJSON::JsonDeserializationError::kind
JsonDeserializationErrorKind kind
Which kind of error occured.
Definition: errorhandling.h:112
ReflectiveRapidJSON::JsonDeserializationErrorKind::ConversionError
ReflectiveRapidJSON::JsonType
JsonType
The JsonType enum specifies the JSON data type.
Definition: errorhandling.h:35
ReflectiveRapidJSON::JsonDeserializationErrors::reportConversionError
void reportConversionError(JsonType jsonType)
Reports a conversion error.
Definition: errorhandling.h:236
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::UnexpectedDuplicate
ReflectiveRapidJSON::JsonDeserializationErrors::currentIndex
std::size_t currentIndex
The index in the array which is currently processed.
Definition: errorhandling.h:165
ReflectiveRapidJSON::JsonDeserializationError::expectedType
JsonType expectedType
The expected type (might not be relevant for all error kinds).
Definition: errorhandling.h:114
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn
ThrowOn
The list of fatal error types in form of flags.
Definition: errorhandling.h:167
ReflectiveRapidJSON
Definition: traits.h:12
ReflectiveRapidJSON::JsonDeserializationErrors::currentMember
const char * currentMember
The name of the member (in currentRecord) which is currently being processed.
Definition: errorhandling.h:163
ReflectiveRapidJSON::JsonDeserializationErrors::reportArraySizeMismatch
void reportArraySizeMismatch()
Reports an array size mismatch.
Definition: errorhandling.h:225
ReflectiveRapidJSON::JsonDeserializationError::member
const char * member
The name of the member which was being processed when the error was ascertained.
Definition: errorhandling.h:120
ReflectiveRapidJSON::JsonDeserializationError::noIndex
static constexpr std::size_t noIndex
Indicates no array was being processed when the error occured.
Definition: errorhandling.h:125
ReflectiveRapidJSON::JsonType::String
ReflectiveRapidJSON::JsonType::Array
ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn::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:122
ReflectiveRapidJSON::JsonDeserializationErrorKind
JsonDeserializationErrorKind
The JsonDeserializationErrorKind enum specifies which kind of error happend when populating variables...
Definition: errorhandling.h:24
ReflectiveRapidJSON::JsonDeserializationErrorKind::TypeMismatch
ReflectiveRapidJSON::JsonDeserializationError
The JsonDeserializationError struct describes any errors of fromJson() except such caused by invalid ...
Definition: errorhandling.h:107
ReflectiveRapidJSON::JsonDeserializationError::actualType
JsonType actualType
The actual type (might not be relevant for all error kinds).
Definition: errorhandling.h:116
ReflectiveRapidJSON::JsonDeserializationErrorKind::ArraySizeMismatch
ReflectiveRapidJSON::JsonType::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:118
ReflectiveRapidJSON::JsonDeserializationErrors::reportTypeMismatch
void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
Reports a type mismatch between.
Definition: errorhandling.h:213
ReflectiveRapidJSON::JsonType::Null
ReflectiveRapidJSON::JsonDeserializationErrors::reportUnexpectedDuplicate
void reportUnexpectedDuplicate(JsonType jsonType)
Reports an unexpected duplicate.
Definition: errorhandling.h:247
ReflectiveRapidJSON::JsonDeserializationErrorKind::UnexpectedDuplicate
ReflectiveRapidJSON::JsonType::Number
ReflectiveRapidJSON::JsonType::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:132
ReflectiveRapidJSON::operator|
constexpr JsonDeserializationErrors::ThrowOn operator|(JsonDeserializationErrors::ThrowOn lhs, JsonDeserializationErrors::ThrowOn rhs)
Combines to ThrowOn values.
Definition: errorhandling.h:193