Skip to content

InvalidCastException when deserializing array of complex class #9

@BreyerW

Description

@BreyerW

Recently i stumbled upon weird bug: i have a class Entity defined as follow:

public class Entity
    {
        private static int lastId = 0;
        internal Vector3 position = Vector3.Zero;

        public readonly int id;

        public Entity parent;
        public List<Entity> childs = new List<Entity>();
        public string name = "Entity Object";

        public Vector3 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
                OnTransformChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        internal Vector3 rotation = Vector3.Zero;

        public Vector3 Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                rotation = value;
                OnTransformChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        internal Vector3 scale = Vector3.One;

        public Vector3 Scale
        {
            get
            {
                return scale;
            }
            set
            {
                scale = value;
                OnTransformChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        public bool active
        {
            get { return enabled; }
            set
            {
                if (enabled == value)
                    return;
                enabled = value;
                //if (enabled)
                //OnEnableInternal ();
                //else
                //OnDisableInternal ();
            }
        }

        private bool enabled;
        private HashSet<int> tags = new HashSet<int>();

        //public
        private Matrix4x4 modelMatrix;

        public ref Matrix4x4 ModelMatrix
        {
            get { return ref modelMatrix; }
        }

        public EventHandler OnTransformChanged;

        private List<Component> components = new List<Component>();
}

and instance of Entity (no array) serialize and deserialize just fine. But as soon as i try to make array of it (any form of array including List<>, HashSet<>, mundane Entity[] etc.) crash with InvalidCastException in BJSON.cs line 622.

This happen with default state too. You can easily test it by copying this class and installing System.Numeric then writing simple Entity[] test = new[] { new Entity(), new Entity() }; and DeepCopy'ing this. EventHandler can be silenced with RegisterCustomType filled with dummy delegates.

Settings are default. Version of BJSON is 1.4.22

What is interesting is that JSON version works just fine for both lone instance and arrays....

BTW while you would be at it could you add license text? I requested it in May 2017 and still no license when its a matter of few clicks in github for any widely used licenses....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions