Devel Lib
JsonSerializableNumber.h
1 #pragma once
2 
3 #include "Serializing/Core/Types/SerializableArithmetic.h"
4 #include "Serializing/Json/JsonSerializableType.h"
7 namespace Devel::Serializing {
8 #pragma pack(push, 1)
9 
13  template<typename T>
14  class IJsonNumber : public IJsonType<T, IArithmetic<T>> {
15  public:
18  IJsonNumber(const CJsonFieldName i_oFieldName) : IJsonType<T, IArithmetic<T>>(i_oFieldName) {}
19 
20  private:
25  bool onDeserialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) override {
26  auto *pObject = this->getObject(i_oObject, i_bIsStrict);
27 
28  if (!pObject) {
29  return true;
30  } else if (pObject->type() == IO::EJsonType::JTNumber) {
31  if (std::is_same_v<T, float>) {
32  this->operator=(static_cast<T>(pObject->toFloat()));
33  } else if (std::is_same_v<T, double>) {
34  this->operator=(static_cast<T>(pObject->toDouble()));
35  } else if (sizeof(T) == 8) {
36  this->operator=(static_cast<T>(pObject->toUInt64()));
37  } else {
38  this->operator=(static_cast<T>(pObject->toInt()));
39  }
40  } else {
41  throw DifferentTypesException;
42  }
43 
44  return true;
45  }
46 
51  bool onSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) override {
52  return this->serialize(i_oObject, i_bIsStrict);
53  }
54 
58  bool onDeserialize(IO::CJsonObject &i_oObject) final {
59  return this->onDeserialize(i_oObject, false);
60  }
61 
65  bool onSerialize(IO::CJsonObject &i_oObject) final {
66  return this->onSerialize(i_oObject, false);
67  }
68 
69  public:
70  using IJsonType<T, IArithmetic<T>>::IJsonType;
71 
75  auto &operator=(const T &i_tValue) {
76  this->m_tValue = i_tValue;
77  this->setNull(false);
78  return *this;
79  }
80 
84  auto &operator=(T &&i_tValue) {
85  this->m_tValue = i_tValue;
86  this->setNull(false);
87  return *this;
88  }
89  };
90 
111 
112 #pragma pack(pop)
113 }
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
A class representing a JSON field name.
Definition: JsonFieldName.h:49
Class representing an arithmetic value for serialization.
Definition: SerializableArithmetic.h:19
Represents a JSON number data type and provides serialization and deserialization functionality for a...
Definition: JsonSerializableNumber.h:14
auto & operator=(T &&i_tValue)
Assigns a rvalue to the IJsonNumber object.
Definition: JsonSerializableNumber.h:84
IJsonNumber(const CJsonFieldName i_oFieldName)
Constructs an IJsonNumber object with the specified field name.
Definition: JsonSerializableNumber.h:18
auto & operator=(const T &i_tValue)
Assigns a value to the IJsonNumber object.
Definition: JsonSerializableNumber.h:75
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
T m_tValue
The value.
Definition: SerializableData.h:78
@ JTNumber
A number type.
The namespace encapsulating serializing related functionality in the Devel framework.
IJsonNumber< short > IJsonShort
Typedef for IJsonNumber<short>.
Definition: JsonSerializableNumber.h:96
IJsonNumber< double > IJsonDouble
Typedef for IJsonNumber<double>.
Definition: JsonSerializableNumber.h:110
IJsonNumber< char > IJsonChar
Typedef for IJsonNumber<char>
Definition: JsonSerializableNumber.h:92
IJsonNumber< float > IJsonFloat
Typedef for IJsonNumber<float>.
Definition: JsonSerializableNumber.h:108
IJsonNumber< uint > IJsonUInt
Typedef for IJsonNumber<uint>.
Definition: JsonSerializableNumber.h:102
IJsonNumber< int64 > IJsonInt64
Typedef for IJsonNumber<int64>.
Definition: JsonSerializableNumber.h:104
IJsonNumber< int > IJsonInt
Typedef for IJsonNumber<int>.
Definition: JsonSerializableNumber.h:100
IJsonNumber< ushort > IJsonUShort
Typedef for IJsonNumber<ushort>.
Definition: JsonSerializableNumber.h:98
IJsonNumber< uint64 > IJsonUInt64
Typedef for IJsonNumber<uint64>.
Definition: JsonSerializableNumber.h:106
IJsonNumber< byte > IJsonByte
Typedef for IJsonNumber<byte>.
Definition: JsonSerializableNumber.h:94