Question

I know how to serialize an object in the following way:

void encodeMsg(char **msg, const ConnectionParams& params)
{
    std::ostringstream oss;
    if (!(oss << params))
    {
        //failure
    }
    msg = oss.str();
}

how can i deserialize a message back into the object?

meaning i want to have this method:

ConnectionParams encodeMsg(char *msg)
    { ... }

so i don't have an instringstream object... how can i do that?

Était-ce utile?

La solution

Something like:

ConnectionParams decodeMsg(char *msg)
{
  ConnectionParams ret;
  std::istringstream iss(msg);
  iss >> ret;
  return ret;
}

That is assuming that this class was made to be serializable with operator<<, and provides the symmetrically opposite operation operator>>. There is no way to serialize an arbitrary C++ object without knowing its internals, and in this answer I am assuming these operation were already implemented by the class provider.

Also, note that you should probably pass ConnectionParams as reference, like:

void encodeMsg(char *msg, ConnectionParams& ret);

Also, your encodeMsg is wrong, has better chance to work as:

void encodeMsg(char **msg, const ConnectionParams& params)
{
    ....
    *msg = oss.str();
}

and even this is wrong, since oss.str() returns an internal pointer from oss, and you can't return it to the caller, because oss will be destroyed upon function return, and the pointer will be invalid. You'd better return a std::string or something like that.

EDIT: What if the class does not provides the means to serialize and deserialize itself? On classes that are simple enough, and don't hold pointers allocated on heap, nor file descriptors, nor have fancy behaviors on constructors and destructors, you may be able to do it the same way done with old C structs: interpret all its bytes as a buffer. Note that this is dangerous, type unsafe, and the buffer may not be inter-operable between different compilers/architectures.

// DISCLAIMER: Untested code!

class A {
  int plain_variable;
  char palin_array[50];
};
// Class A is ok to serialize this way

class B {
  A plain_object_array[10];
  double another_plain_variable;
};
// Class B is ok to serialize this way

class C {
  float plain_object;
  std::string fancy_string;
};
// Class C is *NOT* ok to serialize this way, because it contains an string,
// which, in turn, contains a heap allocated pointer

template<class T>
void serialize(const T& obj, std::string& buff)
{
  const char *accessor = (const char *)&obj;
  std::string tmp(accessor, sizeof(obj));
  std::swap(buff, tmp);
}

template<class T>
void deserialize(const std::string& buff, T& obj)
{
  char *accessor = (char *)&obj;
  std::copy(buff.begin(), buff.end(), accessor);
}

See, the conditions for this scheme to work are so that it is unlikely you will be able to use it with a third-party provided class. In the end, you will probably need to store all the data needed to rebuild the object yourself, and work on this data.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top