AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::json::ObjectMapper< T > Class Template Reference

JSON complex object mapper. More...

#include <mapper.hpp>

Inheritance diagram for anch::json::ObjectMapper< T >:
Collaboration diagram for anch::json::ObjectMapper< T >:

Public Member Functions

 ObjectMapper (const ObjectMapper &other)=delete
 ObjectMapper (ObjectMapper &&other)=delete
void serializeValue (const T &value, anch::json::WriterContext &context) const
bool deserializeValue (T &value, anch::json::ReaderContext &context) const
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & registerField (const std::string &key, P T::*value, C &&... constraints)
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & registerField (const std::string &key, std::function< P(const T &)> getter, const C &... constraints)
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & registerField (const std::string &key, std::function< void(T &, const P &)> setter, C &&... constraints)
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & registerField (const std::string &key, std::function< P(const T &)> getter, std::function< void(T &, const P &)> setter, C &&... constraints)
Public Member Functions inherited from anch::json::GenericMapper< ObjectMapper< T >, T >
void serialize (const T &value, anch::json::WriterContext &context) const
bool deserialize (T &value, anch::json::ReaderContext &context) const

Friends

class anch::json::Factory< T >

Additional Inherited Members

Protected Member Functions inherited from anch::json::GenericMapper< ObjectMapper< T >, T >
 GenericMapper ()
virtual ~GenericMapper ()

Detailed Description

template<typename T>
class anch::json::ObjectMapper< T >

JSON complex object mapper.

This is a generic template header implementation.
Type has to be a class or a structure described by an anch::Mapping specialization (anch-utils, mapping.hpp ): its registerObject function registers the fields on this mapper. Fields of std::optional , smart pointers, collections and maps are mapped with the mapper of their value type.
Field constraints ( anch::val::Required , value constraints like anch::val::MinLength , see anch-val, val/constraints.hpp ) can be given at registration. They are checked on deserialization ( deserialize_check_constraints option, enabled by default) and on serialization ( serialize_check_constraints option, disabled by default).
Errors raised while deserializing or serializing a field value have this field in their path ( MappingError::getPath ).

Template Parameters
Tthe object type
Author
Vincent Lachenal
Since
0.1

Constructor & Destructor Documentation

◆ ObjectMapper() [1/2]

template<typename T>
anch::json::ObjectMapper< T >::ObjectMapper ( const ObjectMapper< T > & other)
delete

Forbids ObjectMapper copy constructor

◆ ObjectMapper() [2/2]

template<typename T>
anch::json::ObjectMapper< T >::ObjectMapper ( ObjectMapper< T > && other)
delete

Forbids ObjectMapper move constructor

Member Function Documentation

◆ deserializeValue()

template<typename T>
bool anch::json::ObjectMapper< T >::deserializeValue ( T & value,
anch::json::ReaderContext & context ) const

Deserialize JSON

Parameters
valuethe value to write in
contextthe mapper context
Returns
false when null found, true otherwise

◆ registerField() [1/4]

template<typename T>
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & anch::json::ObjectMapper< T >::registerField ( const std::string & key,
P T::* value,
C &&... constraints )

Register mapping by field

Template Parameters
Pthe parameter type
Cthe constraints types
Parameters
keythe JSON key
valuethe JSON value in object
constraintsthe field constraints ( anch::val::required , anch::val::minLength , ...)
Returns
this

◆ registerField() [2/4]

template<typename T>
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & anch::json::ObjectMapper< T >::registerField ( const std::string & key,
std::function< P(const T &)> getter,
const C &... constraints )

Register mapping by field

Template Parameters
Pthe parameter type
Cthe constraints types
Parameters
keythe JSON key
getterthe JSON value getter function
constraintsthe field constraints, checked on serialization
Returns
this

◆ registerField() [3/4]

template<typename T>
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & anch::json::ObjectMapper< T >::registerField ( const std::string & key,
std::function< P(const T &)> getter,
std::function< void(T &, const P &)> setter,
C &&... constraints )

Register mapping by field

Template Parameters
Pthe parameter type
Cthe constraints types
Parameters
keythe JSON key
getterthe JSON value getter function
setterthe JSON value setter function
constraintsthe field constraints ( anch::val::required , anch::val::minLength , ...), checked on deserialization
Returns
this

◆ registerField() [4/4]

template<typename T>
template<typename P, typename... C>
requires (anch::val::FieldConstraint<C,P> && ...)
ObjectMapper< T > & anch::json::ObjectMapper< T >::registerField ( const std::string & key,
std::function< void(T &, const P &)> setter,
C &&... constraints )

Register mapping by field

Template Parameters
Pthe parameter type
Cthe constraints types
Parameters
keythe JSON key
setterthe JSON value setter function
constraintsthe field constraints ( anch::val::required , anch::val::minLength , ...)
Returns
this

◆ serializeValue()

template<typename T>
void anch::json::ObjectMapper< T >::serializeValue ( const T & value,
anch::json::WriterContext & context ) const

Serialize value

Parameters
valuethe reference attribute to serialize
contextthe writer context

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