Devel Lib
JsonSerializableArray.h
1 #pragma once
2 
3 #include "Serializing/Json/JsonSerializableType.h"
4 #include "Serializing/Json/JsonSerializer.h"
5 
8 namespace Devel::Serializing {
9 
10 #pragma pack(push, 1)
11 
15  template<typename TStruct>
16  class IJsonArray : public IJsonType<TStruct, IData<std::vector<TStruct>>> {
17  public:
20  IJsonArray(const CJsonFieldName i_oFieldName) : IJsonType<TStruct, IData<std::vector<TStruct>>>(
21  i_oFieldName) {};
22  private:
27  bool onDeserialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) final {
28  IO::CJsonObject *pObject = this->getObject(i_oObject, i_bIsStrict);
29  if (!pObject) {
30  return true;
31  } else if (pObject->type() != IO::EJsonType::JTArray) {
32  throw DifferentTypesException;
33  }
34 
35  this->setNull(false);
36  IO::CJsonArray oArray = pObject->toArray();
37  for (const IO::CJsonObject &oItem: oArray) {
38  TStruct grStruct;
39 
40  if (oItem.type() == IO::EJsonType::JTObject || oItem.type() == IO::EJsonType::JTArray) {
41  if (!DeserializeObject(grStruct, const_cast<IO::CJsonObject &>(oItem))) {
42  return false;
43  }
44  } else {
45  if constexpr (is_data_class<TStruct>::value) {
46  if (!grStruct.doDeserialize(oItem.c_str())) {
47  return false;
48  }
49  } else if constexpr (std::is_same_v<IO::CJsonObject, TStruct>) {
50  grStruct = oItem;
51  } else {
52  throw InvalidTypeException;
53  }
54  }
55 
56  this->m_tValue.push_back(std::move(grStruct));
57  }
58 
59  return true;
60  }
61 
66  bool onSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict = false) const final {
67  if (this->checkSerializeObject(i_oObject, i_bIsStrict)) {
68  return true;
69  }
70 
71  IO::CJsonArray oArray;
72  for (const TStruct &grItem: this->m_tValue) {
73  IO::CJsonObject oObject;
74 
75  if constexpr (std::is_base_of_v<IStruct, TStruct>) {
76  if (!SerializeObject(grItem, oObject)) {
77  return false;
78  }
79  } else if constexpr (is_json_type_class<TStruct>::value) {
80  return grItem.onSerialize(oObject);
81  } else if constexpr (is_data_class<TStruct>::value) {
82 
83  oObject = grItem.value();
84  } else {
85  throw InvalidTypeException;
86  }
87 
88  oArray.push_back(oObject);
89  }
90 
91  if (this->name().empty()) {
92  i_oObject = oArray;
93  } else {
94  i_oObject.get(this->name()) = oArray;
95  }
96  return true;
97  }
98 
102  bool onDeserialize(IO::CJsonObject &i_oObject) final {
103  return this->onDeserialize(i_oObject, false);
104  }
105 
109  bool onSerialize(IO::CJsonObject &i_oObject) const final {
110  return this->onSerialize(i_oObject, false);
111  }
112 
113  public:
114  using IJsonType<TStruct, IData<std::vector<TStruct>>>::IJsonType;
115 
119  auto &operator=(const IJsonArray<TStruct> &i_tValue) {
120  this->setNull(false);
121  this->m_tValue = i_tValue;
122  return *this;
123  }
124 
128  auto &operator=(IJsonArray<TStruct> &&i_tValue) {
129  this->setNull(false);
130  this->m_tValue = i_tValue;
131  return *this;
132  }
133  };
134 
135 #pragma pack(pop)
136 }
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