Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
serializable.h
Go to the documentation of this file.
1#ifndef REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
2#define REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
3
10#include "./reflector.h"
11
12#include <rapidjson/document.h>
13
14#include <string>
15
16namespace ReflectiveRapidJSON {
17
21template <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
43template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container)
44{
45 return JsonReflector::push<Type>(*this, container);
46}
47
51template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container, const char *name)
52{
53 return JsonReflector::push<Type>(*this, name, container);
54}
55
60template <typename Type> RAPIDJSON_NAMESPACE::StringBuffer JsonSerializable<Type>::toJson() const
61{
62 return JsonReflector::toJson<Type>(static_cast<const Type &>(*this));
63}
64
69template <typename Type> RAPIDJSON_NAMESPACE::Document JsonSerializable<Type>::toJsonDocument() const
70{
71 return JsonReflector::toJsonDocument<Type>(static_cast<const Type &>(*this));
72}
73
77template <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
85template <typename Type> Type JsonSerializable<Type>::fromJson(const char *json, JsonDeserializationErrors *errors)
86{
87 return JsonReflector::fromJson<Type>(json, std::strlen(json), errors);
88}
89
93template <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
101template <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
109template <typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * = nullptr>
110const 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
Contains functions to (de)serialize basic types such as int, double, bool, std::string,...
RAPIDJSON_NAMESPACE::StringBuffer toJson(const Type &reflectable)
Serializes the specified reflectable.
Definition reflector.h:1086
Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Deserializes the specified JSON to.
Definition reflector.h:1098
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
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
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
The JsonSerializable class provides the CRTP-base for (de)serializable objects.
RAPIDJSON_NAMESPACE::StringBuffer toJson() const
Converts the object to its JSON representation (rapidjson::StringBuffer).
static Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Constructs a new object from the specified JSON.
void push(RAPIDJSON_NAMESPACE::Value &container)
Pushes the object to the specified RapidJSON array.
RAPIDJSON_NAMESPACE::Document toJsonDocument() const
Converts the object to its JSON representation (rapidjson::Document).
static constexpr const char * qualifiedName