我怎样才能坚持protobuf网可为空值的数组? [英] How can I persist an array of a nullable value in Protobuf-Net?

查看:804
本文介绍了我怎样才能坚持protobuf网可为空值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在迁移从BinaryFormatter的至protobuf网(它到目前为止似乎提供既在存储大小和反序列化时间而言巨大的改进)的过程。



不过,我所遇到的一个问题是双?[]数组不,他们序列相同的形式反序列化。被空获得其全部删除阵列中的任何值 - 即如果我开始用的[NULL,NULL,1,2,3,空],反序列化后,我结束了数组[1 6元素的数组,2,3]。对于我的计划,至关重要的是,我正是之前系列化他们是相同的形式retreive这些阵列 - 仿佛BinaryFormatter的使用了,会发生



一个解决方案,我有。想出到目前为止是为每一位,双[]之一,每个元件具有一个值,布尔[]可用于描述如果原始值为空之一创建两个数组 - 然而,这是非常低效的由于种种原因。



我在以前的相关的问题,有可能是一个ProtoMember一个'SupportNull'选项见提及,但我找不到任何显示文档明确如何实现这一点,不能被玩弄解决它自己。



任何帮助任何人都可以提供将非常感激。


解决方案

 使用protobuf的; 
使用ProtoBuf.Meta;
使用系统;
[ProtoContract]
类Foo
{
[ProtoMember(1)]
公共双[]值{得到什么?;组; }
}
静态类节目
{
静态无效的主要()
{
//配置模型; SupportNull当前不可用
//的属性,所以需要调整模型有点
RuntimeTypeModel.Default.Add(typeof运算(富),真)[1] .SupportNull =真;

//发明一些数据,然后克隆它(序列化+反序列化)
VAR OBJ =新的Foo {值=新的双?[] {1,NULL,2.5,空,3} };
无功克隆= Serializer.DeepClone(OBJ);

//检查我们得到了所有的值返回
的foreach(在clone.Values VAR值)
{
Console.WriteLine(值);
}
}
}


I am in the process of migrating from BinaryFormatter to Protobuf-net (which so far appears to offer HUGE improvements both in terms of storage size and deserialization time).

A problem I've encountered however is that double?[] arrays do not deserialize in the same form they were serialized. Any values in the array that are null get removed in their entirety - i.e. if I start with an array with 6 elements of [null, null, 1, 2, 3, null], after deserialization I end up with an array of [1, 2, 3]. For my programme, it is essential that I retreive these arrays in exactly the same form they were prior to serialization - as would happen if BinaryFormatter were used.

One solution I have come up with so far is to create two arrays for every one, one of double[] where every element has a value, and one of bool[] which can be used to describe if the original value was null - however this is very inefficient for various reasons.

I could see mentioned in a previous related question that there may be a 'SupportNull' option for a ProtoMember, but I couldn't find any documentation showing clearly how to implement this, and could not work it out myself by playing around.

Any help anyone could offer would be hugely appreciated.

解决方案

using ProtoBuf;
using ProtoBuf.Meta;
using System;
[ProtoContract]
class Foo
{
    [ProtoMember(1)]
    public double?[] Values { get; set; }
}
static class Program
{
    static void Main()
    {
        // configure the model; SupportNull is not currently available
        // on the attributes, so need to tweak the model a little
        RuntimeTypeModel.Default.Add(typeof(Foo), true)[1].SupportNull = true;

        // invent some data, then clone it (serialize+deserialize)
        var obj = new Foo { Values = new double?[] {1,null, 2.5, null, 3}};
        var clone = Serializer.DeepClone(obj);

        // check we got all the values back
        foreach (var value in clone.Values)
        {
            Console.WriteLine(value);
        }
    }
}

这篇关于我怎样才能坚持protobuf网可为空值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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