Reflection for RapidJSON  0.0.3
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  static Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors = nullptr);
29  static Type fromJson(const char *json, JsonDeserializationErrors *errors = nullptr);
30  static Type fromJson(const std::string &json, JsonDeserializationErrors *errors = nullptr);
31 
32  static constexpr const char *qualifiedName = "ReflectiveRapidJSON::JsonSerializable";
33 };
34 
38 template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container)
39 {
40  return JsonReflector::push<Type>(*this, container);
41 }
42 
46 template <typename Type> void JsonSerializable<Type>::push(RAPIDJSON_NAMESPACE::Value &container, const char *name)
47 {
48  return JsonReflector::push<Type>(*this, name, container);
49 }
50 
55 template <typename Type> RAPIDJSON_NAMESPACE::StringBuffer JsonSerializable<Type>::toJson() const
56 {
57  return JsonReflector::toJson<Type>(static_cast<const Type &>(*this));
58 }
59 
63 template <typename Type> Type JsonSerializable<Type>::fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors)
64 {
65  return JsonReflector::fromJson<Type>(json, jsonSize, errors);
66 }
67 
71 template <typename Type> Type JsonSerializable<Type>::fromJson(const char *json, JsonDeserializationErrors *errors)
72 {
73  return JsonReflector::fromJson<Type>(json, std::strlen(json), errors);
74 }
75 
79 template <typename Type> Type JsonSerializable<Type>::fromJson(const std::string &json, JsonDeserializationErrors *errors)
80 {
81  return JsonReflector::fromJson<Type>(json.data(), json.size(), errors);
82 }
83 
87 template <typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>>...> JsonSerializable<Type> &as(Type &serializable)
88 {
89  return static_cast<JsonSerializable<Type> &>(serializable);
90 }
91 
95 template <typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>>...>
96 const JsonSerializable<Type> &as(const Type &serializable)
97 {
98  return static_cast<const JsonSerializable<Type> &>(serializable);
99 }
100 
108 #define REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(T) \
109  template <> struct ReflectiveRapidJSON::AdaptedJsonSerializable<T> : Traits::Bool<true> { \
110  }
111 
116 #define REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T) \
117  friend void ::ReflectiveRapidJSON::JsonReflector::push<T>( \
118  const T &reflectable, ::RAPIDJSON_NAMESPACE::Value::Object &value, ::RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
119 
124 #define REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T) \
125  friend void ::ReflectiveRapidJSON::JsonReflector::pull<T>(T & reflectable, \
126  const ::RAPIDJSON_NAMESPACE::GenericValue<::RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value, \
127  ::ReflectiveRapidJSON::JsonDeserializationErrors *errors)
128 
133 #define REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(T) \
134  REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T); \
135  REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T)
136 
137 } // namespace ReflectiveRapidJSON
138 
139 #endif // REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H
static Type fromJson(const char *json, std::size_t jsonSize, JsonDeserializationErrors *errors=nullptr)
Constructs a new object from the specified JSON.
Definition: serializable.h:63
void push(RAPIDJSON_NAMESPACE::Value &container)
Pushes the object to the specified RapidJSON array.
Definition: serializable.h:38
static constexpr const char * qualifiedName
Definition: serializable.h:32
RAPIDJSON_NAMESPACE::StringBuffer toJson() const
Converts the object to its JSON representation.
Definition: serializable.h:55
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
Definition: serializable.h:87
Contains functions to (de)serialize basic types such as int, double, bool, std::string, std::vector, ...
The JsonSerializable class provides the CRTP-base for (de)serializable objects.
Definition: reflector.h:29