3 #include "Serializing/Json/JsonSerializableType.h"
4 #include "Serializing/Json/JsonSerializer.h"
15 template<
typename TStruct>
27 bool onDeserialize(
IO::CJsonObject &i_oObject,
const bool i_bIsStrict)
final {
32 throw DifferentTypesException;
36 IO::CJsonArray oArray = pObject->
toArray();
37 for (
const IO::CJsonObject &oItem: oArray) {
45 if constexpr (is_data_class<TStruct>::value) {
46 if (!grStruct.doDeserialize(oItem.c_str())) {
49 }
else if constexpr (std::is_same_v<IO::CJsonObject, TStruct>) {
52 throw InvalidTypeException;
56 this->
m_tValue.push_back(std::move(grStruct));
66 bool onSerialize(IO::CJsonObject &i_oObject,
const bool i_bIsStrict =
false) const final {
71 IO::CJsonArray oArray;
72 for (
const TStruct &grItem: this->
m_tValue) {
73 IO::CJsonObject oObject;
75 if constexpr (std::is_base_of_v<IStruct, TStruct>) {
79 }
else if constexpr (is_json_type_class<TStruct>::value) {
80 return grItem.onSerialize(oObject);
81 }
else if constexpr (is_data_class<TStruct>::value) {
83 oObject = grItem.value();
85 throw InvalidTypeException;
88 oArray.push_back(oObject);
91 if (this->
name().empty()) {
94 i_oObject.get(this->
name()) = oArray;
102 bool onDeserialize(IO::CJsonObject &i_oObject)
final {
103 return this->onDeserialize(i_oObject,
false);
109 bool onSerialize(IO::CJsonObject &i_oObject)
const final {
110 return this->onSerialize(i_oObject,
false);
114 using IJsonType<TStruct, IData<std::vector<TStruct>>>
::IJsonType;
121 this->m_tValue = i_tValue;
130 this->m_tValue = i_tValue;
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
EJsonType type() const
Returns the type of the JSON object.
Definition: JsonObject.h:123
CJsonArray toArray() const
Converts the JSON object to a JSON array.
Definition: JsonObject.cpp:44
A class representing a JSON field name.
Definition: JsonFieldName.h:49
Template class representing a data field.
Definition: SerializableData.h:86
Represents a JSON array of a specific type.
Definition: JsonSerializableArray.h:16
auto & operator=(const IJsonArray< TStruct > &i_tValue)
Assigns another instance of IJsonArray to this instance.
Definition: JsonSerializableArray.h:119
IJsonArray(const CJsonFieldName i_oFieldName)
Constructs an instance of the JSON array with the given field name.
Definition: JsonSerializableArray.h:20
auto & operator=(IJsonArray< TStruct > &&i_tValue)
Moves another instance of IJsonArray to this instance.
Definition: JsonSerializableArray.h:128
Represents a JSON data type with serialization and deserialization functionality.
Definition: JsonSerializableType.h:84
IO::CJsonObject * getObject(IO::CJsonObject &i_oObject, const bool i_bIsStrict)
Retrieves the JSON object for the specified JSON field.
Definition: JsonSerializableType.h:177
std::string_view name() const
Retrieves the name of the JSON field.
Definition: JsonSerializableType.h:157
void setNull(bool i_bState)
Sets the null state of the JSON field.
Definition: JsonSerializableType.h:169
bool checkSerializeObject(IO::CJsonObject &i_oObject, const bool i_bIsStrict) const
Checks if serialization of the object is allowed and handles null fields.
Definition: JsonSerializableType.h:200
IJsonType(const CJsonFieldName i_oFieldName)
Constructs an IJsonType object with the specified JSON field name.
Definition: JsonSerializableType.h:88
int m_tValue
The value.
Definition: SerializableData.h:78
@ JTArray
An array type (an ordered collection of values).
@ JTObject
An object type (an unordered set of name/value pairs).
The namespace encapsulating serializing related functionality in the Devel framework.
bool SerializeObject(const T &i_oStruct, IO::CJsonObject &i_oObject, const bool i_bIsStrict=false, size_t i_nStructSize=sizeof(T))
Serializes a struct into a JSON object.
Definition: JsonSerializer.h:51
bool DeserializeObject(T &i_oStruct, IO::CJsonObject &i_oObject, const bool i_bIsStrict=false, size_t i_nStructSize=sizeof(T))
Deserializes a JSON object into a struct.
Definition: JsonSerializer.h:17