Skip to content

[MessagePackRuntimeType] error #137

@nuverian

Description

@nuverian

Hello,

I have an issue with [MessagePackRuntimeType] getting a null reference exception, but I can't seem to figure out if it's a bug or I am doing something wrong. Here are the example classes:

public class MsgPackTest : MonoBehaviour {

    public SerializationData data;
    public byte[] bytes;

    void Start(){

        data.animal = new Cat();

        var stream = new MemoryStream();
        var serializer = SerializationContext.Default.GetSerializer<SerializationData>();
        serializer.Pack(stream, data);
        bytes = stream.ToArray();
    }

    void Update(){
        if (Input.GetKeyDown(KeyCode.Space)){
            Deserialize();
        }
    }

    void Deserialize(){
        var stream = new MemoryStream(bytes);
        var serializer = SerializationContext.Default.GetSerializer<SerializationData>();
        data = (SerializationData)serializer.Unpack(stream);
    }
}
[System.Serializable]
public class SerializationData {

    [MessagePackRuntimeType]
    public Animal animal;
}

public class Animal{
    public string name;
}

public class Cat : Animal{
    public string race;
}

And the error:

NullReferenceException: Object reference not set to an instance of an object
MsgPack.Serialization.Polymorphic.TypeInfoEncoder.Encode (MsgPack.Packer packer, System.Type type)
MsgPack.Serialization.Polymorphic.TypeEmbedingPolymorphicMessagePackSerializer`1[Animal].PackToCore (MsgPack.Packer packer, .Animal objectTree)
MsgPack.Serialization.MessagePackSerializer`1[Animal].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree)
MsgPack.Serialization.TypedMessagePackSerializerWrapper`1[Animal].PackToCore (MsgPack.Packer packer, .Animal objectTree)
MsgPack.Serialization.MessagePackSerializer`1[Animal].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree)
MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[SerializationData].PackMemberValue (MsgPack.Packer packer, .SerializationData objectTree, Int32 index)
MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[SerializationData].PackToCore (MsgPack.Packer packer, .SerializationData objectTree)
MsgPack.Serialization.MessagePackSerializer`1[SerializationData].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree)
MsgPack.Serialization.TypedMessagePackSerializerWrapper`1[SerializationData].PackToCore (MsgPack.Packer packer, .SerializationData objectTree)
MsgPack.Serialization.MessagePackSerializer`1[SerializationData].PackTo (MsgPack.Packer packer, .SerializationData objectTree)
MsgPack.Serialization.MessagePackSerializer`1[SerializationData].Pack (System.IO.Stream stream, .SerializationData objectTree)
MsgPackTest.Start () (at Assets/SerializationTesting/MsgPackTest.cs:21)

If I use [MessagePackKnownType] instead, it works fine, but in practise I don't know the types, thus I need [MessagePackRuntimeType] :)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDetected as bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions