4

In his answer to For which scenarios is protobuf-net not appropriate? Marc mentions:

jagged arrays / nested lists without intermediate types aren't OK - you can shim this by introducing an intermediate type in the middle

I'm hoping this suggests there is a way to do it without changing my underlying code, maybe using a surrogate? Has anybody found a good approach to serializing/deserializing a nested/jagged array

Community
  • 1
  • 1
David Hayes
  • 7,402
  • 14
  • 50
  • 62
  • possible duplicate of [Using ProtoBuf-Net, how to (de)serialize a multi-dimensional array?](http://stackoverflow.com/questions/4090173/using-protobuf-net-how-to-deserialize-a-multi-dimensional-array) – arkon Aug 09 '15 at 01:32

2 Answers2

9

At the current time, it would require (as the message suggests) changes to your model. However, in principal this is something that that the library could do entirely in its own imagination - that is simply code that I haven't written / tested yet. So it depends how soon you need it... I can take a look at it, but I can't guarantee any particular timescale.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Thanks, it's for a project I'm working on at the moment. I'll need to investigate the impact of chaning the structure I'm trying to serialize. It would be great if protobuf-net supported 2d/jagged arrays but I should be able to work around it for now – David Hayes Apr 24 '13 at 18:17
  • 2
    Yep, definitely needed here too :) – vexe Sep 29 '14 at 23:05
1

A solution might be to serialize an intermediate type, and use a getter/setter to hide it from the rest of your code. Example:

    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;
    }
}