boost serialization and datatype mismatch

Hi, How to validate datatype before it gets deserialized? e.g. struct Abc { std::map<string,string> tmpMap; int a; float d; std::list<string> stringList; void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned int file_version) { ar & tmpMap; ar & a; ar & d; ar & stringList; } void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned int file_version) { ///////////////////////////////////////////////////////////////////// /* Now the right thing to do over here is ar & tmpMap; ar & a; ar & d; ar & stringList; */ /////////////////////////////////////////////////////////////////////// //But instead, if by mistake user writes something like ar & a; ar & d; ar & stringList; ar & tmpMap; /* So how to handle such problem? Can I validate data somehow before it gets deserialized? On some platforms like SuSE linux I have observed that such thing doesn't throw exception and we get SEGV. */ } }; Thanks, Sameer

The easy way is to use: template<class Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & tmpMap; ar & a; ar & d; ar & stringList; } then input/output are always insync. Robert Ramey "sam p" <sameersp21@gmail.com> wrote in message news:c49aaa00808212055v39e5036habe307521a42d5b@mail.gmail.com... Hi, How to validate datatype before it gets deserialized? e.g. struct Abc { std::map<string,string> tmpMap; int a; float d; std::list<string> stringList; void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned int file_version) { ar & tmpMap; ar & a; ar & d; ar & stringList; } void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned int file_version) { ///////////////////////////////////////////////////////////////////// /* Now the right thing to do over here is ar & tmpMap; ar & a; ar & d; ar & stringList; */ /////////////////////////////////////////////////////////////////////// //But instead, if by mistake user writes something like ar & a; ar & d; ar & stringList; ar & tmpMap; /* So how to handle such problem? Can I validate data somehow before it gets deserialized? On some platforms like SuSE linux I have observed that such thing doesn't throw exception and we get SEGV. */ } }; Thanks, Sameer ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

This is fine for most of the cases. But suppose I want to pass serialized stream over a network and at the other end I want to validate each datatype before deserializing it then how to do it? Can I use typeinfo somehow? Thanks, Sameer On Fri, Aug 22, 2008 at 11:47 PM, Robert Ramey <ramey@rrsd.com> wrote:
The easy way is to use:
template<class Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & tmpMap; ar & a; ar & d; ar & stringList; }
then input/output are always insync.
Robert Ramey
"sam p" <sameersp21@gmail.com> wrote in message news:c49aaa00808212055v39e5036habe307521a42d5b@mail.gmail.com... Hi,
How to validate datatype before it gets deserialized? e.g. struct Abc {
std::map<string,string> tmpMap; int a; float d; std::list<string> stringList;
void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned int file_version) { ar & tmpMap; ar & a; ar & d; ar & stringList; } void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned int file_version) { ///////////////////////////////////////////////////////////////////// /* Now the right thing to do over here is ar & tmpMap; ar & a; ar & d; ar & stringList; */
/////////////////////////////////////////////////////////////////////// //But instead, if by mistake user writes something like ar & a; ar & d; ar & stringList; ar & tmpMap; /* So how to handle such problem? Can I validate data somehow before it gets deserialized? On some platforms like SuSE linux I have observed that such thing doesn't throw exception and we get SEGV. */ }
};
Thanks, Sameer
------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

sam p wrote:
suppose I want to pass serialized stream over a network and at the other end I want to validate each datatype before deserializing it then how to do it?
Maybe you should use an XML stream? Then the library can validate that you're trying to deserialize the name-value pairs in the correct order.
participants (3)
-
Nat Goodspeed
-
Robert Ramey
-
sam p