Is it possible to serialize and deserialize a class in C++?

前端 未结 13 2564
我寻月下人不归
我寻月下人不归 2020-11-22 02:18

Is it possible to serialize and deserialize a class in C++?

I\'ve been using Java for 3 years now, and serialization / deserialization is fairly trivial in that lang

相关标签:
13条回答
  • 2020-11-22 02:36

    As far as "built-in" libraries go, the << and >> have been reserved specifically for serialization.

    You should override << to output your object to some serialization context (usually an iostream) and >> to read data back from that context. Each object is responsible for outputting its aggregated child objects.

    This method works fine so long as your object graph contains no cycles.

    If it does, then you will have to use a library to deal with those cycles.

    0 讨论(0)
  • 2020-11-22 02:45

    The Boost::serialization library handles this rather elegantly. I've used it in several projects. There's an example program, showing how to use it, here.

    The only native way to do it is to use streams. That's essentially all the Boost::serialization library does, it extends the stream method by setting up a framework to write objects to a text-like format and read them from the same format.

    For built-in types, or your own types with operator<< and operator>> properly defined, that's fairly simple; see the C++ FAQ for more information.

    0 讨论(0)
  • 2020-11-22 02:45

    I suggest looking into Abstract factories which is often used as a basis for serialization

    I have answered in another SO question about C++ factories. Please see there if a flexible factory is of interest. I try to describe an old way from ET++ to use macros which has worked great for me.

    ET++ was a project to port old MacApp to C++ and X11. In the effort of it Eric Gamma etc started to think about Design Patterns. ET++ contained automatic ways for serialization and introspection at runtime.

    0 讨论(0)
  • 2020-11-22 02:46

    I recommend Google protocol buffers. I had the chance to test drive the library on a new project and it's remarkably easy to use. The library is heavily optimized for performance.

    Protobuf is different than other serialization solutions mentioned here in the sense that it does not serialize your objects, but rather generates code for objects that are serialization according to your specification.

    0 讨论(0)
  • 2020-11-22 02:50

    Boost::serialization is a great option, but I've encountered a new project: Cereal which I find much more elegant! I highly suggest investigating it.

    0 讨论(0)
  • 2020-11-22 02:51

    You can check the amef protocol, an example of C++ encoding in amef would be like,

        //Create a new AMEF object
        AMEFObject *object = new AMEFObject();
    
        //Add a child string object
        object->addPacket("This is the Automated Message Exchange Format Object property!!","adasd");   
    
        //Add a child integer object
        object->addPacket(21213);
    
        //Add a child boolean object
        object->addPacket(true);
    
        AMEFObject *object2 = new AMEFObject();
        string j = "This is the property of a nested Automated Message Exchange Format Object";
        object2->addPacket(j);
        object2->addPacket(134123);
        object2->addPacket(false);
    
        //Add a child character object
        object2->addPacket('d');
    
        //Add a child AMEF Object
        object->addPacket(object2);
    
        //Encode the AMEF obejct
        string str = new AMEFEncoder()->encode(object,false);
    

    Decoding in java would be like,

        string arr = amef encoded byte array value;
        AMEFDecoder decoder = new AMEFDecoder()
        AMEFObject object1 = AMEFDecoder.decode(arr,true);
    

    The Protocol implementation has codecs for both C++ and Java, the interesting part is it can retain object class representation in the form of name value pairs, I required a similar protocol in my last project, when i incidentally stumbled upon this protocol, i had actually modified the base library according to my requirements. Hope this helps you.

    0 讨论(0)
提交回复
热议问题