Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
caffa::JsonSerializer Class Reference

#include <cafJsonSerializer.h>

Collaboration diagram for caffa::JsonSerializer:
Collaboration graph
[legend]

Public Types

enum  SerializationType { DATA_FULL, DATA_SKELETON, SCHEMA, PATH }
 
using FieldSelector = std::function< bool(const FieldHandle *)>
 

Public Member Functions

 JsonSerializer (ObjectFactory *objectFactory=nullptr)
 
template<DerivesFromObjectHandle ObjectType>
std::shared_ptr< ObjectType > cloneObject (const ObjectType *object) const
 
JsonSerializersetFieldSelector (FieldSelector fieldSelector)
 
JsonSerializersetSerializationType (SerializationType type)
 
JsonSerializersetSerializeUuids (bool serializeUuids)
 
ObjectFactoryobjectFactory () const
 
FieldSelector fieldSelector () const
 
SerializationType serializationType () const
 
bool serializeUuids () const
 
JsonSerializersetClient (bool client)
 
bool isClient () const
 
std::string readUUIDFromObjectString (const std::string &string) const
 
void readObjectFromString (ObjectHandle *object, const std::string &string) const
 
std::string writeObjectToString (const ObjectHandle *object) const
 
std::shared_ptr< ObjectHandlecopyBySerialization (const ObjectHandle *object) const
 
std::shared_ptr< ObjectHandlecopyAndCastBySerialization (const ObjectHandle *object, const std::string &destinationClassKeyword) const
 
std::shared_ptr< ObjectHandlecreateObjectFromString (const std::string &string) const
 
void readStream (ObjectHandle *object, std::istream &stream) const
 
void writeStream (const ObjectHandle *object, std::ostream &stream) const
 
void readObjectFromJson (ObjectHandle *object, const nlohmann::json &jsonObject) const
 
void writeObjectToJson (const ObjectHandle *object, nlohmann::json &jsonObject) const
 

Static Public Member Functions

static std::string serializationTypeLabel (SerializationType type)
 

Protected Attributes

bool m_client
 
ObjectFactorym_objectFactory
 
FieldSelector m_fieldSelector
 
SerializationType m_serializationType
 
bool m_serializeUuids
 

Detailed Description

Implementation of Serializer for JSON serialization.

Constructor & Destructor Documentation

◆ JsonSerializer()

JsonSerializer::JsonSerializer ( ObjectFactory objectFactory = nullptr)

Constructor

Parameters
objectFactoryThe factory used when creating new objects. Not relevant when writing.

Member Function Documentation

◆ cloneObject()

template<DerivesFromObjectHandle ObjectType>
std::shared_ptr<ObjectType> caffa::JsonSerializer::cloneObject ( const ObjectType *  object) const
inline

Clone the object by serializing to and from text string

Parameters
objectThe object to copy
destinationClassKeywordThe class of the object to create.
Returns
unique ptr containing a new copy

◆ copyAndCastBySerialization()

std::shared_ptr< ObjectHandle > JsonSerializer::copyAndCastBySerialization ( const ObjectHandle object,
const std::string &  destinationClassKeyword 
) const

Copy the object by serializing to text string but cast to a different class keyword. Note, it is still returned as a base class pointer.

Parameters
objectThe object to copy
destinationClassKeywordThe class of the object to create.
Returns
unique ptr containing a new copy

◆ copyBySerialization()

std::shared_ptr< ObjectHandle > JsonSerializer::copyBySerialization ( const ObjectHandle object) const

Copy the object by serializing to text string and reading in again

Parameters
objectThe object to copy
Returns
unique ptr containing a new copy

◆ createObjectFromString()

std::shared_ptr< ObjectHandle > JsonSerializer::createObjectFromString ( const std::string &  string) const

Create a new object from a JSON text string

Parameters
stringThe JSON text string
Returns
unique ptr to new object

◆ fieldSelector()

JsonSerializer::FieldSelector JsonSerializer::fieldSelector ( ) const

Get the field selector

Returns
field selector

◆ objectFactory()

ObjectFactory * JsonSerializer::objectFactory ( ) const

Get the object factory

Returns
object factory

◆ readObjectFromString()

void JsonSerializer::readObjectFromString ( ObjectHandle object,
const std::string &  string 
) const

Convenience method to read this particular object (with children) from a json string

Parameters
objectObjectHandle to read in to.
stringThe JSON text string containing the object

◆ readStream()

void JsonSerializer::readStream ( ObjectHandle object,
std::istream &  stream 
) const

Read object from an input stream

Parameters
objectPointer to object to read into
streamThe input stream

◆ readUUIDFromObjectString()

std::string JsonSerializer::readUUIDFromObjectString ( const std::string &  string) const

Convenience method for reading the class keyword and uuid from a json string. This is used to extract the necessary information to find the object in the object hierarchy.

Parameters
stringThe JSON text string containing the object
Returns
pair of keyword and uuid in that order.

◆ serializationType()

JsonSerializer::SerializationType JsonSerializer::serializationType ( ) const

Check which type of serialization we're doing

Returns
The type of serialization to do

◆ serializeUuids()

bool JsonSerializer::serializeUuids ( ) const

Check if we're meant to serialize UUIDs. UUIDs are used for dynamic connection to runtime objects, not for writing to file. Only makes a difference when serializing data.

Returns
true if we should write the UUIDs

◆ setFieldSelector()

JsonSerializer & JsonSerializer::setFieldSelector ( FieldSelector  fieldSelector)

Set Field Selector Since it returns a reference it can be used like: Serializer(objectFactory).setFieldSelector(functor);

Parameters
fieldSelector
Returns
cafSerializer& reference to this

◆ setSerializationType()

JsonSerializer & JsonSerializer::setSerializationType ( SerializationType  type)

Set what to serialize (data, schema, etc) Since it returns a reference it can be used like: Serializer(objectFactory).setSerializationTypes(...);

Parameters
serializationType
Returns
cafSerializer& reference to this

◆ setSerializeUuids()

JsonSerializer & JsonSerializer::setSerializeUuids ( bool  serializeUuids)

Set whether to serialize UUIDs. Only makes a difference when serializing data Since it returns a reference, it can be used like: Serializer(objectFactory).setSerializeUuids(false);

Parameters
serializeUuids
Returns
cafSerializer& reference to this

◆ writeObjectToString()

std::string JsonSerializer::writeObjectToString ( const ObjectHandle object) const

Write an object to JSON text string

Parameters
objectThe object handle to write to string.
Returns
A JSON text string

◆ writeStream()

void JsonSerializer::writeStream ( const ObjectHandle object,
std::ostream &  stream 
) const

Write object to output stream

Parameters
objectPointer to object to write
streamThe output stream

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