110 lines
4.0 KiB
C#
110 lines
4.0 KiB
C#
using System;
|
|
using ln.types.reflection;
|
|
using ln.json.mapping;
|
|
using ln.types;
|
|
namespace ln.json.reflection
|
|
{
|
|
public class ObjectPoolContainerSerializer
|
|
{
|
|
public ObjectPoolContainer ObjectPoolContainer { get; }
|
|
|
|
public ObjectPoolContainerSerializer(ObjectPoolContainer objectPoolContainer)
|
|
{
|
|
ObjectPoolContainer = objectPoolContainer;
|
|
}
|
|
|
|
public JSONObject Serialize()
|
|
{
|
|
JSONObject serializedPoolContainer = new JSONObject();
|
|
|
|
foreach (ObjectPool objectPool in ObjectPoolContainer.Pools)
|
|
{
|
|
JSONArray objectList = new JSONArray();
|
|
|
|
foreach (object o in objectPool.OwnInstances)
|
|
{
|
|
objectList.Add(SerializeObject(objectPool, o));
|
|
}
|
|
|
|
serializedPoolContainer.Add(objectPool.ObjectType.Name, objectList);
|
|
}
|
|
|
|
return serializedPoolContainer;
|
|
}
|
|
|
|
public JSONObject SerializeObject(ObjectPool objectPool, object o)
|
|
{
|
|
TypeDescriptor typeDescriptor = objectPool.TypeDescriptor;
|
|
JSONObject jObject = new JSONObject();
|
|
|
|
jObject["__origin__"] = o.GetType().Name;
|
|
|
|
foreach (AttributeDescriptor attributeDescriptor in typeDescriptor.AttributeDescriptors)
|
|
{
|
|
object attributeValue = attributeDescriptor.GetValue(o);
|
|
if (ObjectPoolContainer.KnowsType(attributeDescriptor.AttributeType))
|
|
{
|
|
attributeValue = ObjectPoolContainer[attributeDescriptor.AttributeType].GetIdentity(attributeValue);
|
|
}
|
|
jObject.Add(attributeDescriptor.AttributeName, JSONMapper.DefaultMapper.ToJson(attributeValue));
|
|
}
|
|
return jObject;
|
|
}
|
|
|
|
|
|
public void Unserialize(JSONObject serializedPoolContainer)
|
|
{
|
|
foreach (ObjectPool objectPool in ObjectPoolContainer.Pools)
|
|
{
|
|
if (serializedPoolContainer.ContainsKey(objectPool.ObjectType.Name))
|
|
{
|
|
JSONArray serializedObjects = serializedPoolContainer[objectPool.ObjectType.Name] as JSONArray;
|
|
foreach (JSONObject serializedObject in serializedObjects.Children)
|
|
{
|
|
objectPool.Add(UnserializeObject(objectPool, serializedObject));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public object UnserializeObject(ObjectPool objectPool, JSONObject jObject)
|
|
{
|
|
object o = Activator.CreateInstance(objectPool.ObjectType);
|
|
|
|
UpdateObject(objectPool, o, jObject);
|
|
|
|
return o;
|
|
}
|
|
|
|
public void UpdateObject(ObjectPool objectPool, object o, JSONObject jObject)
|
|
{
|
|
TypeDescriptor typeDescriptor = objectPool.TypeDescriptor;
|
|
|
|
foreach (AttributeDescriptor attributeDescriptor in typeDescriptor.AttributeDescriptors)
|
|
{
|
|
if (jObject.ContainsKey(attributeDescriptor.AttributeName))
|
|
ApplyValue(objectPool, attributeDescriptor, o, jObject[attributeDescriptor.AttributeName]);
|
|
}
|
|
}
|
|
|
|
public void ApplyValue(ObjectPool objectPool, AttributeDescriptor attributeDescriptor, object o, JSONValue jValue)
|
|
{
|
|
object attributeValue;
|
|
|
|
if (ObjectPoolContainer.KnowsType(attributeDescriptor.AttributeType))
|
|
{
|
|
attributeValue = ObjectPoolContainer[attributeDescriptor.AttributeType][jValue.ToNative()];
|
|
}
|
|
else
|
|
{
|
|
attributeValue = JSONMapper.DefaultMapper.FromJson(jValue, attributeDescriptor.AttributeType);
|
|
}
|
|
|
|
if (!object.ReferenceEquals(null,attributeValue) && !object.Equals(attributeValue.GetType(), attributeDescriptor.AttributeType))
|
|
attributeValue = Cast.To(attributeValue, attributeDescriptor.AttributeType);
|
|
|
|
attributeDescriptor.SetValue(o, attributeValue);
|
|
}
|
|
}
|
|
}
|