fastJSON 反序列化列表

fastJSON Deserialization List

这是我之前的一个问题的基础。我正在尝试保存一个蓝图,它只是 gameobject/Entity 的一堆设置。我现在将组件(及其设置)存储为一个 List < IEntityComponent >(IEntityComponent 是任何组件的接口)包裹在一个名为 ComponentTable 的 class 中。我只想序列化列表,所有私有的东西都没有序列化,只是为了更快地查找(以内存为代价)。这可以正确序列化,甚至反序列化也没有任何错误,但我注意到组件Table 没有正确反序列化。

它创建了 ComponentTable 的一个实例,但实际上从未向其添加值。因此,它不是一个包含 CameraComponent、VelocityComponent 和 InputComponent 的 Component table,而是一个空的 ComponentTable.

{
 "$types" : {
  "ECS.Framework.Collections.Blueprint, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "1",
  "ECS.Features.Core.CameraComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "2",
  "ECS.Features.Core.VelocityComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "3",
  "InputComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "4"
 },
 "$type" : "1",
 "Components" : [
  {
     "$type" : "2",
     "Tag" : "MainCamera",
     "Test" : "0, 0, 0",
     "BackgroundColour" : "0, 0, 1, 1",
     "ViewportRect" : "10, 10 : 10, 10",
     "Orthographic" : false,
     "FieldOfView" : 60,
     "OrthoSize" : 5,
     "Depth" : 0,
     "OcclusionCulling" : true,
     "HDR" : false,
     "Enabled" : true
  },
  {
     "$type" : "3",
     "Enabled" : true,
     "CurrentVelocity" : "0, 0, 0"
  },
  {
     "$type" : "4",
     "TEST" : 0,
     "Enabled" : true
  }
 ],
 "Children" : [

 ],
 "Parent" : ""
}

是这样保存的,看来保存正确。我只控制 Vectors、Rect 和颜色的 serialization/serialization,因为任何单位值类型都会导致错误。

我相信它正在正确序列化,但由于某种原因它没有反序列化到组件中Table。有谁知道 fastJSON 是否有这种继承问题(使 class 从 List< customClass > 继承?

理想情况下,我希望它继承为 Dictionary,但 fastJSON 不会序列化该类型,只是将其保存为 'System.Mono' 然后在序列化时导致错误。

编辑:这是蓝图和组件 Table classes

public sealed class Blueprint 
{
    public ComponentTable Components { get; private set; }

    public List<string> Children { get; set; }

    public string Parent { get; set; }

    public Blueprint()
    {
        Components = new ComponentTable();

        Children = new List<string>();
        Parent = "";
    }

    public Blueprint(Blueprint _blueprint)
    {
        Children = new List<string>(_blueprint.Children);

        Parent = _blueprint.Parent;
    }
}


public class ComponentTable : List<IEntityComponent>
{
    private Dictionary<Type, IEntityComponent> Components { get; set; }

    #region Constructors

    public ComponentTable()
    {
        Components = new Dictionary<Type, IEntityComponent>();
    }

    #endregion

    #region Base Function Overrides

    public void Add(Type _type)
    {
        if (Components.ContainsKey(_type))
            return;

        InternalAdd(_type, (IEntityComponent)Activator.CreateInstance(_type));
    }
    public new void Add(IEntityComponent _component)
    {
        InternalAdd(_component.GetType(), _component);
    }
    public void Add<T>() where T : IEntityComponent
    {
        Add(typeof(T));
    }
    private void InternalAdd(Type _type, IEntityComponent _component)
    {
        if (Components.ContainsKey(_type))
            throw new InvalidOperationException("Component already contained");

        Components.Add(_type, _component);
        base.Add(_component);
    }

    public bool Remove(Type _type)
    {
        if (Components.ContainsKey(_type))
            return InternalRemove(_type, Components[_type]);
        return false;
    }
    public new bool Remove(IEntityComponent _component)
    {
        return InternalRemove(_component.GetType(), _component);
    }
    public bool Remove<T>() where T : IEntityComponent
    {
        return Remove(typeof(T));
    }
    private bool InternalRemove(Type _type, IEntityComponent _component)
    {
        if (!Components.ContainsKey(_type))
            return false;

        Components.Remove(_type);
        return base.Remove(_component);
    }

    public IEntityComponent Get(Type _type)
    {
        if (Contains(_type))
            return Components[_type];
        return null;
    }
    public T Get<T>() where T : IEntityComponent
    {
        return (T)Get(typeof(T));
    }

    public bool TryGetValue(Type _type, out IEntityComponent _component)
    {
        return Components.TryGetValue(_type, out _component);
    }
    public bool TryGetValue<T>(out IEntityComponent _component) where T : IEntityComponent
    {
        return TryGetValue(typeof(T), out _component);
    }

    public bool Contains(Type _type)
    {
        return Components.ContainsKey(_type);
    }
    public new bool Contains(IEntityComponent _component)
    {
        return Contains(_component.GetType());
    }
    public bool Contains<T>() where T : IEntityComponent
    {
        return Contains(typeof(T));
    }

    #endregion

}

我在 Microsoft .Net 上稍微测试了一下,发现了以下问题:

  1. fastJSON 不会反序列化 Components 属性 除非它有 public setter:

    public sealed class Blueprint 
    {
        public ComponentTable Components { get; set; }
    

    似乎没有任何类型的配置选项可以解决这个问题。从 Reflection.cs 你可以看到创建 setter 委托的方法 returns null 如果 setter 不是 public:

    internal static GenericSetter CreateSetMethod(Type type, PropertyInfo propertyInfo)
    {
        MethodInfo setMethod = propertyInfo.GetSetMethod();
        if (setMethod == null)
            return null;
    
  2. fastJSON 确实似乎无法反序列化 List<T> 的子 class——或任何其他 non-array 集合 class—— 不是通用的 。在 deserializer 中有以下检查:

    if (pi.IsGenericType && pi.IsValueType == false && v is List<object>)
        oset = CreateGenericList((List<object>)v, pi.pt, pi.bt, globaltypes);
    

    如您所见,它检查目标类型是否为泛型,而不是目标类型或其基类型之一是否为泛型。

    您可以通过使 ComponentTable 通用来解决此问题:

    public class ComponentTable<TEntityComponent> : List<TEntityComponent> where TEntityComponent : IEntityComponent
    {
        private Dictionary<Type, TEntityComponent> Components { get; set; }
    
        #region Constructors
    
        public ComponentTable()
        {
            Components = new Dictionary<Type, TEntityComponent>();
        }
    
        #endregion
    
        #region Base Function Overrides
    
        public void Add(Type _type)
        {
            if (Components.ContainsKey(_type))
                return;
    
            InternalAdd(_type, (TEntityComponent)Activator.CreateInstance(_type));
        }
        public new void Add(TEntityComponent _component)
        {
            InternalAdd(_component.GetType(), _component);
        }
        public void Add<T>() where T : IEntityComponent
        {
            Add(typeof(T));
        }
        private void InternalAdd(Type _type, TEntityComponent _component)
        {
            if (Components.ContainsKey(_type))
                throw new InvalidOperationException("Component already contained");
    
            Components.Add(_type, _component);
            base.Add(_component);
        }
    
        public bool Remove(Type _type)
        {
            if (Components.ContainsKey(_type))
                return InternalRemove(_type, Components[_type]);
            return false;
        }
        public new bool Remove(TEntityComponent _component)
        {
            return InternalRemove(_component.GetType(), _component);
        }
        public bool Remove<T>() where T : IEntityComponent
        {
            return Remove(typeof(T));
        }
        private bool InternalRemove(Type _type, TEntityComponent _component)
        {
            if (!Components.ContainsKey(_type))
                return false;
    
            Components.Remove(_type);
            return base.Remove(_component);
        }
    
        public IEntityComponent Get(Type _type)
        {
            if (Contains(_type))
                return Components[_type];
            return null;
        }
        public T Get<T>() where T : IEntityComponent
        {
            return (T)Get(typeof(T));
        }
    
        public bool TryGetValue(Type _type, out TEntityComponent _component)
        {
            return Components.TryGetValue(_type, out _component);
        }
        public bool TryGetValue<T>(out TEntityComponent _component) where T : IEntityComponent
        {
            return TryGetValue(typeof(T), out _component);
        }
    
        public bool Contains(Type _type)
        {
            return Components.ContainsKey(_type);
        }
        public new bool Contains(TEntityComponent _component)
        {
            return Contains(_component.GetType());
        }
        public bool Contains<T>() where T : IEntityComponent
        {
            return Contains(typeof(T));
        }
    
        #endregion
    }
    

    然后将Blueprint改为:

    public sealed class Blueprint
    {
        public ComponentTable<IEntityComponent> Components { get; set; }
    

    并且列表内容将被反序列化。然而...

  3. 您的 ComponentTable 继承自 List<T>,需要覆盖 Add()。但是 Add() 不是虚拟的,因此您使用 public new Add() 代替。这里的问题是,如果有人将您的 class 转换为 List<T> 并调用 Add(),您的方法将不会被调用。特别是fastJSON在反序列化的时候是不会调用的!所以你的类型字典在反序列化过程中永远不会被初始化,违背了目的。

    您所做的似乎是 re-inventing KeyedByTypeCollection<TItem>。与其那样做,不如直接使用它。使用这个 class,你的 ComponentTable 变得非常简单:

    public class ComponentTable<TEntityComponent> : KeyedByTypeCollection<TEntityComponent> where TEntityComponent : IEntityComponent
    {
        public void Add(Type _type)
        {
            if (Contains(_type))
                return;
            Add((TEntityComponent)Activator.CreateInstance(_type));
        }
    
        public void Add<T>() where T : IEntityComponent, new()
        {
            Add(typeof(T));
        }
    }
    

    现在可以对集合进行序列化和反序列化,而不会在 Microsoft .Net 上损坏数据。然而...

  4. 我不确定 KeyedByTypeCollection<TItem> 是否存在于统一体中。如果没有,您可能需要移植它。见参考来源KeyedByTypeCollection.cs and the base class source keyedcollection.cs. One attempt to do it can be found here: Alternative to KeyedByTypeCollection in Mono .Net.

  5. 作为替代方案,您可以考虑使用 Json.NET。 Json.NET 支持通过 TypeNameHandling setting 序列化多态类型。但是,它可以统一使用吗?

    Google suggests that there are some ports for unity. There are also a variety of questions here tagged with both Json.NET and unity3d, for instance 。因此,您可以进一步调查该选项。