47 #ifndef _MIRA_JSONSERIALIZER_H_ 48 #define _MIRA_JSONSERIALIZER_H_ 121 : mReadOnly(readOnly), mOutputFormat(
format)
128 mFirstComplex =
true;
145 return this->version<T>(
version,
false);
157 return this->version<T>(
version,
true);
164 version = this->
template queryDesiredClassVersion<T>(
version, acceptDesiredVersion);
167 if ((vf == 0) || (vf == 1))
170 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
186 if (mValuePtr->type() == json_spirit::obj_type) {
190 else if (mValuePtr->type() == json_spirit::array_type) {
202 if (mValuePtr->type() == json_spirit::obj_type) {
206 else if (mValuePtr->type() == json_spirit::array_type) {
207 mValuePtr->get_array().push_back(
member);
221 mFirstComplex =
false;
222 if (!mPointerClass.empty()) {
223 mValuePtr->get_obj()[
"@class"] =
json::Value(mPointerClass);
224 mPointerClass.clear();
231 if (oldValue->type() == json_spirit::obj_type) {
236 else if (oldValue->type() == json_spirit::array_type) {
242 if (!mPointerClass.empty()) {
243 mValuePtr->get_obj()[
"@class"] =
json::Value(mPointerClass);
244 mPointerClass.clear();
247 mValuePtr = oldValue;
254 collectionInternal(
member);
257 template<
template<
typename U,
typename V>
class MapType,
typename T>
258 typename std::enable_if_t<std::is_same_v<T, typename MapType<std::string, T>::mapped_type>,
264 collectionInternal(
member);
271 if (mValuePtr->type() == json_spirit::obj_type)
273 else if (mValuePtr->type() == json_spirit::array_type)
274 mValuePtr->get_array().push_back(val);
279 mPointerClass =
type;
284 if (mValuePtr->type() == json_spirit::obj_type)
286 else if (mValuePtr->type() == json_spirit::array_type)
300 property(name, const_cast<T&>(
member), comment, std::move(hint), flags);
304 void roproperty(
const char* name,
const std::string&
id,
const T&
member,
const char* comment,
311 property(name,
id, const_cast<T&>(
member), comment, std::move(hint), flags);
321 property(name, a, comment, std::move(hint), flags);
325 template<
typename Container>
326 void mapEntry(
int id,
const typename Container::value_type& p)
328 using type =
typename Container::mapped_type;
331 type& nonconstv =
const_cast<type&
>(p.second);
332 member(p.first.c_str(), nonconstv,
"");
335 serialization::reflectReadMapPair<JSONSerializer, Container>(*
this,
"item", id, p);
341 void collectionInternal(T&
member)
345 mFirstComplex =
false;
351 if (oldValue->type() == json_spirit::obj_type) {
356 else if (oldValue->type() == json_spirit::array_type) {
363 mValuePtr = oldValue;
371 std::string mPointerClass;
431 mValue(&value), mStringMapAsObject(false), mInputFormat(
format)
435 template <
typename T>
439 mFirstComplex =
true;
450 if (value->get_obj().count(name) == 0)
451 MIRA_THROW(XMemberNotFound,
"Missing member '" << name
453 <<
") while deserializing object='" <<
json::write(*mValue));
454 return &value->get_obj().at(name);
459 if (value->get_array().size() <= index)
460 MIRA_THROW(XMemberNotFound,
"Missing item[" << index
462 <<
") while deserializing collection='" <<
json::write(*mValue));
463 return &value->get_array()[index];
468 template <
typename T>
472 if ((vf == 0) || (vf == 1))
473 return versionLegacy(expectedVersion);
475 static const std::string v = std::string(
"@version[") + typeName<T>() +
"]";
476 return getVersion<T>(expectedVersion, v);
482 return versionLegacy(expectedVersion);
485 template <
typename T>
488 return this->version<T>(expectedVersion);
495 return getVersion<void>(expectedVersion,
"@version");
498 template <
typename T>
501 if (mValue->get_obj().count(versionElement) == 0)
504 if (
version > expectedVersion) {
506 ") of type " << typeName<T>() <<
" into an older version (" << (int)expectedVersion <<
").";
516 if ( mValue->type() == json_spirit::obj_type )
522 catch(std::exception& ex)
524 MIRA_THROW(XInvalidConfig,
"Error deserializing member '" << mCurrentMemberID
527 <<
"' as " << typeName<T>() <<
": " << ex.what());
530 else if ( mValue->type() == json_spirit::array_type )
536 catch(std::exception& ex)
538 MIRA_THROW(XInvalidConfig,
"Error deserializing item[" << mIndex-1
541 <<
"' as " << typeName<T>() <<
": " << ex.what());
550 catch(std::exception& ex)
553 <<
") with content='" <<
json::write(*mValue) <<
"' as " << typeName<T>() <<
": " << ex.what());
560 if ( mValue->type() == json_spirit::obj_type )
562 else if ( mValue->type() == json_spirit::array_type )
573 mFirstComplex =
false;
578 std::size_t oldIndex = mIndex;
579 if ( mValue->type() == json_spirit::obj_type )
581 mValue =
getMember(mValue, mCurrentMemberID);
583 else if ( mValue->type() == json_spirit::array_type )
585 mValue =
getItem(mValue, oldIndex);
598 collectionInternal(
member);
601 template<
template<
typename U,
typename V>
class MapType,
typename T>
602 typename std::enable_if_t<std::is_same_v<T, typename MapType<std::string, T>::mapped_type>,
606 collectionInternal(
member);
610 if (mStringMapAsObject) {
611 json::Object::const_iterator oldCurrentStringMapMember = mCurrentStringMapMember;
613 mCurrentStringMapMember = oldCurrentStringMapMember;
617 assert(mStringMapAsObject ==
false);
621 std::size_t oldIndex = mIndex;
622 std::string memberID = mCurrentMemberID;
623 bool firstComplex = mFirstComplex;
630 collectionInternal(
member);
632 catch (XInvalidConfig& ex) {
633 MIRA_LOG(
DEBUG) <<
"Deserialize map<string, T> as array failed, try as object:" << std::endl;
641 mCurrentMemberID = memberID;
642 mFirstComplex = firstComplex;
646 mStringMapAsObject =
true;
652 mStringMapAsObject =
false;
653 MIRA_RETHROW(ex,
"While trying to deserialize MapType<string, T> as object.");
656 mStringMapAsObject =
false;
660 mStringMapAsObject =
false;
668 array = &mValue->get_array();
670 catch(std::exception& ex)
673 <<
") from json::Value='" <<
json::write(*mValue) <<
"': " << ex.what());
686 if ( mValue->type() == json_spirit::obj_type )
687 value =
getMember(mValue, mCurrentMemberID);
688 else if ( mValue->type() == json_spirit::array_type )
689 value =
getItem(mValue, mIndex);
692 std::size_t oldIndex = mIndex;
695 if(value->is_null()) {
700 if (value->type() == json_spirit::obj_type) {
701 auto it = value->get_obj().find(
"@ref");
702 if(it!=value->get_obj().end()) {
704 pointer = resolveReference<T>(it->second.get_value<std::string>());
715 if (mValue->type() == json_spirit::array_type)
726 if ( mValue->type() == json_spirit::obj_type )
730 value =
getMember(mValue, mCurrentMemberID);
732 catch(XMemberNotFound& ex)
734 MIRA_RETHROW(ex,
"Error getting pointer class type of member '" 736 <<
") from object='" <<
json::write(*mValue)<<
"'");
739 else if ( mValue->type() == json_spirit::array_type )
743 value =
getItem(mValue, mIndex);
745 catch(XMemberNotFound& ex)
747 MIRA_RETHROW(ex,
"Error getting pointer class type of item[" << mIndex
749 <<
") from collection='" <<
json::write(*mValue) <<
"'");
753 if(value->type() == json_spirit::obj_type)
755 auto it = value->get_obj().find(
"@class");
756 if(it!=value->get_obj().end())
757 return it->second.get_value<std::string>();
759 return "Unknown Class";
767 std::string oldID = mCurrentMemberID;
769 mCurrentMemberID = meta.
id;
777 mCurrentMemberID = oldID;
781 mCurrentMemberID = oldID;
788 if (mStringMapAsObject) {
791 mCurrentStringMapMember = mValue->get_obj().begin();
794 catch(std::exception& ex)
796 MIRA_THROW(XInvalidConfig,
"Error counting object elements from json::Value='" 805 template<
typename Container>
806 void mapEntry(
int id, Container& c,
typename Container::iterator& hint)
808 typedef typename Container::value_type value_type;
809 typedef typename Container::mapped_type mapped_type;
811 if (mStringMapAsObject) {
812 std::string key = mCurrentStringMapMember->first;
813 hint = c.insert(hint, value_type(key, mapped_type()));
814 property(key.c_str(), hint->second,
"");
815 ++mCurrentStringMapMember;
823 void collectionInternal(T&
member)
827 mFirstComplex =
false;
832 std::size_t oldIndex = mIndex;
833 if ( mValue->type() == json_spirit::obj_type )
835 mValue =
getMember(mValue, mCurrentMemberID);
838 else if ( mValue->type() == json_spirit::array_type )
840 mValue =
getItem(mValue, oldIndex);
854 std::string mCurrentMemberID;
856 bool mStringMapAsObject;
857 json::Object::const_iterator mCurrentStringMapMember;
862 namespace serialization {
873 template<
typename Collection>
888 template<
typename Collection>
894 ioCount = (
uint32)a->size();
898 namespace SpecializeForMapped_Type {
902 template<
typename Collection,
typename T,
typename HasMappedType = std::true_type>
908 ioCount = (
uint32)a->size();
915 template<
typename Collection,
typename T>
917 typename
std::is_same<T, typename Collection::mapped_type>
::type>
934 template<
template<
typename U,
typename V>
class MapType,
typename T>
945 template<
template<
typename U,
typename V>
class MapType,
typename mapped_type>
963 template<
template<
typename U,
typename V>
class MapType,
typename mapped_type>
974 for(
uint32 id=0;
id<count; ++id)
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:95
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:436
Base::VersionType VersionType
Definition: JSONSerializer.h:466
void object(T &member)
Is called for each complex object.
Definition: RecursiveMemberReflector.h:385
void pointer(T *&pointer)
Definition: JSONSerializer.h:679
InputFormat
Flags for JSON input format.
Definition: JSONSerializer.h:416
void pointerNull()
Definition: JSONSerializer.h:282
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:969
void roproperty(const char *name, const T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: JSONSerializer.h:293
void mapEntry(int id, Container &c, typename Container::iterator &hint)
Definition: JSONSerializer.h:806
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:315
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:603
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:554
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:947
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:558
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:596
VersionType version(VersionType version, const T *caller=NULL)
Definition: JSONSerializer.h:143
std::vector< void *> ObjectsVector
Definition: Deserializer.h:311
void pointerWithClassType(const std::string &type)
Definition: JSONSerializer.h:277
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:1049
std::map< std::string, void *> ObjectNameToInstanceMap
Definition: Deserializer.h:307
void collection(T &member)
Definition: JSONSerializer.h:252
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:479
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
Definition: JSONSerializer.h:765
#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:448
VersionType version(VersionType expectedVersion, const T *object=NULL)
Definition: JSONSerializer.h:469
MapType< std::string, mapped_type > Container
Definition: JSONSerializer.h:966
const ReflectMemberMeta & getCurrentMemberMeta() const
Returns the meta-information of the current member that is reflected.
Definition: RecursiveMemberReflector.h:543
Container::value_type value_type
Definition: JSONSerializer.h:949
uint32 getStringMapElementCount()
Definition: JSONSerializer.h:786
MIRA_ENUM_TO_FLAGS_INCLASS(OutputFormat) JSONSerializer(bool readOnly
Construct a serializer.
void object(T &member)
Definition: JSONSerializer.h:216
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:406
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:304
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:155
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:259
ObjectsVector mObjects
maps the ids to the instance pointers
Definition: Deserializer.h:313
void atomic(T &member)
Definition: JSONSerializer.h:514
static void reflect(JSONDeserializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:891
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:199
Definition: JSONSerializer.h:66
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:326
Base class for exceptions.
Definition: Exception.h:195
MapType< std::string, mapped_type > Container
Definition: JSONSerializer.h:948
Base::VersionType VersionType
Definition: Serializer.h:154
MIRA_ENUM_TO_FLAGS_INCLASS(InputFormat) JSONDeserializer(const json
Definition: JSONSerializer.h:428
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:443
Accept associative containers with string keys as JSON object (keys = member names), in ADDITION to the standard array format (i.e.
Definition: JSONSerializer.h:425
VersionType version(VersionType expectedVersion, AcceptDesiredVersion, const T *caller=NULL)
Definition: JSONSerializer.h:486
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:148
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:569
Container::iterator iterator
Definition: JSONSerializer.h:967
const json::Value * getItem(const json::Value *value, std::size_t index)
Definition: JSONSerializer.h:457
static constexpr auto MIRA_REFLECTOR_TOPLEVEL_NAME
Use this when a reflector needs to choose a name for serializing an object.
Definition: RecursiveMemberReflector.h:333
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
The actual invokeMember implementation that is called from invokeMember().
Definition: RecursiveMemberReflector.h:750
Default format.
Definition: JSONSerializer.h:107
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:104
Definition: LoggingCore.h:78
const json::Array * getCollection()
Definition: JSONSerializer.h:663
Default format.
Definition: JSONSerializer.h:419
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:110
void atomic(T &member)
Definition: JSONSerializer.h:183
static void reflect(JSONSerializer &r, Container &c)
Definition: JSONSerializer.h:951
Helpers for serialization and deserialization of STL containers.
Definition: StlCollections.h:391
std::string pointerClassType()
Definition: JSONSerializer.h:719
static void reflect(JSONSerializer &r, uint32 &ioCount)
Definition: JSONSerializer.h:876
void pointerReference(int referencedObjectID)
Definition: JSONSerializer.h:267
Is a special reflector that is used for deserialization.
Definition: Deserializer.h:99
OutputFormat format
Definition: JSONSerializer.h:120