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<
typename mapped_type>
258 collectionInternal(
member);
265 if (mValuePtr->type() == json_spirit::obj_type)
267 else if (mValuePtr->type() == json_spirit::array_type)
268 mValuePtr->get_array().push_back(val);
273 mPointerClass =
type;
278 if (mValuePtr->type() == json_spirit::obj_type)
280 else if (mValuePtr->type() == json_spirit::array_type)
294 property(name, const_cast<T&>(
member), comment, std::move(hint), flags);
298 void roproperty(
const char* name,
const std::string&
id,
const T&
member,
const char* comment,
305 property(name,
id, const_cast<T&>(
member), comment, std::move(hint), flags);
315 property(name, a, comment, std::move(hint), flags);
319 template<
typename Container>
320 void mapEntry(
int id,
const typename Container::value_type& p)
322 using type =
typename Container::mapped_type;
325 type& nonconstv =
const_cast<type&
>(p.second);
326 member(p.first.c_str(), nonconstv,
"");
329 serialization::reflectReadMapPair<JSONSerializer, Container>(*
this,
"item", id, p);
335 void collectionInternal(T&
member)
339 mFirstComplex =
false;
345 if (oldValue->type() == json_spirit::obj_type) {
350 else if (oldValue->type() == json_spirit::array_type) {
357 mValuePtr = oldValue;
365 std::string mPointerClass;
416 ACCEPT_STRING_MAP_AS_OBJECT = 0x01
422 mValue(&value), mStringMapAsObject(false), mInputFormat(
format)
426 template <
typename T>
430 mFirstComplex =
true;
431 Base::deserialize(MIRA_REFLECTOR_TOPLEVEL_NAME, value);
441 if (value->get_obj().count(name) == 0)
442 MIRA_THROW(XMemberNotFound,
"Missing member '" << name
443 <<
"' (" << getCurrentMemberFullID()
444 <<
") while deserializing object='" <<
json::write(*mValue));
445 return &value->get_obj().at(name);
450 if (value->get_array().size() <= index)
451 MIRA_THROW(XMemberNotFound,
"Missing item[" << index
452 <<
"] (" << getCurrentMemberFullID()
453 <<
") while deserializing collection='" <<
json::write(*mValue));
454 return &value->get_array()[index];
459 template <
typename T>
462 int vf = this->forcedDeserializeVersion();
463 if ((vf == 0) || (vf == 1))
464 return versionLegacy(expectedVersion);
466 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
467 return getVersion<T>(expectedVersion, v);
473 return versionLegacy(expectedVersion);
476 template <
typename T>
479 return this->version<T>(expectedVersion);
486 return getVersion<void>(expectedVersion,
"@version");
489 template <
typename T>
492 if (mValue->get_obj().count(versionElement) == 0)
495 if (version > expectedVersion) {
496 MIRA_LOG(
WARNING) <<
"Trying to deserialize JSON data of a newer version (" << (int)version <<
497 ") of type " << typeName<T>() <<
" into an older version (" << (int)expectedVersion <<
").";
507 if ( mValue->type() == json_spirit::obj_type )
509 const json::Value* item = getMember(mValue, mCurrentMemberID);
513 catch(std::exception& ex)
515 MIRA_THROW(XInvalidConfig,
"Error deserializing member '" << mCurrentMemberID
516 <<
"' (" << getCurrentMemberFullID()
518 <<
"' as " << typeName<T>() <<
": " << ex.what());
521 else if ( mValue->type() == json_spirit::array_type )
523 const json::Value* item = getItem(mValue, mIndex++);
527 catch(std::exception& ex)
529 MIRA_THROW(XInvalidConfig,
"Error deserializing item[" << mIndex-1
530 <<
"] (" << getCurrentMemberFullID()
532 <<
"' as " << typeName<T>() <<
": " << ex.what());
541 catch(std::exception& ex)
543 MIRA_THROW(XMemberNotFound,
"Error deserializing atomic (" << getCurrentMemberFullID()
544 <<
") with content='" <<
json::write(*mValue) <<
"' as " << typeName<T>() <<
": " << ex.what());
551 if ( mValue->type() == json_spirit::obj_type )
552 member = *getMember(mValue, mCurrentMemberID);
553 else if ( mValue->type() == json_spirit::array_type )
554 member = *getItem(mValue, mIndex++);
564 mFirstComplex =
false;
565 Base::object(member);
569 std::size_t oldIndex = mIndex;
570 if ( mValue->type() == json_spirit::obj_type )
572 mValue = getMember(mValue, mCurrentMemberID);
574 else if ( mValue->type() == json_spirit::array_type )
576 mValue = getItem(mValue, oldIndex);
580 Base::object(member);
589 collectionInternal(member);
592 template<
typename mapped_type>
595 if (!(mInputFormat & ACCEPT_STRING_MAP_AS_OBJECT)) {
596 collectionInternal(member);
600 if (mStringMapAsObject) {
601 json::Object::const_iterator oldCurrentStringMapMember = mCurrentStringMapMember;
603 mCurrentStringMapMember = oldCurrentStringMapMember;
607 assert(mStringMapAsObject ==
false);
611 std::size_t oldIndex = mIndex;
612 std::string memberID = mCurrentMemberID;
613 bool firstComplex = mFirstComplex;
620 collectionInternal(member);
622 catch (XInvalidConfig& ex) {
623 MIRA_LOG(
DEBUG) <<
"Deserialize map<string, T> as array failed, try as object:" << std::endl;
626 mObjectNameToInstance = trackingNames;
627 mObjects = trackingInstances;
631 mCurrentMemberID = memberID;
632 mFirstComplex = firstComplex;
636 mStringMapAsObject =
true;
642 mStringMapAsObject =
false;
643 MIRA_RETHROW(ex,
"While trying to deserialize map<string, T> as object.");
646 mStringMapAsObject =
false;
650 mStringMapAsObject =
false;
658 array = &mValue->get_array();
660 catch(std::exception& ex)
662 MIRA_THROW(XInvalidConfig,
"Error deserializing collection (" << getCurrentMemberFullID()
663 <<
") from json::Value='" <<
json::write(*mValue) <<
"': " << ex.what());
676 if ( mValue->type() == json_spirit::obj_type )
677 value = getMember(mValue, mCurrentMemberID);
678 else if ( mValue->type() == json_spirit::array_type )
679 value = getItem(mValue, mIndex);
682 std::size_t oldIndex = mIndex;
685 if(value->is_null()) {
690 assert(value->type() == json_spirit::obj_type);
691 auto it = value->get_obj().find(
"@ref");
692 if(it!=value->get_obj().end()) {
694 pointer = resolveReference<T>(it->second.get_value<std::string>());
697 Base::pointer(pointer);
701 if (mValue->type() == json_spirit::array_type)
712 if ( mValue->type() == json_spirit::obj_type )
716 value = getMember(mValue, mCurrentMemberID);
718 catch(XMemberNotFound& ex)
720 MIRA_RETHROW(ex,
"Error getting pointer class type of member '" 721 << mCurrentMemberID <<
"' (" << getCurrentMemberFullID()
722 <<
") from object='" <<
json::write(*mValue)<<
"'");
725 else if ( mValue->type() == json_spirit::array_type )
729 value = getItem(mValue, mIndex);
731 catch(XMemberNotFound& ex)
733 MIRA_RETHROW(ex,
"Error getting pointer class type of item[" << mIndex
734 <<
"] (" << getCurrentMemberFullID()
735 <<
") from collection='" <<
json::write(*mValue) <<
"'");
739 if(value->type() == json_spirit::obj_type)
741 auto it = value->get_obj().find(
"@class");
742 if(it!=value->get_obj().end())
743 return it->second.get_value<std::string>();
745 return "Unknown Class";
753 std::string oldID = mCurrentMemberID;
755 mCurrentMemberID = meta.
id;
758 Base::invokeMemberOverwrite(member, meta);
763 mCurrentMemberID = oldID;
767 mCurrentMemberID = oldID;
774 if (mStringMapAsObject) {
777 mCurrentStringMapMember = mValue->get_obj().begin();
778 return (uint32)o->size();
780 catch(std::exception& ex)
782 MIRA_THROW(XInvalidConfig,
"Error counting object elements from json::Value='" 787 return (uint32)a->size();
791 template<
typename Container>
792 void mapEntry(
int id, Container& c,
typename Container::iterator& hint)
794 typedef typename Container::value_type value_type;
795 typedef typename Container::mapped_type mapped_type;
797 if (mStringMapAsObject) {
798 std::string key = mCurrentStringMapMember->first;
799 hint = c.insert(hint, value_type(key, mapped_type()));
800 property(key.c_str(), hint->second,
"");
801 ++mCurrentStringMapMember;
809 void collectionInternal(T& member)
813 mFirstComplex =
false;
814 Base::object(member);
818 std::size_t oldIndex = mIndex;
819 if ( mValue->type() == json_spirit::obj_type )
821 mValue = getMember(mValue, mCurrentMemberID);
824 else if ( mValue->type() == json_spirit::array_type )
826 mValue = getItem(mValue, oldIndex);
830 Base::object(member);
840 std::string mCurrentMemberID;
842 bool mStringMapAsObject;
843 json::Object::const_iterator mCurrentStringMapMember;
845 InputFormat mInputFormat;
848 namespace serialization {
859 template<
typename Collection>
873 template<
typename Collection>
879 ioCount = (uint32)a->size();
883 template<
typename mapped_type>
892 template<
typename mapped_type>
910 template<
typename mapped_type>
921 for(uint32
id=0;
id<count; ++id)
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
void deserialize(T &value)
Definition: JSONSerializer.h:427
Base::VersionType VersionType
Definition: JSONSerializer.h:457
void object(T &member)
Is called for each complex object.
Definition: RecursiveMemberReflector.h:301
void pointer(T *&pointer)
Definition: JSONSerializer.h:669
InputFormat
Flags for JSON input format.
Definition: JSONSerializer.h:407
void pointerNull()
Definition: JSONSerializer.h:276
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
void roproperty(const char *name, const T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:287
void mapEntry(int id, Container &c, typename Container::iterator &hint)
Definition: JSONSerializer.h:792
Contains the Serializer template, a base class for all serializers.
static void reflect(JSONDeserializer &r, Container &c, uint32 count)
Definition: JSONSerializer.h:916
void roproperty(const char *name, Getter< T > getter, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:309
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:190
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
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:862
const std::string & getHumanReadableFullID(int objectID) const
Returns the full human readable object id / name for the given internal objectID. ...
Definition: Serializer.h:456
void atomic(json::Value &member)
Definition: JSONSerializer.h:549
std::map< std::string, mapped_type > Container
Definition: JSONSerializer.h:895
Is a special reflector that is used for serialization.
Definition: Serializer.h:126
std::map< std::string, mapped_type > Container
Definition: JSONSerializer.h:913
#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:148
void collection(T &member)
Definition: JSONSerializer.h:587
VersionType version(VersionType version, const T *caller=NULL)
Definition: JSONSerializer.h:138
std::vector< void *> ObjectsVector
Definition: Deserializer.h:302
void pointerWithClassType(const std::string &type)
Definition: JSONSerializer.h:271
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:964
static void reflect(JSONSerializer &r, Container &c)
Definition: JSONSerializer.h:898
std::map< std::string, void *> ObjectNameToInstanceMap
Definition: Deserializer.h:298
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:470
Container::iterator iterator
Definition: JSONSerializer.h:914
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
Definition: JSONSerializer.h:751
uint8 VersionType
Definition: ReflectorInterface.h:72
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
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:439
VersionType version(VersionType expectedVersion, const T *object=NULL)
Definition: JSONSerializer.h:460
const ReflectMemberMeta & getCurrentMemberMeta() const
Returns the meta-information of the current member that is reflected.
Definition: RecursiveMemberReflector.h:459
uint32 getStringMapElementCount()
Definition: JSONSerializer.h:772
MIRA_ENUM_TO_FLAGS_INCLASS(OutputFormat) JSONSerializer(bool readOnly
Construct a serializer.
MIRA_BASE_EXPORT 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 object(T &member)
Definition: JSONSerializer.h:211
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
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:298
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.
void atomic(T &member)
Definition: JSONSerializer.h:505
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:876
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:183
void collection(std::map< std::string, mapped_type > &member)
Definition: JSONSerializer.h:253
void collection(std::map< std::string, mapped_type > &member)
Definition: JSONSerializer.h:593
void mapEntry(int id, const typename Container::value_type &p)
Definition: JSONSerializer.h:320
Base class for exceptions.
Definition: Exception.h:199
Base::VersionType VersionType
Definition: Serializer.h:154
MIRA_ENUM_TO_FLAGS_INCLASS(InputFormat) JSONDeserializer(const json
Definition: JSONSerializer.h:419
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:176
typename ReflectorInterface< JSONSerializer >::VersionType VersionType
Definition: AbstractReflector.h:165
void deserialize(json::Value &value)
Definition: JSONSerializer.h:434
VersionType version(VersionType expectedVersion, AcceptDesiredVersion, const T *caller=NULL)
Definition: JSONSerializer.h:477
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:560
Container::value_type value_type
Definition: JSONSerializer.h:896
const json::Value * getItem(const json::Value *value, std::size_t index)
Definition: JSONSerializer.h:448
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:886
static constexpr auto MIRA_REFLECTOR_TOPLEVEL_NAME
Use this when a reflector needs to choose a name for serializing an object.
Definition: RecursiveMemberReflector.h:249
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:653
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
Helpers for serialization and deserialization of STL containers.
Definition: StlCollections.h:391
std::string pointerClassType()
Definition: JSONSerializer.h:705
static void reflect(JSONSerializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:862
void pointerReference(int referencedObjectID)
Definition: JSONSerializer.h:261
OutputFormat format
Definition: JSONSerializer.h:115