|
Boost Users : |
Subject: [Boost-users] [Serialization] Runtime checking of types in deserialization
From: Frank Winklmeier (frank.winklmeier_at_[hidden])
Date: 2008-10-28 15:24:44
Hi,
is there any way to check whether the class type used in deserialization
is identical to the class type in the serialization? Below is a simple
example, that serializes class "Data" into a text archive and
deserializes this archive into class "StringData". If that happens I
would like an exception to be thrown. Is this possible?
Thanks for any help,
Frank
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream>
using namespace std;
class Data {
public:
Data() { for (int i=0; i<100; i++) a[i]=i; };
int a[100];
template<class Archive>
void serialize(Archive & ar, const unsigned int) { ar & a; }
};
class StringData {
public:
StringData() {};
string s;
template<class Archive>
void serialize(Archive & ar, const unsigned int) { ar & s; }
};
int main() {
ostringstream oss;
// Serialize the class
const Data d1;
{
boost::archive::text_oarchive oa(oss);
oa << d1;
}
// Deserialize it using the wrong type
StringData d2;
{
istringstream iss(oss.str());
boost::archive::text_iarchive ia(iss);
ia >> d2;
}
cout << oss.str() << endl;
cout << d2.s << endl;
}
Output:
=======
22 serialization::archive 4 0 0 100 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
87 88 89 90 91 92 93 94 95 96 97 98 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net