C# MsgPack 在解包包含 NULL 的可空整数列表时抛出

C# MsgPack throws when unpack list of nullable integers containing a NULL as item

我想使用 MsgPack 而不是 Newtonsoft.JSON,因为它更快,但是我在尝试反序列化可为 null 的整数列表时遇到问题。

这是我正在使用的代码片段:

          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 unpacked value is not 'System.Int32' type. Do not convert nil MessagePackObject to System.Int32."}”的异常

我的'MyCustomList'属性是List类型的,不工作。如果我切换到 IList 就可以了

知道这是否是已知错误吗?我怎样才能摆脱它?

谢谢

我使用的是 MsgPack 版本 2.0.0-alpha2,它工作正常。 我唯一做的就是注释掉这一行,因为这个版本的 msgpack 无法识别它。

//context.DictionarySerializationOptions.OmitNullEntry = true;

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

}