0

I want to use MsgPack instead of Newtonsoft.JSON as it is much faster but I have an issue when trying to deserialize a list of nullable integer.

Here is a snippet of the code I am using:

          public class MyClass
          {
                 public MyClass()
                 {
                       MyCustomList = new List<int?>();
                 }
                 public List<int?> MyCustomList { get; private set; }
          }


        MyClass source = new MyClass();
        source.MyCustomList.Add(1);
        source.MyCustomList.Add(null);

        var context = new SerializationContext {SerializationMethod = SerializationMethod.Map};
        context.DictionarySerlaizationOptions.OmitNullEntry = true;

        //Create serializers
        var serializer = SerializationContext.Default.GetSerializer<MyClass>(context);
        var serializerDest = SerializationContext.Default.GetSerializer<MyClass>(context);

        Stream stream = new MemoryStream();
        serializer.Pack(stream, source);
        stream.Position = 0;
        var unpackedObject = serializerDest.Unpack(stream); 

The last line of code is throwing an exception like "{"The unpacked value is not 'System.Int32' type. Do not convert nil MessagePackObject to System.Int32."}"

My 'MyCustomList' property is of type List and does not work. If I switched to IList it works

Any idea if this is a known error? How can I get rid of it?

Thanks

Bogdan MITREA
  • 101
  • 1
  • 10

1 Answers1

0

I am using MsgPack version 2.0.0-alpha2 and it works fine. Only thing I did is I commented out this line, because this version of msgpack did not recognise it.

//context.DictionarySerializationOptions.OmitNullEntry = true;

enter image description here

using System.IO;
//using Microsoft.VisualStudio.Modeling;
using MsgPack.Serialization;


namespace ConsoleApplicationTestCs
{
class Program
{
    static void Main(string[] args)
    {

    MyClass source = new MyClass();
    source.MyCustomList.Add(1);
    source.MyCustomList.Add(null);

    var context = new SerializationContext { SerializationMethod = SerializationMethod.Map };

    //context.DictionarySerializationOptions.OmitNullEntry = true;

    //Create serializers
    var serializer = SerializationContext.Default.GetSerializer<MyClass>(context);
    var serializerDest = SerializationContext.Default.GetSerializer<MyClass>(context);

    Stream stream = new MemoryStream();
    serializer.Pack(stream, source);
    stream.Position = 0;
    var unpackedObject = serializerDest.Unpack(stream);

}


}

public class MyClass
{
    public MyClass()
    {
        MyCustomList = new List<int?>();
    }
    public List<int?> MyCustomList { get; private set; }
}

}

Botond Bertalan
  • 370
  • 2
  • 8