Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
errorformatting.h
Go to the documentation of this file.
1 #ifndef REFLECTIVE_RAPIDJSON_JSON_ERROR_FORMATTING_H
2 #define REFLECTIVE_RAPIDJSON_JSON_ERROR_FORMATTING_H
3 
9 #include "./errorhandling.h"
10 
11 #include <c++utilities/conversion/stringbuilder.h>
12 
13 #include <string_view>
14 
15 namespace ReflectiveRapidJSON {
16 
17 inline std::string_view jsonTypeToString(JsonType jsonType)
18 {
19  switch (jsonType) {
21  return "null";
23  return "number";
25  return "bool";
27  return "string";
29  return "array";
31  return "object";
32  default:
33  return "?";
34  }
35 }
36 
38 {
39  using namespace CppUtilities;
40  std::string_view errorKind;
41  std::string additionalInfo;
42  switch (error.kind) {
44  errorKind = "type mismatch";
45  additionalInfo = ": expected \"" % jsonTypeToString(error.expectedType) % "\", got \"" % jsonTypeToString(error.actualType) + '\"';
46  break;
48  errorKind = "array size mismatch";
49  break;
51  errorKind = "conversion error";
52  break;
54  errorKind = "unexpected duplicate";
55  break;
57  errorKind = "invalid variant object";
58  break;
60  errorKind = "invalid variant index";
61  break;
62  default:
63  errorKind = "semantic error";
64  }
65  if (error.record && error.member) {
66  return errorKind % " within record \"" % error.record % "\" and member \"" % error.member % '\"' + additionalInfo;
67  } else if (error.record && error.index != JsonDeserializationError::noIndex) {
68  return errorKind % " within record \"" % error.record % "\" and index \"" % error.index % '\"' + additionalInfo;
69  } else if (error.record) {
70  return errorKind % " within record \"" % error.record % '\"' + additionalInfo;
71  } else {
72  return errorKind % " in document" + additionalInfo;
73  }
74 }
75 
76 } // namespace ReflectiveRapidJSON
77 
78 #endif // REFLECTIVE_RAPIDJSON_JSON_ERROR_FORMATTING_H
ReflectiveRapidJSON::JsonDeserializationErrorKind::InvalidVariantIndex
@ InvalidVariantIndex
ReflectiveRapidJSON::jsonType
constexpr JsonType jsonType()
Definition: errorhandling.h:50
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::JsonDeserializationError::expectedType
JsonType expectedType
The expected type (might not be relevant for all error kinds).
Definition: errorhandling.h:116
errorhandling.h
Contains helper for error handling when deserializing JSON files.
ReflectiveRapidJSON
Definition: traits.h:13
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::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::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::formatJsonDeserializationError
std::string formatJsonDeserializationError(const JsonDeserializationError &error)
Definition: errorformatting.h:37
ReflectiveRapidJSON::JsonDeserializationErrorKind::ArraySizeMismatch
@ ArraySizeMismatch
ReflectiveRapidJSON::JsonType::Bool
@ Bool
ReflectiveRapidJSON::jsonTypeToString
std::string_view jsonTypeToString(JsonType jsonType)
Definition: errorformatting.h:17
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::JsonType::Null
@ Null
ReflectiveRapidJSON::JsonDeserializationErrorKind::UnexpectedDuplicate
@ UnexpectedDuplicate
ReflectiveRapidJSON::JsonType::Number
@ Number
ReflectiveRapidJSON::JsonType::Object
@ Object