Search code examples
c++cross-platformice

Explicitly serialize Slice object to string or ostream in C++


Is there a way to explicitly serialize a Slice object to a string using ice? The problem is that there is an object which must be sendable by json / xml / ice and since ice already has a platform independent object in the Specification Language for Ice(Slice), there is no need to include another library like protobuf. But as far as I can see, it is not possible to serialize the object explicitly. Am I wrong?


Solution

  • You can serialize the object in the Ice binary format using the OutputStream API

    Ice::ByteSeq inParams, outParams;
    Ice::OutputStream out(communicator);
    out.startEncapsulation();
    Demo::CPtr c = new Demo::C;
    c->s.name = "blue";
    c->s.value = Demo::blue;
    out.write(c);
    out.writePendingValues();
    out.endEncapsulation();
    out.finished(inParams);
    

    There is additional examples in ice-demos repository https://github.com/zeroc-ice/ice-demos/tree/3.7/cpp98/Ice/invoke

    The docs for OutputStream can be found at https://doc.zeroc.com/ice/3.7/client-server-features/dynamic-ice/streaming-interfaces/c++-streaming-interfaces/the-outputstream-interface-in-c++