Devel Lib
JsonSerializableString.h
1 #pragma once
2 
3 #include "Serializing/Json/JsonSerializableType.h"
4 
7 namespace Devel::Serializing {
8 #pragma pack(push, 1)
9 
12  class IJsonString : public IJsonType<std::string, IData<std::string>> {
13  public:
16  IJsonString(const CJsonFieldName i_oFieldName) : IJsonType<std::string, IData<std::string>>(i_oFieldName) {}
17 
18  private:
23  bool onDeserialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) override {
24  IO::CJsonObject *pObject = this->getObject(i_oObject, i_bIsStrict);
25 
26  if (!pObject) {
27  return true;
28  } else if (pObject->type() != IO::EJsonType::JTString) {
29  throw DifferentTypesException;
30  }
31 
32  this->operator=(*pObject);
33  return true;
34  }
35 
40  bool onSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) const override {
41  return this->serialize(i_oObject, i_bIsStrict);
42  }
43 
47  bool onDeserialize(IO::CJsonObject &i_oObject) final {
48  return this->onDeserialize(i_oObject, false);
49  }
50 
54  bool onSerialize(IO::CJsonObject &i_oObject) const final {
55  return this->onSerialize(i_oObject, false);
56  }
57 
58  public:
59  using IJsonType<std::string, IData<std::string>>::IJsonType;
60 
65  template<typename String,
66  std::enable_if_t<std::is_constructible_v<std::string, String>, bool> = true
67  >
68  IJsonString &operator=(const String &i_tValue) {
69  this->setNull(false);
70  this->m_tValue = i_tValue;
71  return *this;
72  }
73 
78  template<typename String,
79  std::enable_if_t<std::is_constructible_v<std::string, String>, bool> = true
80  >
81  IJsonString &operator=(String &&i_tValue) {
82  this->setNull(false);
83  this->m_tValue = std::move(i_tValue);
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 string data type and provides serialization and deserialization functionality.
Definition: JsonSerializableString.h:12
IJsonString & operator=(String &&i_tValue)
Assigns an rvalue to the IJsonString object.
Definition: JsonSerializableString.h:81
IJsonString(const CJsonFieldName i_oFieldName)
Constructs an IJsonString object with the specified field name.
Definition: JsonSerializableString.h:16
IJsonString & operator=(const String &i_tValue)
Assigns a value to the IJsonString object.
Definition: JsonSerializableString.h:68
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
bool serialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict=false) const
Serializes the data to a JSON object.
Definition: JsonSerializableType.h:142
void setNull(bool i_bState)
Sets the null state of the JSON field.
Definition: JsonSerializableType.h:169
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
@ JTString
A string type.
The namespace encapsulating serializing related functionality in the Devel framework.