從我以前的一個問題開始構建。我正在嘗試保存一個藍圖,這只是一堆遊戲對象/實體的設置。我現在將組件(和它們的設置)作爲列表存儲在一個名爲ComponentTable的類中,IEntityComponent>(IEntityComponent是任何組件的接口)。我只想序列化這個列表,並且所有私有的東西都不是序列化的,只是爲了加快查找速度(以內存爲代價)。這序列化正確,甚至反序列化沒有任何錯誤,但我注意到componentTable不正確反序列化。fastJSON反序列化列表
它創建一個ComponentTable的實例,但從未實際添加值到它。因此,而不是包含CameraComponent,VelocityComponent和InputComponent的Component表,它只是一個空的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" : ""
}
這是它如何保存,所以它看起來好像是正確保存。我只控制向量,矩形和顏色的序列化/序列化,因爲任何單值類型都會導致錯誤。
我相信它是正確的序列化,但由於某種原因它不是反序列化到componentTable中。有誰知道,如果FASTJSON有問題,這種繼承的(從列表中製作一個類繼承< customClass>?
理想我想有繼承它作爲一個字典<類型,IEntityComponent>,但FASTJSON不會序列化的類型,只是將其保存爲「System.Mono」序列化時則導致錯誤
編輯:這裏是
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
}
嚴重頂級回答10/10。你把比我想象的更多的細節放在那裏,非常感謝你。我沒有意識到類KeyedByTypeCollection甚至存在,我會嘗試改變它,看看它如何與fastJSON一起工作。我假設你嘗試使用fastJSON序列化它,它的工作?老實說,我可以使用不同的JSON,即使fastJSON不是統一的一部分,我不得不導入它,它恰好是我發現的第一個。加上它看起來很快,並有很多功能。但它仍然遇到像這樣的問題,所以我可能會改變它在 – Lolop
@Lolop - 我成功地在Microsoft.Net上成功使用fastJSON成功使'KeyedByTypeCollection'成功。我沒有團結,所以我無法在那裏測試。 –
dbc
你是上帝派的。我確實必須將這些類複製到一個統一體中,並修復那些與統一體無關的東西。唯一的問題是他們用來拋出錯誤,我不得不用他們的替換(希望不是我的損失),只是正常的例外。這是因爲(我假設)統一使用.Net 3.5,而他們在KeyedByType中使用的東西,它的基類是.Net 4.5+? (也可能是爲什麼它不是一致的開始)。再次感謝你,我可能永遠不會想到它。私人環境正在停止反序列化,但通用的東西正在打破它。 – Lolop