2013-04-23 95 views
3

在他的回答For which scenarios is protobuf-net not appropriate?马克提到:如何使用protobuf-net序列化/反序列化锯齿状/嵌套数组?

交错数组/嵌套,没有中间类型的名单都不行 - 你可以通过在中间

我引入一个中间类型垫片这希望这表明有一种方法可以在不改变我的底层代码的情况下做到,也许使用代理? 有没有人发现一个好的方法来序列化/反序列化嵌套/锯齿状阵列

+0

的可能重复的[?使用protobuf网,如何(DE)序列化多维阵列(http://stackoverflow.com/questions/4090173/using-protobuf-net- how-to-deserialize-a-multi-dimensional-array) – 2015-08-09 01:32:52

回答

8

在当前时间,它会要求(如消息所示)改变你的模型。然而,原则上这是图书馆完全可以在自己的想象中完成的事情 - 这只是我还没有编写/测试过的代码。所以这取决于你需要多久...我可以看看它,但我不能保证任何特定的时间表。

+1

谢谢,这是我目前正在开发的一个项目。我需要调查chaning我试图序列化的结构的影响。如果protobuf-net支持2d /参差不齐的阵列,这将是非常好的,但我现在应该能够解决这个问题 – 2013-04-24 18:17:08

+7

+1,因为我也需要这个功能:) – Fdr 2013-12-11 14:05:16

+2

是的,这里肯定也需要:) – vexe 2014-09-29 23:05:50

1

解决方案可能是序列化中间类型,并使用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; 
    } 
}