47 #ifndef _MIRA_JSONSERIALIZER_H_ 48 #define _MIRA_JSONSERIALIZER_H_ 116 : mReadOnly(readOnly), mOutputFormat(
format)
123 mFirstComplex =
true;
140 return this->version<T>(
version,
false);
152 return this->version<T>(
version,
true);
159 version = this->
template queryDesiredClassVersion<T>(
version, acceptDesiredVersion);
162 if ((vf == 0) || (vf == 1))
165 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
181 if (mValuePtr->type() == json_spirit::obj_type) {
185 else if (mValuePtr->type() == json_spirit::array_type) {
197 if (mValuePtr->type() == json_spirit::obj_type) {
201 else if (mValuePtr->type() == json_spirit::array_type) {
202 mValuePtr->get_array().push_back(
member);
216 mFirstComplex =
false;
217 if (!mPointerClass.empty()) {
218 mValuePtr->get_obj()[
"@class"] =
json::Value(mPointerClass);
219 mPointerClass.clear();
226 if (oldValue->type() == json_spirit::obj_type) {
231 else if (oldValue->type() == json_spirit::array_type) {
237 if (!mPointerClass.empty()) {
238 mValuePtr->get_obj()[
"@class"] =
json::Value(mPointerClass);
239 mPointerClass.clear();
242 mValuePtr = oldValue;
249 collectionInternal(
member);
252 template<
template<
typename U,
typename V>
class MapType,
typename T>
253 typename std::enable_if_t<std::is_same_v<T, typename MapType<std::string, T>::mapped_type>,
259 collectionInternal(
member);
266 if (mValuePtr->type() == json_spirit::obj_type)
268 else if (mValuePtr->type() == json_spirit::array_type)
269 mValuePtr->get_array().push_back(val);
274 mPointerClass =
type;
279 if (mValuePtr->type() == json_spirit::obj_type)
281 else if (mValuePtr->type() == json_spirit::array_type)
295 property(name, const_cast<T&>(
member), comment, std::move(hint), flags);
299 void roproperty(
const char* name,
const std::string&
id,
const T&
member,
const char* comment,
306 property(name,
id, const_cast<T&>(
member), comment, std::move(hint), flags);
316 property(name, a, comment, std::move(hint), flags);
320 template<
typename Container>
321 void mapEntry(
int id,
const typename Container::value_type& p)
323 using type =
typename Container::mapped_type;
326 type& nonconstv =
const_cast<type&
>(p.second);
327 member(p.first.c_str(), nonconstv,
"");
330 serialization::reflectReadMapPair<JSONSerializer, Container>(*
this,
"item", id, p);
336 void collectionInternal(T&
member)
340 mFirstComplex =
false;
346 if (oldValue->type() == json_spirit::obj_type) {
351 else if (oldValue->type() == json_spirit::array_type) {
358 mValuePtr = oldValue;
366 std::string mPointerClass;
423 mValue(&value), mStringMapAsObject(false), mInputFormat(
format)
427 template <
typename T>
431 mFirstComplex =
true;
442 if (value->get_obj().count(name) == 0)
443 MIRA_THROW(XMemberNotFound,
"Missing member '" << name
445 <<
") while deserializing object='" <<
json::write(*mValue));
446 return &value->get_obj().at(name);
451 if (value->get_array().size() <= index)
452 MIRA_THROW(XMemberNotFound,
"Missing item[" << index
454 <<
") while deserializing collection='" <<
json::write(*mValue));
455 return &value->get_array()[index];
460 template <
typename T>
464 if ((vf == 0) || (vf == 1))
465 return versionLegacy(expectedVersion);
467 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
468 return getVersion<T>(expectedVersion, v);
474 return versionLegacy(expectedVersion);
477 template <
typename T>
480 return this->version<T>(expectedVersion);
487 return getVersion<void>(expectedVersion,
"@version");
490 template <
typename T>
493 if (mValue->get_obj().count(versionElement) == 0)
496 if (
version > expectedVersion) {
498 ") of type " << typeName<T>() <<
" into an older version (" << (int)expectedVersion <<
").";
508 if ( mValue->type() == json_spirit::obj_type )
514 catch(std::exception& ex)
516 MIRA_THROW(XInvalidConfig,
"Error deserializing member '" << mCurrentMemberID
519 <<
"' as " << typeName<T>() <<
": " << ex.what());
522 else if ( mValue->type() == json_spirit::array_type )
528 catch(std::exception& ex)
530 MIRA_THROW(XInvalidConfig,
"Error deserializing item[" << mIndex-1
533 <<
"' as " << typeName<T>() <<
": " << ex.what());
542 catch(std::exception& ex)
545 <<
") with content='" <<
json::write(*mValue) <<
"' as " << typeName<T>() <<
": " << ex.what());
552 if ( mValue->type() == json_spirit::obj_type )
554 else if ( mValue->type() == json_spirit::array_type )
565 mFirstComplex =
false;
570 std::size_t oldIndex = mIndex;
571 if ( mValue->type() == json_spirit::obj_type )
573 mValue =
getMember(mValue, mCurrentMemberID);
575 else if ( mValue->type() == json_spirit::array_type )
577 mValue =
getItem(mValue, oldIndex);
590 collectionInternal(
member);
593 template<
template<
typename U,
typename V>
class MapType,
typename T>
594 typename std::enable_if_t<std::is_same_v<T, typename MapType<std::string, T>::mapped_type>,
598 collectionInternal(
member);
602 if (mStringMapAsObject) {
603 json::Object::const_iterator oldCurrentStringMapMember = mCurrentStringMapMember;
605 mCurrentStringMapMember = oldCurrentStringMapMember;
609 assert(mStringMapAsObject ==
false);
613 std::size_t oldIndex = mIndex;
614 std::string memberID = mCurrentMemberID;
615 bool firstComplex = mFirstComplex;
622 collectionInternal(
member);
624 catch (XInvalidConfig& ex) {
625 MIRA_LOG(
DEBUG) <<
"Deserialize map<string, T> as array failed, try as object:" << std::endl;
633 mCurrentMemberID = memberID;
634 mFirstComplex = firstComplex;
638 mStringMapAsObject =
true;
644 mStringMapAsObject =
false;
645 MIRA_RETHROW(ex,
"While trying to deserialize MapType<string, T> as object.");
648 mStringMapAsObject =
false;
652 mStringMapAsObject =
false;
660 array = &mValue->get_array();
662 catch(std::exception& ex)
665 <<
") from json::Value='" <<
json::write(*mValue) <<
"': " << ex.what());
678 if ( mValue->type() == json_spirit::obj_type )
679 value =
getMember(mValue, mCurrentMemberID);
680 else if ( mValue->type() == json_spirit::array_type )
681 value =
getItem(mValue, mIndex);
684 std::size_t oldIndex = mIndex;
687 if(value->is_null()) {
692 assert(value->type() == json_spirit::obj_type);
693 auto it = value->get_obj().find(
"@ref");
694 if(it!=value->get_obj().end()) {
696 pointer = resolveReference<T>(it->second.get_value<std::string>());
703 if (mValue->type() == json_spirit::array_type)
714 if ( mValue->type() == json_spirit::obj_type )
718 value =
getMember(mValue, mCurrentMemberID);
720 catch(XMemberNotFound& ex)
722 MIRA_RETHROW(ex,
"Error getting pointer class type of member '" 724 <<
") from object='" <<
json::write(*mValue)<<
"'");
727 else if ( mValue->type() == json_spirit::array_type )
731 value =
getItem(mValue, mIndex);
733 catch(XMemberNotFound& ex)
735 MIRA_RETHROW(ex,
"Error getting pointer class type of item[" << mIndex
737 <<
") from collection='" <<
json::write(*mValue) <<
"'");
741 if(value->type() == json_spirit::obj_type)
743 auto it = value->get_obj().find(
"@class");
744 if(it!=value->get_obj().end())
745 return it->second.get_value<std::string>();
747 return "Unknown Class";
755 std::string oldID = mCurrentMemberID;
757 mCurrentMemberID = meta.
id;
765 mCurrentMemberID = oldID;
769 mCurrentMemberID = oldID;
776 if (mStringMapAsObject) {
779 mCurrentStringMapMember = mValue->get_obj().begin();
782 catch(std::exception& ex)
784 MIRA_THROW(XInvalidConfig,
"Error counting object elements from json::Value='" 793 template<
typename Container>
794 void mapEntry(
int id, Container& c,
typename Container::iterator& hint)
796 typedef typename Container::value_type value_type;
797 typedef typename Container::mapped_type mapped_type;
799 if (mStringMapAsObject) {
800 std::string key = mCurrentStringMapMember->first;
801 hint = c.insert(hint, value_type(key, mapped_type()));
802 property(key.c_str(), hint->second,
"");
803 ++mCurrentStringMapMember;
811 void collectionInternal(T&
member)
815 mFirstComplex =
false;
820 std::size_t oldIndex = mIndex;
821 if ( mValue->type() == json_spirit::obj_type )
823 mValue =
getMember(mValue, mCurrentMemberID);
826 else if ( mValue->type() == json_spirit::array_type )
828 mValue =
getItem(mValue, oldIndex);
842 std::string mCurrentMemberID;
844 bool mStringMapAsObject;
845 json::Object::const_iterator mCurrentStringMapMember;
850 namespace serialization {
861 template<
typename Collection>
876 template<
typename Collection>
882 ioCount = (
uint32)a->size();
886 namespace SpecializeForMapped_Type {
890 template<
typename Collection,
typename T,
typename HasMappedType = std::true_type>
896 ioCount = (
uint32)a->size();
903 template<
typename Collection,
typename T>
905 typename
std::is_same<T, typename Collection::mapped_type>
::type>
922 template<
template<
typename U,
typename V>
class MapType,
typename T>
933 template<
template<
typename U,
typename V>
class MapType,
typename mapped_type>
951 template<
template<
typename U,
typename V>
class MapType,
typename mapped_type>
962 for(
uint32 id=0;
id<count; ++id)
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
void write(const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
Writes a json::Value into a given stream using the JSON format.
void deserialize(T &value)
Definition: JSONSerializer.h:428
Base::VersionType VersionType
Definition: JSONSerializer.h:458
void object(T &member)
Is called for each complex object.
Definition: RecursiveMemberReflector.h:321
void pointer(T *&pointer)
Definition: JSONSerializer.h:671
InputFormat
Flags for JSON input format.
Definition: JSONSerializer.h:408
void pointerNull()
Definition: JSONSerializer.h:277
TEigenFormat< Derived > format(Eigen::MatrixBase< Derived > &matrix, Eigen::IOFormat format=EigenFormat::matlab())
Function for formatting an Eigen matrix using a special format.
Definition: EigenFormat.h:522
static void reflect(JSONDeserializer &r, Container &c, uint32 count)
Definition: JSONSerializer.h:957
void roproperty(const char *name, const T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:288
void mapEntry(int id, Container &c, typename Container::iterator &hint)
Definition: JSONSerializer.h:794
Contains the Serializer template, a base class for all serializers.
void pointer(T *&pointer)
Definition: Deserializer.h:157
void deserialize(const std::string &name, T &value)
Deserializes the specified object value with the given name.
Definition: Deserializer.h:133
void roproperty(const char *name, Getter< T > getter, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:310
void reflectWriteMapPair(Derived &r, const char *itemName, const char *keyName, uint32 id, Container &c, typename Container::iterator &ioHint)
Definition: StlCollections.h:324
json_spirit::mArray Array
A representation of an array (vector) in JSON.
Definition: JSON.h:188
std::enable_if_t< std::is_same_v< T, typename MapType< std::string, T >::mapped_type >, void > collection(MapType< std::string, T > &member)
Definition: JSONSerializer.h:595
typename ReflectorInterface< JSONSerializer >::AcceptDesiredVersion AcceptDesiredVersion
Definition: AbstractReflector.h:195
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
const std::string & getCurrentMemberFullID() const
Returns the full human readable object id / name of the current member being reflected.
Definition: RecursiveMemberReflector.h:490
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:883
const std::string & getHumanReadableFullID(int objectID) const
Returns the full human readable object id / name for the given internal objectID. ...
Definition: Serializer.h:465
void atomic(json::Value &member)
Definition: JSONSerializer.h:550
static int forcedDeserializeVersion()
Returns either the version number from value of environment variable 'MIRA_FORCE_DESERIALIZE_VERSION'...
Definition: Deserializer.h:118
Is a special reflector that is used for serialization.
Definition: Serializer.h:126
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:529
#define MIRA_RETHROW(ex, msg)
Macro for rethrowing an exception with file and line information and for adding additional informatio...
Definition: Exception.h:144
void collection(T &member)
Definition: JSONSerializer.h:588
VersionType version(VersionType version, const T *caller=NULL)
Definition: JSONSerializer.h:138
std::vector< void *> ObjectsVector
Definition: Deserializer.h:311
void pointerWithClassType(const std::string &type)
Definition: JSONSerializer.h:272
Definition: StlCollections.h:339
void property(const char *name, T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:985
std::map< std::string, void *> ObjectNameToInstanceMap
Definition: Deserializer.h:307
void collection(T &member)
Definition: JSONSerializer.h:247
void serialize(const std::string &name, const T &value, const std::string &comment="")
Serializes the specified object value under the given name.
Definition: Serializer.h:204
MIRA_DEPRECATED("Please call as version<MyType>(v) or version(v, this)", VersionType version(VersionType expectedVersion))
Definition: JSONSerializer.h:471
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
Definition: JSONSerializer.h:753
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
A property hint gives optional instructions to the property editor, i.e.
Definition: PropertyHint.h:82
const json::Value * getMember(const json::Value *value, const std::string &name)
Definition: JSONSerializer.h:440
VersionType version(VersionType expectedVersion, const T *object=NULL)
Definition: JSONSerializer.h:461
MapType< std::string, mapped_type > Container
Definition: JSONSerializer.h:954
const ReflectMemberMeta & getCurrentMemberMeta() const
Returns the meta-information of the current member that is reflected.
Definition: RecursiveMemberReflector.h:479
Container::value_type value_type
Definition: JSONSerializer.h:937
uint32 getStringMapElementCount()
Definition: JSONSerializer.h:774
MIRA_ENUM_TO_FLAGS_INCLASS(OutputFormat) JSONSerializer(bool readOnly
Construct a serializer.
void object(T &member)
Definition: JSONSerializer.h:211
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:401
void roproperty(const char *name, const std::string &id, const T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:299
ObjectNameToInstanceMap mObjectNameToInstance
maps full id names to instance pointers
Definition: Deserializer.h:309
VersionType version(VersionType version, AcceptDesiredVersion, const T *caller=NULL)
Definition: JSONSerializer.h:150
No flags.
Definition: ReflectControlFlags.h:65
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Contains base class for all deserializers.
std::enable_if_t< std::is_same_v< T, typename MapType< std::string, T >::mapped_type >, void > collection(MapType< std::string, T > &member)
Definition: JSONSerializer.h:254
ObjectsVector mObjects
maps the ids to the instance pointers
Definition: Deserializer.h:313
void atomic(T &member)
Definition: JSONSerializer.h:506
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:879
Getter< T > getter(T(*f)())
Creates a Getter for global or static class methods returning the result by value.
Definition: GetterSetter.h:136
void atomic(json::Value &member)
Definition: JSONSerializer.h:194
json_spirit::mObject Object
A representation of an object (class, struct) in JSON.
Definition: JSON.h:181
void mapEntry(int id, const typename Container::value_type &p)
Definition: JSONSerializer.h:321
Base class for exceptions.
Definition: Exception.h:195
MapType< std::string, mapped_type > Container
Definition: JSONSerializer.h:936
Base::VersionType VersionType
Definition: Serializer.h:154
MIRA_ENUM_TO_FLAGS_INCLASS(InputFormat) JSONDeserializer(const json
Definition: JSONSerializer.h:420
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:174
typename ReflectorInterface< JSONSerializer >::VersionType VersionType
Definition: AbstractReflector.h:165
void deserialize(json::Value &value)
Definition: JSONSerializer.h:435
Accept associative containers with string keys as JSON object (keys = member names), in ADDITION to the standard array format (i.e.
Definition: JSONSerializer.h:417
VersionType version(VersionType expectedVersion, AcceptDesiredVersion, const T *caller=NULL)
Definition: JSONSerializer.h:478
static int forcedSerializeVersion()
Returns either the version number from value of environment variable 'MIRA_FORCE_SERIALIZE_VERSION', or -1 (= do not force a version).
Definition: Serializer.h:145
MIRA_DEPRECATED("Please call as version<MyType>(v) or version(v, this)", VersionType version(VersionType version))
Definition: JSONSerializer.h:143
Can be specialized for a concrete derived RecursiveMemberReflector to reflect the size of collections...
Definition: ReflectCollection.h:69
Holds a boost::function object to a special getter function that must meet the signature "T method()"...
Definition: GetterSetter.h:87
Definition: LoggingCore.h:76
void object(T &member)
Definition: JSONSerializer.h:561
Container::iterator iterator
Definition: JSONSerializer.h:955
const json::Value * getItem(const json::Value *value, std::size_t index)
Definition: JSONSerializer.h:449
static constexpr auto MIRA_REFLECTOR_TOPLEVEL_NAME
Use this when a reflector needs to choose a name for serializing an object.
Definition: RecursiveMemberReflector.h:269
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
The actual invokeMember implementation that is called from invokeMember().
Definition: RecursiveMemberReflector.h:686
Default format.
Definition: JSONSerializer.h:102
ReflectCtrlFlags
Control Flags that can modify the behavior of certain reflectors.
Definition: ReflectControlFlags.h:63
"Null-Setter" tag-class where the AccessorSetterPart does nothing.
Definition: Accessor.h:198
OutputFormat
Flags for JSON output format.
Definition: JSONSerializer.h:99
Definition: LoggingCore.h:78
const json::Array * getCollection()
Definition: JSONSerializer.h:655
Default format.
Definition: JSONSerializer.h:411
Accessor< Getter, Setter > makeAccessor(const Getter &getter, const Setter &setter)
Helper method that creates an accessor from a different combination of either direct access to a vari...
Definition: Accessor.h:300
Serialize associative containers with string keys as JSON object (keys = member names) ...
Definition: JSONSerializer.h:105
void atomic(T &member)
Definition: JSONSerializer.h:178
static void reflect(JSONSerializer &r, Container &c)
Definition: JSONSerializer.h:939
Helpers for serialization and deserialization of STL containers.
Definition: StlCollections.h:391
std::string pointerClassType()
Definition: JSONSerializer.h:707
static void reflect(JSONSerializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:864
void pointerReference(int referencedObjectID)
Definition: JSONSerializer.h:262
Is a special reflector that is used for deserialization.
Definition: Deserializer.h:99
OutputFormat format
Definition: JSONSerializer.h:115