7

The main objective is to dynamically populated the Dictionary Object and serialize it using the Protobuf-net and send it across the WCF service to the client side.

@marc Can you please tell me a code sample as to how can I resolve the error "Nested or jagged lists and arrays are not supported" when I am trying to serialize using Protobuf-net.

[Serializable]
[ProtoContract]
public class DynamicWrapper
{
    [ProtoMember(1)]
    public List<Dictionary<string, string>> Items { get; set; }

    public DynamicWrapper()
    {
        Items = new  List<Dictionary<string, string>>();
    }
}

public class Service1 : IService1
{
    public byte[] GetData(string sVisibleColumnList)
    {
        DynamicWrapper dw = new DynamicWrapper();
        Dictionary<string, string> d = new Dictionary<string, string>();
        d.Add("CUSIP", "123456");
        dw.Items.Add(d);

        d = new Dictionary<string, string>();
        d.Add("ISIN", "456789");
        dw.Items.Add(d);
        var ms = new MemoryStream();
        var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
        model.Serialize(ms, dw);
        Serializer.Serialize <DynamicWrapper>(ms, dw);
        return ms.GetBuffer();
    }

}
Ram
  • 147
  • 3
  • 11

1 Answers1

13

You can wrap your Dictionary into a separate class. Then use a list of these objects instead.

[ProtoContract]
public class DynamicWrapper
{
    [ProtoMember(1)]
    public List<DictWrapper> Items { get; set; }

    public DynamicWrapper()
    {
        Items = new  List<DictWrapper>();
    }
}

[ProtoContract]
public class DictWrapper
{
    [ProtoMember(1)]
    public Dictionary<string, string> Dictionary { get; set; }
}
MichaelS
  • 3,809
  • 2
  • 26
  • 33