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;
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);
32 static constexpr
const char *
qualifiedName =
"ReflectiveRapidJSON::JsonSerializable";
40 return JsonReflector::push<Type>(*
this, container);
48 return JsonReflector::push<Type>(*
this, name, container);
57 return JsonReflector::toJson<Type>(
static_cast<const Type &
>(*this));
65 return JsonReflector::fromJson<Type>(json, jsonSize, errors);
73 return JsonReflector::fromJson<Type>(json, std::strlen(json), errors);
81 return JsonReflector::fromJson<Type>(json.data(), json.size(), errors);
87 template <
typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>>* =
nullptr>
JsonSerializable<Type> &
as(Type &serializable)
95 template <
typename Type, Traits::EnableIf<std::is_base_of<JsonSerializable<Type>, Type>>* =
nullptr>
108 #define REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(T) \ 109 template <> struct ReflectiveRapidJSON::AdaptedJsonSerializable<T> : Traits::Bool<true> { \ 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) 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) 133 #define REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(T) \ 134 REFLECTIVE_RAPIDJSON_PUSH_PRIVATE_MEMBERS(T); \ 135 REFLECTIVE_RAPIDJSON_PULL_PRIVATE_MEMBERS(T) 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.
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.
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
JsonSerializable< Type > & as(Type &serializable)
Helps to disambiguate when inheritance is used.
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.