Devel Lib
Devel::Serializing::IJsonType< T, TData > Class Template Reference

Represents a JSON data type with serialization and deserialization functionality. More...

#include <JsonSerializableType.h>

Inheritance diagram for Devel::Serializing::IJsonType< T, TData >:
Collaboration diagram for Devel::Serializing::IJsonType< T, TData >:

Public Member Functions

bool doDeserialize (IO::CJsonObject &i_oObject, const bool i_bIsStrict=false)
 Deserializes the object from a JSON object. More...
 
bool doSerialize (IO::CJsonObject &i_oObject, const bool i_bIsStrict=false) const
 Serializes the object to a JSON object. More...
 
size_t size () const override
 Retrieves the size of the object. More...
 
template<typename TJsonFieldName , std::enable_if_t< std::is_same_v< CJsonFieldName, TJsonFieldName >, bool > = true>
IJsonTypeoperator= (const TJsonFieldName &i_tValue)
 Assigns a JSON field name to the object. More...
 
template<typename TJsonFieldName , std::enable_if_t< std::is_same_v< CJsonFieldName, TJsonFieldName >, bool > = true>
IJsonTypeoperator= (TJsonFieldName &&i_tValue)
 Assigns a JSON field name to the object (move assignment). More...
 
- Public Member Functions inherited from Devel::Serializing::IData< int >
virtual ~IData ()=default
 Default destructor.
 
bool doSerialize (IO::CWriteStream &i_oStream) const
 Serializes the data to a write stream. More...
 
bool doDeserialize (IO::CReadStream &i_oStream)
 Deserializes the data from a read stream. More...
 
bool doDeserialize (const char *i_szString)
 Deserializes the data from a string. More...
 
bool doDeserialize (std::vector< std::string >::const_iterator &i_oIt, const std::vector< std::string >::const_iterator &i_oItEnd)
 Deserializes the data from a range of strings. More...
 
virtual bool isHidden () const
 Returns whether the field is hidden. More...
 
virtual SCompareData compareData () const
 Returns the compare data for the data field. More...
 
auto begin () const
 Returns an iterator pointing to the beginning of the value. More...
 
auto end () const
 Returns an iterator pointing to the end of the value. More...
 
auto & operator[] (const size_t i_nIndex)
 Returns a reference to the value at the specified index. More...
 
const auto & operator[] (const size_t i_nIndex) const
 Returns a const reference to the value at the specified index. More...
 
 SerializerEnableIfNotArithmetic (IData< int > &) operator--()
 Pre-decrement operator. More...
 
 SerializerEnableIfNotArithmetic (IData< int > &) operator--(int)
 Post-decrement operator. More...
 
 SerializerEnableIfNotArithmetic (IData< int > &) operator++()
 Pre-increment operator. More...
 
 SerializerEnableIfNotArithmetic (IData< int > &) operator++(int)
 Post-increment operator. More...
 
- Public Member Functions inherited from Devel::Serializing::IValue< int >
 IValue (TArgs... i_tArgs)
 Constructs an IValue object with the specified arguments. More...
 
const int & value () const
 Returns the value. More...
 
int & value ()
 Returns a reference to the value. More...
 

Protected Member Functions

 IJsonType (const CJsonFieldName i_oFieldName)
 Constructs an IJsonType object with the specified JSON field name. More...
 
virtual ~IJsonType ()=default
 Destructor.
 
virtual bool onDeserialize (IO::CJsonObject &i_oObject, const bool i_bIsStrict)
 Called during deserialization to populate the object from a JSON object. More...
 
virtual bool onSerialize (IO::CJsonObject &i_oObject, const bool i_bIsStrict) const
 Called during serialization to convert the object to a JSON object. More...
 
virtual bool onDeserialize (IO::CJsonObject &i_oObject)
 Called during deserialization to populate the object from a JSON object (non-strict mode). More...
 
virtual bool onSerialize (IO::CJsonObject &i_oObject) const
 Called during deserialization to populate the object from a JSON object (non-strict mode). More...
 
bool serialize (IO::CJsonObject &i_oObject, const bool i_bIsStrict=false) const
 Serializes the data to a JSON object. More...
 
std::string_view name () const
 Retrieves the name of the JSON field. More...
 
bool isNull () const
 Checks if the JSON field is null. More...
 
void setNull (bool i_bState)
 Sets the null state of the JSON field. More...
 
IO::CJsonObjectgetObject (IO::CJsonObject &i_oObject, const bool i_bIsStrict)
 Retrieves the JSON object for the specified JSON field. More...
 
bool checkSerializeObject (IO::CJsonObject &i_oObject, const bool i_bIsStrict) const
 Checks if serialization of the object is allowed and handles null fields. More...
 

Additional Inherited Members

- Protected Attributes inherited from Devel::Serializing::IValue< int >
int m_tValue
 The value.
 

Detailed Description

template<typename T = int, typename TData = IData<T>>
class Devel::Serializing::IJsonType< T, TData >

Represents a JSON data type with serialization and deserialization functionality.

Template Parameters
TThe underlying data type of the JSON field.
TDataThe base class for serialization and deserialization operations. Example

This class represents a JSON data type and provides serialization and deserialization functionality.

Here is an example of how to use the IJsonType class:

#include "IJsonType.h"
#include "IO/JsonObject/JsonObject.h"
using namespace Devel::Serializing;
using namespace Devel::IO;
// Define a custom JSON data type
class MyJsonType : public IJsonType<int> {
public:
MyJsonType(const CJsonFieldName fieldName) : IJsonType<int>(fieldName) {}
protected:
bool onDeserialize(CJsonObject& object) override {
if (object.contains(this->name()) && object.get(this->name()).isInteger()) {
this->m_tValue = object.get(this->name()).toInt();
return true;
}
return false;
}
bool onSerialize(CJsonObject& object) const override {
object.get(this->name()) = this->m_tValue;
return true;
}
};
int main() {
// Create a JSON object
CJsonObject jsonObject;
// Create an instance of MyJsonType
MyJsonType myJsonType("myField");
// Serialize the MyJsonType object to the JSON object
myJsonType.doSerialize(jsonObject);
// Deserialize the JSON object into the MyJsonType object
myJsonType.doDeserialize(jsonObject);
return 0;
}
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
std::string_view name() const
Retrieves the name of the JSON field.
Definition: JsonSerializableType.h:157
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
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
The namespace encapsulating I/O related classes and functions in the Devel framework.
The namespace encapsulating serializing related functionality in the Devel framework.

This example demonstrates the creation of a custom JSON data type called MyJsonType, which derives from IJsonType. It overrides the onDeserialize and onSerialize functions to provide custom deserialization and serialization logic. The MyJsonType object is then serialized to a JSON object and deserialized back from the JSON object. Note that the example uses the CJsonObject class from the IO namespace for JSON object manipulation.

Constructor & Destructor Documentation

◆ IJsonType()

template<typename T = int, typename TData = IData<T>>
Devel::Serializing::IJsonType< T, TData >::IJsonType ( const CJsonFieldName  i_oFieldName)
inlineexplicitprotected

Constructs an IJsonType object with the specified JSON field name.

Parameters
i_oFieldNameThe name of the JSON field.

Member Function Documentation

◆ checkSerializeObject()

template<typename T = int, typename TData = IData<T>>
bool Devel::Serializing::IJsonType< T, TData >::checkSerializeObject ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict 
) const
inlineprotected

Checks if serialization of the object is allowed and handles null fields.

Parameters
i_oObjectThe JSON object being serialized to.
i_bIsStrictDetermines if strict serialization rules should be applied.
Returns
True if serialization should be skipped due to null field or empty name, false otherwise.

◆ doDeserialize()

template<typename T = int, typename TData = IData<T>>
bool Devel::Serializing::IJsonType< T, TData >::doDeserialize ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict = false 
)
inline

Deserializes the object from a JSON object.

Parameters
i_oObjectThe JSON object containing the serialized data.
i_bIsStrictDetermines if strict deserialization rules should be applied.
Returns
True if deserialization is successful, false otherwise.

◆ doSerialize()

template<typename T = int, typename TData = IData<T>>
bool Devel::Serializing::IJsonType< T, TData >::doSerialize ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict = false 
) const
inline

Serializes the object to a JSON object.

Parameters
i_oObjectThe JSON object to populate with serialized data.
i_bIsStrictDetermines if strict serialization rules should be applied.
Returns
True if serialization is successful, false otherwise.

◆ getObject()

template<typename T = int, typename TData = IData<T>>
IO::CJsonObject* Devel::Serializing::IJsonType< T, TData >::getObject ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict 
)
inlineprotected

Retrieves the JSON object for the specified JSON field.

Parameters
i_oObjectThe JSON object to search within.
i_bIsStrictDetermines if strict deserialization rules should be applied.
Returns
A pointer to the JSON object for the specified field, or nullptr if not found.

◆ isNull()

template<typename T = int, typename TData = IData<T>>
bool Devel::Serializing::IJsonType< T, TData >::isNull ( ) const
inlineprotected

Checks if the JSON field is null.

Returns
True if the JSON field is null, false otherwise.

◆ name()

template<typename T = int, typename TData = IData<T>>
std::string_view Devel::Serializing::IJsonType< T, TData >::name ( ) const
inlineprotected

Retrieves the name of the JSON field.

Returns
The name of the JSON field as a string view.

◆ onDeserialize() [1/2]

template<typename T = int, typename TData = IData<T>>
virtual bool Devel::Serializing::IJsonType< T, TData >::onDeserialize ( IO::CJsonObject i_oObject)
inlineprotectedvirtual

Called during deserialization to populate the object from a JSON object (non-strict mode).

Parameters
i_oObjectThe JSON object containing the serialized data.
Returns
True if deserialization is successful, false otherwise.

◆ onDeserialize() [2/2]

template<typename T = int, typename TData = IData<T>>
virtual bool Devel::Serializing::IJsonType< T, TData >::onDeserialize ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict 
)
inlineprotectedvirtual

Called during deserialization to populate the object from a JSON object.

Parameters
i_oObjectThe JSON object containing the serialized data.
i_bIsStrictDetermines if strict deserialization rules should be applied.
Returns
True if deserialization is successful, false otherwise.

◆ onSerialize() [1/2]

template<typename T = int, typename TData = IData<T>>
virtual bool Devel::Serializing::IJsonType< T, TData >::onSerialize ( IO::CJsonObject i_oObject) const
inlineprotectedvirtual

Called during deserialization to populate the object from a JSON object (non-strict mode).

Parameters
i_oObjectThe JSON object containing the serialized data.
Returns
True if deserialization is successful, false otherwise.

◆ onSerialize() [2/2]

template<typename T = int, typename TData = IData<T>>
virtual bool Devel::Serializing::IJsonType< T, TData >::onSerialize ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict 
) const
inlineprotectedvirtual

Called during serialization to convert the object to a JSON object.

Parameters
i_oObjectThe JSON object to populate with serialized data.
i_bIsStrictDetermines if strict serialization rules should be applied.
Returns
True if serialization is successful, false otherwise.

◆ operator=() [1/2]

template<typename T = int, typename TData = IData<T>>
template<typename TJsonFieldName , std::enable_if_t< std::is_same_v< CJsonFieldName, TJsonFieldName >, bool > = true>
IJsonType& Devel::Serializing::IJsonType< T, TData >::operator= ( const TJsonFieldName &  i_tValue)
inline

Assigns a JSON field name to the object.

Template Parameters
TJsonFieldNameThe type of the JSON field name (CJsonFieldName).
Parameters
i_tValueThe JSON field name to assign.
Returns
A reference to the assigned IJsonType object.

◆ operator=() [2/2]

template<typename T = int, typename TData = IData<T>>
template<typename TJsonFieldName , std::enable_if_t< std::is_same_v< CJsonFieldName, TJsonFieldName >, bool > = true>
IJsonType& Devel::Serializing::IJsonType< T, TData >::operator= ( TJsonFieldName &&  i_tValue)
inline

Assigns a JSON field name to the object (move assignment).

Template Parameters
TJsonFieldNameThe type of the JSON field name (CJsonFieldName).
Parameters
i_tValueThe JSON field name to assign.
Returns
A reference to the assigned IJsonType object.

◆ serialize()

template<typename T = int, typename TData = IData<T>>
bool Devel::Serializing::IJsonType< T, TData >::serialize ( IO::CJsonObject i_oObject,
const bool  i_bIsStrict = false 
) const
inlineprotected

Serializes the data to a JSON object.

This function serializes the data to a JSON object represented by the CJsonObject class. If the object already contains a field with the same name as this data object, the existing value is overwritten.

Parameters
i_oObjectThe JSON object to serialize to.
i_bIsStrictFlag indicating whether strict serialization should be performed. If set to true, only fields marked for serialization will be included in the output. If set to false, all fields will be included in the output.
Returns
true if serialization was successful, false otherwise.

◆ setNull()

template<typename T = int, typename TData = IData<T>>
void Devel::Serializing::IJsonType< T, TData >::setNull ( bool  i_bState)
inlineprotected

Sets the null state of the JSON field.

Parameters
i_bStateThe null state to set.

◆ size()

template<typename T = int, typename TData = IData<T>>
size_t Devel::Serializing::IJsonType< T, TData >::size ( ) const
inlineoverridevirtual

Retrieves the size of the object.

Returns
The size of the object in bytes.

Reimplemented from Devel::Serializing::IData< int >.


The documentation for this class was generated from the following file: