Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
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 
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 
39 template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container)
40 {
41  return JsonReflector::push<Type>(*this, container);
42 }
43 
47 template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container, const char *name)
48 {
49  return JsonReflector::push<Type>(*this, name, container);
50 }
51 
56 template <typename Type> RAPIDJSON_NAMESPACE::StringBuffer JsonSerializable<Type>::toJson() const
57 {
58  return JsonReflector::toJson<Type>(static_cast<const Type &>(*this));
59 }
60 
65 template <typename Type> RAPIDJSON_NAMESPACE::Document JsonSerializable<Type>::toJsonDocument() const
66 {
67  return JsonReflector::toJsonDocument<Type>(static_cast<const Type &>(*this));
68 }
69 
73 template <typename Type> Type JsonSerializable<Type>::fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors)
74 {
75  return JsonReflector::fromJson<Type>(json, jsonSize, errors);
76 }
77 
81 template <typename Type> Type JsonSerializable<Type>::fromJson(const char *json, JsonDeserializationErrors *errors)
82 {
83  return JsonReflector::fromJson<Type>(json, std::strlen(json), errors);
84 }
85 
89 template <typename Type> Type JsonSerializable<Type>::fromJson(const std::string &json, JsonDeserializationErrors *errors)
90 {
91  return JsonReflector::fromJson<Type>(json.data(), json.size(), errors);
92 }
93 
97 template <typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * = nullptr> JsonSerializable<Type> &as(Type &serializable)
98 {
99  return static_cast<JsonSerializable<Type> &>(serializable);
100 }
101 
105 template <typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * = nullptr>
106 const JsonSerializable<Type> &as(const Type &serializable)
107 {
108  return static_cast<const JsonSerializable<Type> &>(serializable);
109 }
110 
118 #define REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(T) \
119  template <> struct ReflectiveRapidJSON::AdaptedJsonSerializable<T> : Traits::Bool<true> { \
120  }
121 
126 #define REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T) \
127  friend void ::ReflectiveRapidJSON::JsonReflector::push<T>( \
128  const T &reflectable, ::RAPIDJSON_NAMESPACE::Value::Object &value, ::RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
129 
134 #define REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T) \
135  friend void ::ReflectiveRapidJSON::JsonReflector::pull<T>(T & reflectable, \
136  const ::RAPIDJSON_NAMESPACE::GenericValue<::RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value, \
137  ::ReflectiveRapidJSON::JsonDeserializationErrors *errors)
138 
143 #define REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(T) \
144  REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T); \
145  REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T)
146 
147 } // namespace ReflectiveRapidJSON
148 
149 #endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition: errorhandling.h:154
reflector.h
Contains BinaryReader and BinaryWriter supporting binary (de)serialization of primitive and custom ty...
ReflectiveRapidJSON::JsonSerializable::toJsonDocument
RAPIDJSON_NAMESPACE::Document toJsonDocument() const
Converts the object to its JSON representation (rapidjson::Document).
Definition: serializable.h:65
ReflectiveRapidJSON::JsonSerializable::push
void push(RAPIDJSON_NAMESPACE::Value &container)
Pushes the object to the specified RapidJSON array.
Definition: serializable.h:39
ReflectiveRapidJSON
Definition: traits.h:13
ReflectiveRapidJSON::JsonSerializable
The JsonSerializable class provides the CRTP-base for (de)serializable objects.
Definition: reflector.h:33
ReflectiveRapidJSON::as
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
Definition: serializable.h:97
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:73
ReflectiveRapidJSON::JsonSerializable::qualifiedName
static constexpr const char * qualifiedName
Definition: serializable.h:33
ReflectiveRapidJSON::JsonSerializable::toJson
RAPIDJSON_NAMESPACE::StringBuffer toJson() const
Converts the object to its JSON representation (rapidjson::StringBuffer).
Definition: serializable.h:56