I am trying to Serialize a class.
Class definition:
class StartPeerSessionRequest {
public:
StartPeerSessionRequest();
virtual ~StartPeerSessionRequest();
void composeRequestwithHardCodeValues();
void save();
stringstream serializedRequest;
/*boost::serialization::binary_object serlreq;*/
private:
StartPeerSessionRequest(const StartPeerSessionRequest &);
uint16_t mProtocolVersion;
uint16_t mSessionFlags;
uint16_t mMaxResponseLength;
string mMake;
string mModel;
string mSerialNumber;
uint8_t mTrackDelay;
string mHeadUnitModel;
string mCarModelYear;
string mVin;
uint16_t mVehicleMileage;
uint8_t mShoutFormat;
uint8_t mNotificationInterval;
friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &ar, const unsigned int version);
};
StartPeerSessionRequest::StartPeerSessionRequest() {
mProtocolVersion = 1 * 10000 + 14 * 100 + 4;
mSessionFlags = 1;
mMaxResponseLength = 0;
mMake = "MyMake";
mModel = "MyModel";
mSerialNumber = "10000";
mTrackDelay = 0;
mHeadUnitModel = "Headunit";
mCarModelYear = "2014";
mVin = "1234567980";
mVehicleMileage = 1000;
mShoutFormat = 3;
mNotificationInterval = 1;
}
template <class Archive> void StartPeerSessionRequest::serialize(Archive &ar, const unsigned int version) {
ar & mProtocolVersion;
ar & mSessionFlags;
ar & mMaxResponseLength;
ar & mMake;
ar & mModel;
ar & mSerialNumber;
ar & mTrackDelay;
ar & mHeadUnitModel;
ar & mCarModelYear;
ar & mVin;
ar & mVehicleMileage;
ar & mShoutFormat;
ar & mNotificationInterval;
}
void StartPeerSessionRequest::save() {
boost::archive::binary_oarchive oa(serlreq, boost::archive::no_header);
oa << (*this);
/*cout<<"\n binary_oarchive :"<<serlreq.size();*/
boost::archive::text_oarchive ota(serializedRequest, boost::archive::no_header);
ota << (*this);
cout << "\n text_oarchive :" << serializedRequest.str() << "size :" << serializedRequest.str().size();
}
serializedRequest.str.size()
provides me a length of 87
Actually it should provide me 65 bytes. (I've counted u can figure that out from the constructor)
I suspect it is appending lengths in between.
I have tried using text_archive
also it doesnt work.
What I need is to just plain serialize class members as it is.
I guess i need to use some traits or wrappers.
Please let me know
Thanks
You seem to have some highly specific assumptions about how Boost Serialization should serialize to it's proprietary, non-portable binary format.
Boost serialization is much more highlevel, more or less specifically designed to deal with non-POD data. If you insist, you should be able to serialize an array of your POD type directly. In your question, though, the class is not at all POD and hence not bitwise serializable anyway.
For portable archives, see EOS Portable Archive.
Boost Archives have optional flags that suppress the format header:
See Archive Models
Here's a backgrounder to see what introduces overhead over simple bitwise serialization:
Okay, so, just to see how I'd do, I've tried to reach the optimum sizes I calculated on the back of my napkin:
In this instance, I created binary serialization code using Boost Spirit (Karma for serialization and Qi for de-serialization). I made the size of the length field configurable (8,16,32 or 64 bit unsigned).
Here's a working proof of concept: Live On Coliru
generate()
The const generate member function delegates the work to helper functions in a separate namespace:
Note that
do_generate
overloads can be freely added as required for future typesstd::vector<unsigned char>
, to e.g.boost::interprocess::containers::string<char, char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> >
.parse()
The parse method is very similar except it delegates to
do_parse
overloads to do the work.Testing
The test program roundtrips with all possible configurations:
As you can see it's not even that outrageous that the natural Boost Serialization solution would take 107 bytes on my system (it's only 8 bytes more than my last configuration).
Note also, that since the Karma generators all take any output iterator, it should be relatively easy to wire it directly into the low-level Boost Archive operations for performance and to avoid allocating intermediate storage.