是否可以在 C++ 中序列化和反序列化一个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/234724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to serialize and deserialize a class in C++?
提问by Agusti-N
Is it possible to serialize and deserialize a class in C++?
是否可以在 C++ 中序列化和反序列化一个类?
I've been using Java for 3 years now, and serialization / deserialization is fairly trivial in that language. Does C++ have similar features? Are there native libraries that handle serialization?
我已经使用 Java 3 年了,序列化/反序列化在该语言中相当简单。C++有类似的功能吗?是否有处理序列化的本机库?
An example would be helpful.
一个例子会有所帮助。
采纳答案by Head Geek
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.
该Boost::serialization
库处理这个相当典雅。我已经在几个项目中使用过它。这里有一个例子程序,说明如何使用它,在这里。
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.
唯一的原生方法是使用流。这基本上是Boost::serialization
库所做的一切,它通过设置一个框架来将对象写入类似文本的格式并从相同的格式读取它们,从而扩展了流方法。
For built-in types, or your own types with operator<<
and operator>>
properly defined, that's fairly simple; see the C++ FAQfor more information.
对于内置类型,或您自己的类型operator<<
并operator>>
正确定义,这相当简单;有关更多信息,请参阅C++ 常见问题解答。
回答by Azoth
I realize this is an old post but it's one of the first that comes up when searching for c++ serialization
.
我意识到这是一篇旧帖子,但它是搜索c++ serialization
.
I encourage anyone who has access to C++11 to take a look at cereal, a C++11 header only library for serialization that supports binary, JSON, and XML out of the box. cereal was designed to be easy to extend and use and has a similar syntax to Boost.
我鼓励任何可以访问 C++11 的人都看一下谷物,这是一个 C++11 仅标头的序列化库,它支持开箱即用的二进制、JSON 和 XML。谷物被设计为易于扩展和使用,并且具有与 Boost 相似的语法。
回答by Frank Krueger
Boost is a good suggestion. But if you would like to roll your own, it's not so hard.
提升是一个很好的建议。但如果你想自己动手,也不是那么难。
Basically you just need a way to build up a graph of objects and then output them to some structured storage format (JSON, XML, YAML, whatever). Building up the graph is as simple as utilizing a marking recursive decent object algorithm and then outputting all the marked objects.
基本上,您只需要一种方法来构建对象图,然后将它们输出为某种结构化存储格式(JSON、XML、YAML 等)。构建图就像使用标记递归体面对象算法然后输出所有标记对象一样简单。
I wrote an article describing a rudimentary (but still powerful) serialization system. You may find it interesting: Using SQLite as an On-disk File Format, Part 2.
我写了一篇文章描述了一个基本(但仍然强大)的序列化系统。您可能会发现它很有趣:使用 SQLite 作为磁盘文件格式,第 2 部分。
回答by Frank Krueger
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.
您应该重写<<
以将您的对象输出到某个序列化上下文(通常是iostream
)并>>
从该上下文读回数据。每个对象负责输出其聚合的子对象。
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.
如果是这样,那么您将不得不使用库来处理这些循环。
回答by yoav.aviram
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.
Protobuf 不同于这里提到的其他序列化解决方案,因为它不会序列化您的对象,而是根据您的规范为序列化的对象生成代码。
回答by M2tM
Boost::serializationis a great option, but I've encountered a new project: Cerealwhich I find much more elegant! I highly suggest investigating it.
Boost::serialization是一个不错的选择,但我遇到了一个新项目:Cereal,我觉得它更优雅!我强烈建议调查它。
回答by Dave
You can check the amefprotocol, an example of C++ encoding in amef would be like,
您可以检查amef协议,amef 中的 C++ 编码示例如下所示,
//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,
在java中解码会像,
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.
协议实现有 C++ 和 Java 的编解码器,有趣的部分是它可以以名称值对的形式保留对象类表示,我在上一个项目中需要一个类似的协议,当我偶然发现这个协议时,我实际上根据我的要求修改了基础库。希望这对你有帮助。
回答by jbat100
I recommend using boost serialization as described by other posters. Here is a good detailed tutorial on how to use it which complements the boost tutorials nicely: http://www.ocoudert.com/blog/2011/07/09/a-practical-guide-to-c-serialization/
我建议使用其他海报所描述的 boost 序列化。这是一个关于如何使用它的详细教程,它很好地补充了 boost 教程:http: //www.ocoudert.com/blog/2011/07/09/a-practical-guide-to-c-serialization/
回答by Vincent
Sweet Persistis another one.
Sweet Persist是另一个。
It is possible to serialize to and from streams in XML, JSON, Lua, and binary formats.
可以与 XML、JSON、Lua 和二进制格式的流进行序列化。
回答by epatel
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 thereif a flexible factory is of interest. I try to describe an old way from ET++ to use macros which has worked great for me.
我已经在另一个关于 C++ 工厂的 SO 问题中回答了。如果您对灵活的工厂感兴趣,请查看那里。我试图描述一种从 ET++ 到使用宏的旧方法,它对我很有用。
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.
ET++是一个将旧 MacApp 移植到 C++ 和 X11 的项目。在它的努力下,Eric Gamma 等开始考虑设计模式。ET++ 包含在运行时进行序列化和自省的自动方法。