views:

131

answers:

4

I'm trying to serialize objects to send over network through a socket using only STL. I'm not finding a way to keep objects' structure to be deserialized in the other host. I tried converting to string, to char* and I've spent a long time search tutorial, posts in internet and until now I found nothing.

Is there a way to do it only with STL?

Is there any good tutorial you konw?

I am almost trying boost, but if there is how to do it with STL I'd like to learn.

Thank you.

+2  A: 

You can serialize with anything. All serialization means is that you are converting the object to bytes so that you can send it over a stream (like an std::ostream) and read it with another (like an std::istream). Just override operator <<(std::ostream&, const T&) and operator >>(std::istream&, T&) where T is each of your types. And all the types contained in your types.

However, you should probably just use an already-existing library (Boost is pretty nice). There are tons of things that a library like Boost does for you, like byte-ordering, taking care of common objects (like arrays and all the stuff from the standard library), providing a consistent means of performing serialization and tons of other stuff.

Travis Gockel
Does current versions of Boost.Serialization take care of byte ordering? Previous versions of it only supported non-portable binary archives out of the box.
Staffan
How long ago was that? One of the **current** goals of the library is: `Data Portability - Streams of bytes created on one platform should be readable on any other.` So I'm pretty sure.
Travis Gockel
Thank you. It helps me.
Lucas Arbiza
+2  A: 

I recommend checking out c++ faq lite on serialization. It contains much useful information. http://www.parashift.com/c++-faq-lite/serialization.html

Laserallan
+1  A: 

My first question will be: do you want serialization or messaging ?

It might seem stupid at first, since you asked for serialization, but then I have always distinguished the two terms.

  • Serialization is about taking a snapshot of your memory and restoring it later on. Each object is represented as a separate entity (though they might be composed)
  • Messaging is about sending information from one point to another. The message usually has its own grammar and may not reflect the organization of your Business Model.

Too often I've seen people using Serialization where Messaging should have been used. It does not mean that Serialization is useless, but it does mean that you should think ahead of times. It's quite difficult to alter the BOM once you have decided to serialize it, especially if you decide to relocate some part of information (move it from one object to another)... because how then are you going to decode the "old" serialized version ?

Now that that's been cleared up...

... I will recommend Google's Protocol Buffer.

You could perfectly rewrite your own using the STL, but you would end up doing work that has already been done, and unless you wish to learn from it, it's quite pointless.

One great thing about protobuf is that it's language agnostic in a way: ie you can generate the encoder/decoder of a given message for C++, Java or Python. The use of Python is nice for message injection (testing) or message decoding (to check the output of a logged message). It's not something that would come easy were you to use the STL.

Matthieu M.
It's not only messaging, I want serialize a class containing types like int, string, double. I want to serialize an object and send it to another host and in this host deserialize it.Thanks for your answer.
Lucas Arbiza
Well, it can be thought as messenging still :) Messenging is about sending information, that the information coincides with a given object of your Business Model doesn't really matter.
Matthieu M.
A: 

I got it!

I used strinstream to serialize objects and I sent it as a message using the stringstream's method str() and so string's c_str().

Look.

class Object {
public:
int a;
string b;

void methodSample1 ();
void methosSample2 ();

friend ostream& operator<< (ostream& out, Object& object) {
out << object.a << " " << object.b;   //The space (" ") is necessari for separete elements
return out;
}

friend istream& operator>> (istream& in, Object& object) {
in >> object.a;
in >> object.b;
return in;
}
};

/* Server side */
int main () {
Object o;
stringstream ss;
o.a = 1;
o.b = 2;
ss << o;    //serialize

write (socket, ss.str().c_str(), 20); //send - the buffer size must be adjusted, it's a sample
}

/* Client side */
int main () {
Object o2;
stringstream ss2;
char buffer[20];
string temp;

read (socket, buffer, 20);  //receive
temp.assign(buffer);
ss << temp;
ss >> o2;   //unserialize
}

I'm not sure if is necessary convert to string before to serialize (ss << o), maybe is possible directly from char.

Lucas Arbiza
What happens when `Object.b` contains spaces?
ezpz
Lucas Arbiza