Devel Lib
JsonSerializableStruct.h
1 #pragma once
2 
3 #include "Core/Global.h"
4 #include "Serializing/Json/JsonSerializableType.h"
5 #include "Serializing/Json/JsonSerializer.h"
6 
9 namespace Devel::Serializing {
10 #pragma pack(push, 1)
11 
15  template<typename TStruct>
16  class IJsonStruct : public IJsonType<TStruct, IData<TStruct>> {
17  public:
20  IJsonStruct(const CJsonFieldName i_oFieldName) : IJsonType<TStruct, IData<TStruct>>(i_oFieldName) {}
21 
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 
30  if (!pObject) {
31  return true;
32  }
33  if (pObject->type() != IO::EJsonType::JTObject) {
34  throw DifferentTypesException;
35  }
36 
37  return DeserializeObject(this->m_tValue, *pObject);
38  }
39 
44  bool onSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) const final {
45  if (this->checkSerializeObject(i_oObject, i_bIsStrict)) {
46  return true;
47  }
48 
49  return SerializeObject(this->m_tValue, i_oObject.get(this->name()), i_bIsStrict);
50  }
51 
55  bool onDeserialize(IO::CJsonObject &i_oObject) final {
56  return this->onDeserialize(i_oObject, false);
57  }
58 
62  bool onSerialize(IO::CJsonObject &i_oObject) const final {
63  return this->onSerialize(i_oObject, false);
64  }
65 
66  public:
67  using IJsonType<TStruct, IData<TStruct>>::IJsonType;
68 
72  IJsonStruct &operator=(const TStruct &i_tValue) {
73  this->m_tValue = i_tValue;
74  this->setNull(false);
75  return *this;
76  }
77 
81  IJsonStruct &operator=(TStruct &&i_tValue) {
82  this->m_tValue = std::move(i_tValue);
83  this->setNull(false);
84  return *this;
85  }
86  };
87 
88 #pragma pack(pop)
89 }
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
A class representing a JSON field name.
Definition: JsonFieldName.h:49
Template class representing a data field.
Definition: SerializableData.h:86
Represents a JSON structure data type and provides serialization and deserialization functionality.
Definition: JsonSerializableStruct.h:16
IJsonStruct(const CJsonFieldName i_oFieldName)
Constructs an IJsonStruct object with the specified field name.
Definition: JsonSerializableStruct.h:20
IJsonStruct & operator=(TStruct &&i_tValue)
Assigns an rvalue to the IJsonStruct object.
Definition: JsonSerializableStruct.h:81
IJsonStruct & operator=(const TStruct &i_tValue)
Assigns a value to the IJsonStruct object.
Definition: JsonSerializableStruct.h:72
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
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
@ 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