Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
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
19namespace ReflectiveRapidJSON {
20
32
37enum 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
48template <typename Type,
49 Traits::EnableIf<Traits::Not<std::is_same<Type, bool>>, Traits::Any<std::is_integral<Type>, std::is_floating_point<Type>>> * = nullptr>
50constexpr JsonType jsonType()
51{
52 return JsonType::Number;
53}
54
55template <typename Type, Traits::EnableIfAny<std::is_same<Type, bool>> * = nullptr> constexpr JsonType jsonType()
56{
57 return JsonType::Bool;
58}
59
60template <typename Type, Traits::EnableIfAny<Traits::IsString<Type>, Traits::IsCString<Type>> * = nullptr> constexpr JsonType jsonType()
61{
62 return JsonType::String;
63}
64
65template <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>
69constexpr JsonType jsonType()
70{
71 return JsonType::Array;
72}
73
74template <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>
79constexpr JsonType jsonType()
80{
81 return JsonType::Object;
82}
83
87constexpr 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
154struct JsonDeserializationErrors : public std::vector<JsonDeserializationError> {
156
157 template <typename ExpectedType> void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType);
158 template <RAPIDJSON_NAMESPACE::Type expectedType> void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType);
162
164 const char *currentRecord;
166 const char *currentMember;
168 std::size_t currentIndex;
170 enum class ThrowOn : std::uint8_t {
171 None = 0,
172 TypeMismatch = 0x1,
173 ArraySizeMismatch = 0x2,
174 ConversionError = 0x4,
176 All = 0xFF,
178
179private:
180 void throwMaybe(ThrowOn on) const;
181};
182
187 : currentRecord("[document]")
188 , currentMember(nullptr)
189 , currentIndex(JsonDeserializationError::noIndex)
190 , throwOn(ThrowOn::None)
191{
192}
193
198{
199 return static_cast<JsonDeserializationErrors::ThrowOn>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
200}
201
207inline void JsonDeserializationErrors::throwMaybe(ThrowOn on) const
208{
209 if (static_cast<std::uint8_t>(throwOn) & static_cast<std::uint8_t>(on)) {
210 throw back();
211 }
212}
213
217template <typename ExpectedType> inline void JsonDeserializationErrors::reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
218{
219 emplace_back(
221 throwMaybe(ThrowOn::TypeMismatch);
222}
223
227template <RAPIDJSON_NAMESPACE::Type expectedType> inline void JsonDeserializationErrors::reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
228{
229 emplace_back(
231 throwMaybe(ThrowOn::TypeMismatch);
232}
233
244
255
266
267} // namespace ReflectiveRapidJSON
268
269#endif // REFLECTIVE_RAPIDJSON_JSON_REFLECTOR_H
constexpr JsonDeserializationErrors::ThrowOn operator|(JsonDeserializationErrors::ThrowOn lhs, JsonDeserializationErrors::ThrowOn rhs)
Combines to ThrowOn values.
JsonDeserializationErrorKind
The JsonDeserializationErrorKind enum specifies which kind of error happend when populating variables...
JsonType
The JsonType enum specifies the JSON data type.
constexpr JsonType jsonType()
The JsonDeserializationError struct describes any errors of fromJson() except such caused by invalid ...
JsonType expectedType
The expected type (might not be relevant for all error kinds).
std::size_t index
The index in the array which was being processed when the error was ascertained.
JsonType actualType
The actual type (might not be relevant for all error kinds).
const char * member
The name of the member which was being processed when the error was ascertained.
const char * record
The name of the class or struct which was being processed when the error was ascertained.
static constexpr std::size_t noIndex
Indicates no array was being processed when the error occurred.
JsonDeserializationError(JsonDeserializationErrorKind kind, JsonType expectedType, JsonType actualType, const char *record, const char *member=nullptr, std::size_t index=noIndex)
Constructs a new JsonDeserializationError.
JsonDeserializationErrorKind kind
Which kind of error occurred.
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
void reportConversionError(JsonType jsonType)
Reports a conversion error.
void reportTypeMismatch(RAPIDJSON_NAMESPACE::Type presentType)
Reports a type mismatch between.
const char * currentMember
The name of the member (in currentRecord) which is currently being processed.
ThrowOn
The list of fatal error types in form of flags.
const char * currentRecord
The name of the class or struct which is currently being processed.
void reportUnexpectedDuplicate(JsonType jsonType)
Reports an unexpected duplicate.
void reportArraySizeMismatch()
Reports an array size mismatch.
enum ReflectiveRapidJSON::JsonDeserializationErrors::ThrowOn throwOn
std::size_t currentIndex
The index in the array which is currently processed.
JsonDeserializationErrors()
Creates an empty JsonDeserializationErrors object with default context and no errors considered fatal...