需要库进行二进制流序列化,C ++ [英] Need library for binary stream serialization, C++

查看:69
本文介绍了需要库进行二进制流序列化,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找的东西类似于RakNet内置的序列化库(我不能在当前项目中使用).我需要能够在本地将二进制流保存/加载为自定义格式,并通过网络发送它们.网络部分已解决,但是我真的不想编写自己的方法将所有不同类型的数据序列化为二进制,特别是因为如果没有任何压缩方法,效率会很低.

What I'm looking for is similar to the serialization library built into RakNet (which I cannot use on my current project). I need to be able save/load binary streams into a custom format locally, and also send them over a network. The networking part is solved, but I really don't want to write my own methods for serializing all of my different types of data into binary, especially since it would be inefficient without any compression methods.

这里有一些伪代码类似于RakNet的比特流的工作方式,这与我要寻找的内容类似:

Here's some pseudocode similar to how RakNet's bitstreams work, this is along the lines of what I'm looking for:

class Foo
{
public:
    void Foo::save(BitStream& out)
    {
        out->write(m_someInt);
        out->write(m_someBool);
        m_someBar.save(out);

        // Alternative syntax
        out->write<int>(m_someInt);

        // Also, you can define custom serialization for custom types so you can do this...
        out->write<Bar>(m_someBar);

        // Or this...
        out->write(m_someBar);     
    }

    void Foo::load(BitStream& in)
    {
        in->read(m_someInt);
        in->read(m_someBool);
        in->read(m_someBar);
    }

private:
    int m_someInt;
    bool m_someBool;
    Bar m_someBar;
};

有没有免费的C ++库允许类似这样的事情?我基本上只希望将数据打包成二进制文件,然后压缩以进行序列化,然后将其解压缩为二进制文件,以便可以将其反馈到数据中.

Are there any free C++ libraries out there that allow for something like this? I basically just want something to pack my data into binary, and compress it for serialization, and then decompress it back into binary that I can feed back into my data.

编辑,添加更多信息:
不幸的是,无论是Google协议缓冲区还是Boost序列化都无法满足我的需求.两者都希望序列化对象成员,我只需要简单地序列化数据.例如,假设我有一个 std :: vector< Person> ,而类 Person 的名称是 std :: string ,和其中的其他数据,但我只想序列化和反序列化其名称.使用Google Protocol Buffers,我希望我可以将它作为整体进行Person对象的序列化.但是,我可以通过Boost序列化来实现,但是如果我有另一种需要对整个 Person 进行序列化的方案,则无法做到这一点,您要么必须对所有序列化,或没有.基本上,我需要相当大的灵活性来制作二进制流,但是我认为合适,我只想一个库来帮助我管理向/从流中读取和写入二进制数据,以及对其进行压缩/解压缩.

EDIT, adding more information:
Unfortunately neither Google Protocol Buffers or Boost Serialization will work for my needs. Both expect to serialize object members, I need to simply serialize data. For example, lets say I have a std::vector<Person>, and the class Person has a std::string for name, and other data in it, but I only want to serialize and deserialize their names. With Google Protocol Buffers it expects me to give it the Person object as a whole for serialization. I can achieve however, achieve this with Boost Serialization, but if I have another scenario where I need the entire Person to be serialized, there is no way to do that, you either have to serialize all of it, or none. Basically I need quite a bit of flexibility to craft the binary stream however I see fit, I just want a library to help me manage reading and writing binary data to/from the stream, and compressing/decompressing it.

推荐答案

  1. Google协议缓冲区
  2. Boost序列化

更新

看着更新的问题,我认为编写一个完全符合要求的小型自定义库可能是最简单的.我有一个类似的代码,只有几百行代码(没有压缩).编写此类代码的单元测试非常容易,因此从第一天开始就很可靠.

Looking at the updated question I think it might be easiest to write a small custom library that does exactly what is required. I have a similar one and it is only a few hundred lines of code (without compression). It is extremely easy to write unit tests for this kind of code, so it can be reliable from day one.

要序列化自定义类型,我有一个具有保存和加载方法的Persistent基类:

To serialize custom types, I have a Persistent base class that has save and load methods:

class Storage {
public:
  void writeInt( int i );
  void writeString( string s );
  int readInt();
  string readString();
};

class Persistent {
public:
  virtual void save( Storage & storage ) = 0;
  virtual void load( Storage & storage ) = 0;
};

class Person : public Persistent {
private:
  int height;
  string name;
public:
  void save( Storage & storage ) {
    storage.writeInt( height );
    storage.writeString( name );
  }

  void load( Storage & storage ) {
    storage.readInt( height );
    storage.readString( name );
  }
};

然后在其上有一个简单的层,该层在保存时存储一些类型信息,并在加载时使用Factory创建新对象.

And then there's a simple layer on top of that that stores some type information when saving and uses a Factory to create new objects when loading.

这可以通过使用C ++的流(我不太喜欢,因此是Storage类)来进一步简化.或复制Boost使用&的方法操作员将加载和保存合并到一个方法中.

This could be further simplified by using C++'s streams (which I don't like very much, hence the Storage class). Or copying Boost's approach of using the & operator to merge load and save into a single method.

这篇关于需要库进行二进制流序列化,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆