序列化/反序列化协议缓冲区 [英] Serializing/Deserializing Protocol Buffers

查看:145
本文介绍了序列化/反序列化协议缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用协议缓冲区(版本3 C#). 我正在来回发送消息到不同的服务. 并且目前正在尝试将某些消息中存储的某些数据保存到数据库中(实际上可以是任何种类).

I am currently working with Protocol Buffers (version 3 C#). I am sending messages back and forth to different services. and currently trying to save some data stored in certain messages to a database (could be of any kind really).

问题是 byte [] 被创建为类型 ByteString List< T> 创建为 RepeatedField< T> . 现在,我遇到的问题是我还没有成功100%成功地序列化或反序列化它们.

The problem being that byte[] is created as the type ByteString and List< T> is created as RepeatedField< T>. Now the issue I have with these are that I haven't managed to serialize or deserialize them 100% successfully.

基本类型的工作原理很吸引人,但是协议缓冲区自身的类型可能是一个挑战.

Basic types works like a charm, but Protocol Buffers own types can be a challenge.

我尝试了 AutoMapper 并创建了使ByteString可以序列化/反序列化的映射,但是RepeatedField很难,因为它是只读的并且具有私有setter且不能在构造函数中设置.

I tried AutoMapper and created maps which made ByteString possible to serialize/deserialize but the RepeatedField is hard since it is readonly and have a private setter and is not settable in the constructor.

我无法设法使AutoMapper正确映射到它,如果在此过程中遇到更多麻烦,并且不知道是否存在更简单的方法来将消息保存在数据库中,我不会感到惊讶吗?

I cannot manage to get AutoMapper to map correctly to it, I wouldn't be surprised if there are more troubles along the way and was wondering if there are easier ways to save messages in a database?

我已经阅读了protobuf的旧版本,在那里(如果我没有记错的话)有所谓的生成器,您可以访问每条消息的可变属性,这会使序列化/反序列化变得更加容易.还是有一种更明显的方式来访问数据并将其存储在我没有看到的数据库中?

I have read older versions of protobuf where (if I'm not mistaken) there were so called builders that you could access properties for each message which were mutable and would have made serializing/deserializing much easier. Or is there a more apparent way to access the data and store it in a database that I am just not seeing?

我理解消息不可变的原因,但实际上没有将包含数据保存到数据库的简单方法吗? 感觉很重要.

I understand the reason why messages are immutable but is there really no straight-forward way of saving the containg data to a database? Feels like an important feature.

PS:我知道有一个protobuf-net解决方案可以处理序列化/反序列化,但它仅支持protobuf v.2,而我在很大程度上依赖于v.3功能,例如 Any .

PS: I am aware that there is a protobuf-net solution which handles serializing/deserializing but it only supports protobuf v.2 and I am heavy dependent on v.3 features such as Any.

推荐答案

我已经将AutoMapper 6.1.1映射到protobufs 3 RepeatedField<>.我确定可以改善反射,但是AutoMapper的配置是:

I have got AutoMapper 6.1.1 to map to protobufs 3 RepeatedField<>. I'm sure the reflection could be improved, but AutoMapper configuration is:

void Configure(IMapperConfigurationExpression cfg)
{
    cfg.CreateMap<ProtoThings, HasListOfThings>().ReverseMap();

    bool IsToRepeatedField(PropertyMap pm)
    {
        if (pm.DestinationPropertyType.IsConstructedGenericType)
        {
            var destGenericBase = pm.DestinationPropertyType.GetGenericTypeDefinition();
            return destGenericBase == typeof(RepeatedField<>);
        }
        return false;
    }
    cfg.ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());
}

这篇关于序列化/反序列化协议缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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