Reflection for RapidJSON
0.0.17
Reflection for serializing/deserializing with RapidJSON
Toggle main menu visibility
Loading...
Searching...
No Matches
json
serializable.h
Go to the documentation of this file.
1
#ifndef REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
2
#define REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
3
9
10
#include "
./reflector.h
"
11
12
#include <rapidjson/document.h>
13
14
#include <string>
15
16
namespace
ReflectiveRapidJSON
{
17
21
template
<
typename
Type>
struct
JsonSerializable
{
22
// RapidJSON-level API
23
void
push
(RAPIDJSON_NAMESPACE::Value &container);
24
void
push
(RAPIDJSON_NAMESPACE::Value &container,
const
char
*name);
25
26
// high-level API
27
RAPIDJSON_NAMESPACE::StringBuffer
toJson
()
const
;
28
RAPIDJSON_NAMESPACE::Document
toJsonDocument
()
const
;
29
static
Type
fromJson
(
const
char
*json, std::size_t jsonSize,
JsonDeserializationErrors
*errors =
nullptr
);
30
static
Type
fromJson
(
const
char
*json,
JsonDeserializationErrors
*errors =
nullptr
);
31
static
Type
fromJson
(
const
std::string &json,
JsonDeserializationErrors
*errors =
nullptr
);
32
33
static
constexpr
const
char
*
qualifiedName
=
"ReflectiveRapidJSON::JsonSerializable"
;
34
35
#if __cplusplus > 201707L
36
bool
operator==(
const
JsonSerializable<Type>
&)
const
=
default
;
37
#endif
38
};
39
43
template
<
typename
Type>
void
JsonSerializable<Type>::push
(RAPIDJSON_NAMESPACE::Value &container)
44
{
45
return
JsonReflector::push<Type>
(*
this
, container);
46
}
47
51
template
<
typename
Type>
void
JsonSerializable<Type>::push
(RAPIDJSON_NAMESPACE::Value &container,
const
char
*name)
52
{
53
return
JsonReflector::push<Type>
(*
this
, name, container);
54
}
55
60
template
<
typename
Type> RAPIDJSON_NAMESPACE::StringBuffer
JsonSerializable<Type>::toJson
()
const
61
{
62
return
JsonReflector::toJson<Type>
(
static_cast<
const
Type &
>
(*
this
));
63
}
64
69
template
<
typename
Type> RAPIDJSON_NAMESPACE::Document
JsonSerializable<Type>::toJsonDocument
()
const
70
{
71
return
JsonReflector::toJsonDocument<Type>
(
static_cast<
const
Type &
>
(*
this
));
72
}
73
77
template
<
typename
Type> Type
JsonSerializable<Type>::fromJson
(
const
char
*json, std::size_t jsonSize,
JsonDeserializationErrors
*errors)
78
{
79
return
JsonReflector::fromJson<Type>
(json, jsonSize, errors);
80
}
81
85
template
<
typename
Type> Type
JsonSerializable<Type>::fromJson
(
const
char
*json,
JsonDeserializationErrors
*errors)
86
{
87
return
JsonReflector::fromJson<Type>
(json, std::strlen(json), errors);
88
}
89
93
template
<
typename
Type> Type
JsonSerializable<Type>::fromJson
(
const
std::string &json,
JsonDeserializationErrors
*errors)
94
{
95
return
JsonReflector::fromJson<Type>
(json.data(), json.size(), errors);
96
}
97
101
template
<
typename
Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * =
nullptr
>
JsonSerializable<Type>
&
as
(Type &serializable)
102
{
103
return
static_cast<
JsonSerializable<Type>
&
>
(serializable);
104
}
105
109
template
<
typename
Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * =
nullptr
>
110
const
JsonSerializable<Type>
&
as
(
const
Type &serializable)
111
{
112
return
static_cast<
const
JsonSerializable<Type>
&
>
(serializable);
113
}
114
122
#define REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(T) \
123
template <> struct ReflectiveRapidJSON::AdaptedJsonSerializable<T> : Traits::Bool<true> {}
124
129
#define REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T) \
130
friend void ::ReflectiveRapidJSON::JsonReflector::push<T>( \
131
const T &reflectable, ::RAPIDJSON_NAMESPACE::Value::Object &value, ::RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
132
137
#define REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T) \
138
friend void ::ReflectiveRapidJSON::JsonReflector::pull<T>(T & reflectable, \
139
const ::RAPIDJSON_NAMESPACE::GenericValue<::RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value, \
140
::ReflectiveRapidJSON::JsonDeserializationErrors *errors)
141
146
#define REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(T) \
147
REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T); \
148
REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T)
149
150
}
// namespace ReflectiveRapidJSON
151
152
#endif
// REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
reflector.h
Contains functions to (de)serialize basic types such as int, double, bool, std::string,...
ReflectiveRapidJSON::JsonReflector::toJson
RAPIDJSON_NAMESPACE::StringBuffer toJson(const Type &reflectable)
Serializes the specified reflectable.
Definition
reflector.h:1086
ReflectiveRapidJSON::JsonReflector::fromJson
Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Deserializes the specified JSON to.
Definition
reflector.h:1098
ReflectiveRapidJSON::JsonReflector::push
void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
Pushes the specified reflectable to the specified value.
Definition
reflector.h:140
ReflectiveRapidJSON::JsonReflector::toJsonDocument
RAPIDJSON_NAMESPACE::Document toJsonDocument(const Type &reflectable)
Serializes the specified reflectable which has a custom type or can be mapped to and object.
Definition
reflector.h:1019
ReflectiveRapidJSON
Definition
traits.h:13
ReflectiveRapidJSON::as
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
Definition
serializable.h:101
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition
errorhandling.h:154
ReflectiveRapidJSON::JsonSerializable
The JsonSerializable class provides the CRTP-base for (de)serializable objects.
Definition
serializable.h:21
ReflectiveRapidJSON::JsonSerializable::toJson
RAPIDJSON_NAMESPACE::StringBuffer toJson() const
Converts the object to its JSON representation (rapidjson::StringBuffer).
Definition
serializable.h:60
ReflectiveRapidJSON::JsonSerializable::fromJson
static Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Constructs a new object from the specified JSON.
Definition
serializable.h:77
ReflectiveRapidJSON::JsonSerializable::push
void push(RAPIDJSON_NAMESPACE::Value &container)
Pushes the object to the specified RapidJSON array.
Definition
serializable.h:43
ReflectiveRapidJSON::JsonSerializable::toJsonDocument
RAPIDJSON_NAMESPACE::Document toJsonDocument() const
Converts the object to its JSON representation (rapidjson::Document).
Definition
serializable.h:69
ReflectiveRapidJSON::JsonSerializable::qualifiedName
static constexpr const char * qualifiedName
Definition
serializable.h:33
Generated on
for Reflection for RapidJSON by
1.18.0