1 #ifndef REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H 2 #define REFLECTIVE_RAPIDJSON_JSON_SERIALIZABLE_H 12 #include <rapidjson/document.h> 21 template <
typename Type>
struct JsonSerializable {
23 void push(RAPIDJSON_NAMESPACE::Value &container);
24 void push(RAPIDJSON_NAMESPACE::Value &container,
const char *name);
27 RAPIDJSON_NAMESPACE::StringBuffer
toJson()
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);
33 static constexpr
const char *
qualifiedName =
"ReflectiveRapidJSON::JsonSerializable";
41 return JsonReflector::push<Type>(*
this, container);
49 return JsonReflector::push<Type>(*
this, name, container);
58 return JsonReflector::toJson<Type>(static_cast<const Type &>(*
this));
67 return JsonReflector::toJsonDocument<Type>(static_cast<const Type &>(*
this));
75 return JsonReflector::fromJson<Type>(json, jsonSize, errors);
83 return JsonReflector::fromJson<Type>(json, std::strlen(json), errors);
91 return JsonReflector::fromJson<Type>(json.data(), json.size(), errors);
97 template <
typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * =
nullptr>
JsonSerializable<Type> &
as(Type &serializable)
105 template <
typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>> * =
nullptr>
118 #define REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(T) \ 119 template <> struct ReflectiveRapidJSON::AdaptedJsonSerializable<T> : Traits::Bool<true> { \ 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) 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) 143 #define REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(T) \ 144 REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T); \ 145 REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T) 149 #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.
void push(RAPIDJSON_NAMESPACE::Value &container)
Pushes the object to the specified RapidJSON array.
static constexpr const char * qualifiedName
RAPIDJSON_NAMESPACE::StringBuffer toJson() const
Converts the object to its JSON representation (rapidjson::StringBuffer).
RAPIDJSON_NAMESPACE::Document toJsonDocument() const
Converts the object to its JSON representation (rapidjson::Document).
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
The JsonSerializable class provides the CRTP-base for (de)serializable objects.