繁体   English   中英

如何使用protobuf-net序列化/反序列化锯齿/嵌套数组?

[英]How do you serialize/deserialize jagged/nested arrays with protobuf-net?

在他的回答中, 对于哪些场景是protobuf-net不合适? 马克提到:

没有中间类型的锯齿状数组/嵌套列表也不行 - 您可以通过在中间引入中间类型来对此进行填充

我希望这表明有一种方法可以在不改变我的底层代码的情况下完成,也许使用代理? 有没有人找到一个很好的方法来序列化/反序列化嵌套/锯齿状数组

目前,它需要(如消息所示)对您的模型进行更改。 然而,原则上这是图书馆可以完全按照自己的想象完成的事情 - 这只是我尚未编写/测试过的代码。 所以这取决于你需要多久......我可以看看它,但我不能保证任何特定的时间尺度。

解决方案可能是序列化中间类型,并使用getter / setter将其与其余代码隐藏。 例:

    List<double[]> _nestedArray ; // The nested array I would like to serialize.

    [ProtoMember(1)] 
    private List<ProtobufArray<double>> _nestedArrayForProtoBuf // Never used elsewhere
    {
        get 
        {
            if (_nestedArray == null)  //  ( _nestedArray == null || _nestedArray.Count == 0 )  if the default constructor instanciate it
                return null;
            return _nestedArray.Select(p => new ProtobufArray<double>(p)).ToList();
        }
        set 
        {
            _nestedArray = value.Select(p => p.MyArray).ToList();
        }
    }



[ProtoContract]
public class ProtobufArray<T>   // The intermediate type
{
    [ProtoMember(1)]
    public T[] MyArray;

    public ProtobufArray()
    { }
    public ProtobufArray(T[] array)
    {
        MyArray = array;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM