fastBinaryJSON icon indicating copy to clipboard operation
fastBinaryJSON copied to clipboard

InvalidCastException when deserializing array of complex class

Open BreyerW opened this issue 6 years ago • 1 comments

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....

BreyerW avatar Mar 28 '18 12:03 BreyerW

Extra data after debugging session:

  • it happen only when all those Vector fields have different values than default(Vector3) and serialized data is some form of array with more than 1 elements;
  • i found out that either generating _cirrev is bugged (probably doesnt grow properly with 2 or more elements) or parameter d from ParsedDictionary have incorrect data.

BreyerW avatar Apr 07 '18 21:04 BreyerW