Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
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
15namespace ReflectiveRapidJSON {
16
17inline 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
Contains helper for error handling when deserializing JSON files.
std::string formatJsonDeserializationError(const JsonDeserializationError &error)
std::string_view jsonTypeToString(JsonType jsonType)
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.
JsonDeserializationErrorKind kind
Which kind of error occurred.