3 #include "Core/Global.h"
4 #include "Core/Exceptions.h"
5 #include "Serializing/Json/JsonFieldName.h"
6 #include "Serializing/SerializingDefines.h"
7 #include "Serializing/Core/SerializableData.h"
8 #include "IO/JsonObject/JsonObject.h"
14 static auto InvalidObjectName = std::runtime_error(
"Object invalid or empty name!");
16 static auto InvalidData = std::runtime_error(
"Object does not matches the struct!");
18 static auto DifferentTypesException = std::runtime_error(
"Different types!");
20 static auto InvalidTypeException = std::runtime_error(
"Invalid type!");
83 template<
typename T =
int,
typename TData = IData<T>>
147 if (std::is_arithmetic_v<T>) {
157 [[nodiscard]] std::string_view
name()
const {
158 return this->m_oFieldName.
name();
164 return this->m_bIsNull;
170 this->m_bIsNull = i_bState;
179 ? i_oObject.
find(this->name())
180 : ((this->
name().empty()) ? &i_oObject
201 if (!i_bIsStrict && this->
isNull()) {
205 if (this->
name().empty()) {
206 throw InvalidObjectName;
236 [[nodiscard]]
size_t size()
const override {
237 return sizeof(*this);
247 template<
typename TJsonFieldName,
248 std::enable_if_t<std::is_same_v<CJsonFieldName, TJsonFieldName>,
bool> =
true
251 this->m_oFieldName = i_tValue;
259 template<
typename TJsonFieldName,
260 std::enable_if_t<std::is_same_v<CJsonFieldName, TJsonFieldName>,
bool> =
true
263 this->m_oFieldName = std::move(i_tValue);
276 template<
typename TField,
typename TStruct>
278 TField *pField =
nullptr;
279 IterateSerializableStruct<TField>(&i_oStruct, [&](TField *i_pData) {
280 if (i_pData->name() == i_sName) {
291 std::false_type is_json_type_class_impl(...);
293 template<
typename T =
int,
typename TData = IData<T>>
294 std::true_type is_json_type_class_impl(IJsonType<T, TData> *);
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
void setNull()
Sets the type of the JSON object to null.
Definition: JsonObject.h:132
EJsonType type() const
Returns the type of the JSON object.
Definition: JsonObject.h:123
CJsonObject * find(std::string_view i_stName)
Finds a value in the JSON object.
Definition: JsonObject.cpp:17
CJsonObject & get(const std::string_view i_stName)
Gets the value associated with the specified name.
Definition: JsonObject.h:128
A class representing a JSON field name.
Definition: JsonFieldName.h:49
const char * name() const
Retrieves the underlying name string.
Definition: JsonFieldName.h:88
Represents a JSON data type with serialization and deserialization functionality.
Definition: JsonSerializableType.h:84
bool isNull() const
Checks if the JSON field is null.
Definition: JsonSerializableType.h:163
virtual ~IJsonType()=default
Destructor.
IO::CJsonObject * getObject(IO::CJsonObject &i_oObject, const bool i_bIsStrict)
Retrieves the JSON object for the specified JSON field.
Definition: JsonSerializableType.h:177
virtual bool onDeserialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict)
Called during deserialization to populate the object from a JSON object.
Definition: JsonSerializableType.h:105
virtual bool onSerialize(IO::CJsonObject &i_oObject) const
Called during deserialization to populate the object from a JSON object (non-strict mode).
Definition: JsonSerializableType.h:127
std::string_view name() const
Retrieves the name of the JSON field.
Definition: JsonSerializableType.h:157
bool serialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict=false) const
Serializes the data to a JSON object.
Definition: JsonSerializableType.h:142
bool doSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict=false) const
Serializes the object to a JSON object.
Definition: JsonSerializableType.h:229
virtual bool onDeserialize(IO::CJsonObject &i_oObject)
Called during deserialization to populate the object from a JSON object (non-strict mode).
Definition: JsonSerializableType.h:120
void setNull(bool i_bState)
Sets the null state of the JSON field.
Definition: JsonSerializableType.h:169
IJsonType & operator=(const TJsonFieldName &i_tValue)
Assigns a JSON field name to the object.
Definition: JsonSerializableType.h:250
IJsonType & operator=(TJsonFieldName &&i_tValue)
Assigns a JSON field name to the object (move assignment).
Definition: JsonSerializableType.h:262
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
bool doDeserialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict=false)
Deserializes the object from a JSON object.
Definition: JsonSerializableType.h:221
virtual bool onSerialize(IO::CJsonObject &i_oObject, const bool i_bIsStrict) const
Called during serialization to convert the object to a JSON object.
Definition: JsonSerializableType.h:113
size_t size() const override
Retrieves the size of the object.
Definition: JsonSerializableType.h:236
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).
The namespace encapsulating serializing related functionality in the Devel framework.
decltype(is_json_type_class_impl(std::declval< T * >())) is_json_type_class
Checks if a type is a JSON type class.
Definition: JsonSerializableType.h:299
TField * GetJsonFieldByName(TStruct &i_oStruct, const std::string_view i_sName)
Retrieves a JSON field by name from a struct.
Definition: JsonSerializableType.h:277