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,
"Member '" << name <<
"' is missing");
443 return &value->get_obj().at(name);
448 if (value->get_array().size() <= index)
449 MIRA_THROW(XMemberNotFound,
"Item[" << index <<
"] is missing");
450 return &value->get_array()[index];
455 template <
typename T>
458 int vf = this->forcedDeserializeVersion();
459 if ((vf == 0) || (vf == 1))
460 return versionLegacy(expectedVersion);
462 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
463 return getVersion<T>(expectedVersion, v);
469 return versionLegacy(expectedVersion);
472 template <
typename T>
475 return this->version<T>(expectedVersion);
482 return getVersion<void>(expectedVersion,
"@version");
485 template <
typename T>
488 if (mValue->get_obj().count(versionElement) == 0)
491 if (version > expectedVersion) {
492 MIRA_LOG(
WARNING) <<
"Trying to deserialize JSON data of a newer version (" << (int)version <<
493 ") of type " << typeName<T>() <<
" into an older version (" << (int)expectedVersion <<
").";
503 if ( mValue->type() == json_spirit::obj_type )
507 member = json::reverse_cast<T>(getMember(mValue, mCurrentMemberID)->get_value<
typename json::TypeTrait<T>::type >());
509 catch(std::exception& ex)
511 MIRA_THROW(XMemberNotFound,
"Error deserializing member '" << mCurrentMemberID
512 <<
"' (" << getCurrentMemberFullID()
514 <<
"' as " << typeName<T>() <<
": " << ex.what());
517 else if ( mValue->type() == json_spirit::array_type )
523 catch(std::exception& ex)
525 MIRA_THROW(XMemberNotFound,
"Error deserializing item[" << mIndex-1
526 <<
"] (" << getCurrentMemberFullID()
528 <<
"' as " << typeName<T>() <<
": " << ex.what());
537 catch(std::exception& ex)
539 MIRA_THROW(XMemberNotFound,
"Error deserializing atomic (" << getCurrentMemberFullID()
540 <<
") with content='" <<
json::write(*mValue) <<
"' as " << typeName<T>() <<
": " << ex.what());
547 if ( mValue->type() == json_spirit::obj_type )
548 member = *getMember(mValue, mCurrentMemberID);
549 else if ( mValue->type() == json_spirit::array_type )
550 member = *getItem(mValue, mIndex++);
560 mFirstComplex =
false;
561 Base::object(member);
565 std::size_t oldIndex = mIndex;
566 if ( mValue->type() == json_spirit::obj_type )
568 mValue = getMember(mValue, mCurrentMemberID);
570 else if ( mValue->type() == json_spirit::array_type )
572 mValue = getItem(mValue, oldIndex);
576 Base::object(member);
585 collectionInternal(member);
588 template<
typename mapped_type>
591 if (!(mInputFormat & ACCEPT_STRING_MAP_AS_OBJECT)) {
592 collectionInternal(member);
596 if (mStringMapAsObject) {
597 json::Object::const_iterator oldCurrentStringMapMember = mCurrentStringMapMember;
599 mCurrentStringMapMember = oldCurrentStringMapMember;
603 assert(mStringMapAsObject ==
false);
607 std::size_t oldIndex = mIndex;
608 std::string memberID = mCurrentMemberID;
609 bool firstComplex = mFirstComplex;
616 collectionInternal(member);
618 catch (XInvalidConfig& ex) {
619 MIRA_LOG(
DEBUG) <<
"Deserialize map<string, T> as array failed, try as object:" << std::endl;
622 mObjectNameToInstance = trackingNames;
623 mObjects = trackingInstances;
627 mCurrentMemberID = memberID;
628 mFirstComplex = firstComplex;
632 mStringMapAsObject =
true;
638 mStringMapAsObject =
false;
639 MIRA_RETHROW(ex,
"While trying to deserialize map<string, T> as object.");
642 mStringMapAsObject =
false;
646 mStringMapAsObject =
false;
654 array = &mValue->get_array();
656 catch(std::exception& ex)
658 MIRA_THROW(XInvalidConfig,
"Error deserializing collection (" << getCurrentMemberFullID()
659 <<
") from json::Value='" <<
json::write(*mValue) <<
"': " << ex.what());
672 if ( mValue->type() == json_spirit::obj_type )
673 value = getMember(mValue, mCurrentMemberID);
674 else if ( mValue->type() == json_spirit::array_type )
675 value = getItem(mValue, mIndex);
678 std::size_t oldIndex = mIndex;
681 if(value->is_null()) {
686 assert(value->type() == json_spirit::obj_type);
687 auto it = value->get_obj().find(
"@ref");
688 if(it!=value->get_obj().end()) {
690 pointer = resolveReference<T>(it->second.get_value<std::string>());
693 Base::pointer(pointer);
697 if (mValue->type() == json_spirit::array_type)
708 if ( mValue->type() == json_spirit::obj_type )
712 value = getMember(mValue, mCurrentMemberID);
714 catch(XMemberNotFound& ex)
716 MIRA_RETHROW(ex,
"Error getting pointer class type of member '" 717 << mCurrentMemberID <<
"' (" << getCurrentMemberFullID()
718 <<
") from object='" <<
json::write(*mValue)<<
"'");
721 else if ( mValue->type() == json_spirit::array_type )
725 value = getItem(mValue, mIndex);
727 catch(XMemberNotFound& ex)
729 MIRA_RETHROW(ex,
"Error getting pointer class type of item[" << mIndex
730 <<
"] (" << getCurrentMemberFullID()
731 <<
") from collection='" <<
json::write(*mValue) <<
"'");
735 if(value->type() == json_spirit::obj_type)
737 auto it = value->get_obj().find(
"@class");
738 if(it!=value->get_obj().end())
739 return it->second.get_value<std::string>();
741 return "Unknown Class";
749 std::string oldID = mCurrentMemberID;
751 mCurrentMemberID = meta.
id;
754 Base::invokeMemberOverwrite(member, meta);
759 mCurrentMemberID = oldID;
763 mCurrentMemberID = oldID;
770 if (mStringMapAsObject) {
773 mCurrentStringMapMember = mValue->get_obj().begin();
774 return (uint32)o->size();
776 catch(std::exception& ex)
778 MIRA_THROW(XInvalidConfig,
"Error counting object elements from json::Value='" 783 return (uint32)a->size();
787 template<
typename Container>
788 void mapEntry(
int id, Container& c,
typename Container::iterator& hint)
790 typedef typename Container::value_type value_type;
791 typedef typename Container::mapped_type mapped_type;
793 if (mStringMapAsObject) {
794 std::string key = mCurrentStringMapMember->first;
795 hint = c.insert(hint, value_type(key, mapped_type()));
796 property(key.c_str(), hint->second,
"");
797 ++mCurrentStringMapMember;
805 void collectionInternal(T& member)
809 mFirstComplex =
false;
810 Base::object(member);
814 std::size_t oldIndex = mIndex;
815 if ( mValue->type() == json_spirit::obj_type )
817 mValue = getMember(mValue, mCurrentMemberID);
820 else if ( mValue->type() == json_spirit::array_type )
822 mValue = getItem(mValue, oldIndex);
826 Base::object(member);
836 std::string mCurrentMemberID;
838 bool mStringMapAsObject;
839 json::Object::const_iterator mCurrentStringMapMember;
841 InputFormat mInputFormat;
844 namespace serialization {
855 template<
typename Collection>
869 template<
typename Collection>
875 ioCount = (uint32)a->size();
879 template<
typename mapped_type>
888 template<
typename mapped_type>
906 template<
typename mapped_type>
917 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:453
void object(T &member)
Is called for each complex object.
Definition: RecursiveMemberReflector.h:302
void pointer(T *&pointer)
Definition: JSONSerializer.h:665
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:788
ReflectorInterface< JSONSerializer >::VersionType VersionType
Definition: AbstractReflector.h:170
Contains the Serializer template, a base class for all serializers.
static void reflect(JSONDeserializer &r, Container &c, uint32 count)
Definition: JSONSerializer.h:912
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
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:871
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:545
std::map< std::string, mapped_type > Container
Definition: JSONSerializer.h:891
Is a special reflector that is used for serialization.
Definition: Serializer.h:126
std::map< std::string, mapped_type > Container
Definition: JSONSerializer.h:909
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:528
#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:583
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:973
static void reflect(JSONSerializer &r, Container &c)
Definition: JSONSerializer.h:894
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:466
Container::iterator iterator
Definition: JSONSerializer.h:910
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
Definition: JSONSerializer.h:747
uint8 VersionType
Definition: ReflectorInterface.h:72
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
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:456
const ReflectMemberMeta & getCurrentMemberMeta() const
Returns the meta-information of the current member that is reflected.
Definition: RecursiveMemberReflector.h:451
uint32 getStringMapElementCount()
Definition: JSONSerializer.h:768
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:501
ReflectorInterface< JSONSerializer >::AcceptDesiredVersion AcceptDesiredVersion
Definition: AbstractReflector.h:200
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:872
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:589
void mapEntry(int id, const typename Container::value_type &p)
Definition: JSONSerializer.h:320
Base class for exceptions.
Definition: Exception.h:194
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
void deserialize(json::Value &value)
Definition: JSONSerializer.h:434
VersionType version(VersionType expectedVersion, AcceptDesiredVersion, const T *caller=NULL)
Definition: JSONSerializer.h:473
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:75
void object(T &member)
Definition: JSONSerializer.h:556
Container::value_type value_type
Definition: JSONSerializer.h:892
const json::Value * getItem(const json::Value *value, std::size_t index)
Definition: JSONSerializer.h:446
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:882
static constexpr auto MIRA_REFLECTOR_TOPLEVEL_NAME
Use this when a reflector needs to choose a name for serializing an object.
Definition: RecursiveMemberReflector.h:250
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:77
const json::Array * getCollection()
Definition: JSONSerializer.h:649
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:701
static void reflect(JSONSerializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:858
void pointerReference(int referencedObjectID)
Definition: JSONSerializer.h:261
OutputFormat format
Definition: JSONSerializer.h:115