WIP
parent
53d1138cb9
commit
1557eef64c
|
@ -0,0 +1,10 @@
|
||||||
|
using System;
|
||||||
|
namespace ln.json.attributes
|
||||||
|
{
|
||||||
|
public class ReflectedAttribute : Attribute
|
||||||
|
{
|
||||||
|
public ReflectedAttribute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -56,6 +56,7 @@
|
||||||
<Compile Include="mapping\JSONDictionaryMapping.cs" />
|
<Compile Include="mapping\JSONDictionaryMapping.cs" />
|
||||||
<Compile Include="mapping\JSONExceptionMapping.cs" />
|
<Compile Include="mapping\JSONExceptionMapping.cs" />
|
||||||
<Compile Include="mapping\JSONIPv6Mapping.cs" />
|
<Compile Include="mapping\JSONIPv6Mapping.cs" />
|
||||||
|
<Compile Include="attributes\ReflectedAttribute.cs" />
|
||||||
<Compile Include="reflection\ObjectPoolContainerSerializer.cs" />
|
<Compile Include="reflection\ObjectPoolContainerSerializer.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -17,9 +17,9 @@ namespace ln.json.mapping
|
||||||
Type elementType;
|
Type elementType;
|
||||||
|
|
||||||
public JSONEnumerableMapping()
|
public JSONEnumerableMapping()
|
||||||
: base(typeof(T))
|
: base(typeof(IEnumerable<T>))
|
||||||
{
|
{
|
||||||
elementType = typeof(T).GetGenericArguments()[0];
|
elementType = typeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object FromJson(JSONMapper mapper, JSONValue json)
|
public override object FromJson(JSONMapper mapper, JSONValue json)
|
||||||
|
|
|
@ -12,6 +12,7 @@ using System.Reflection;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Diagnostics.Tracing;
|
using System.Diagnostics.Tracing;
|
||||||
|
using ln.types.reflection;
|
||||||
namespace ln.json.mapping
|
namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
public class JSONMapper
|
public class JSONMapper
|
||||||
|
@ -149,9 +150,12 @@ namespace ln.json.mapping
|
||||||
else if (targetType.IsGenericType)
|
else if (targetType.IsGenericType)
|
||||||
{
|
{
|
||||||
Type genericTypeDefinition = targetType.GetGenericTypeDefinition();
|
Type genericTypeDefinition = targetType.GetGenericTypeDefinition();
|
||||||
if (genericTypeDefinition.Equals(typeof(IEnumerable<>)))
|
if (targetType.HasGenericInterface(typeof(IEnumerable<>)))
|
||||||
{
|
{
|
||||||
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
targetType = targetType.GetGenericInterface(typeof(IEnumerable<>));
|
||||||
|
if (!mappings.ContainsKey(targetType))
|
||||||
|
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (genericTypeDefinition.Equals(typeof(Dictionary<,>)))
|
else if (genericTypeDefinition.Equals(typeof(Dictionary<,>)))
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using ln.types.reflection;
|
using ln.types.reflection;
|
||||||
using ln.json.mapping;
|
using ln.json.mapping;
|
||||||
|
using ln.types;
|
||||||
namespace ln.json.reflection
|
namespace ln.json.reflection
|
||||||
{
|
{
|
||||||
public class ObjectPoolContainerSerializer
|
public class ObjectPoolContainerSerializer
|
||||||
|
@ -20,9 +21,9 @@ namespace ln.json.reflection
|
||||||
{
|
{
|
||||||
JSONArray objectList = new JSONArray();
|
JSONArray objectList = new JSONArray();
|
||||||
|
|
||||||
foreach (object o in objectPool.Instances)
|
foreach (object o in objectPool.OwnInstances)
|
||||||
{
|
{
|
||||||
objectList.Add(JSONMapper.DefaultMapper.ToJson(o));
|
objectList.Add(SerializeObject(objectPool, o));
|
||||||
}
|
}
|
||||||
|
|
||||||
serializedPoolContainer.Add(objectPool.ObjectType.Name, objectList);
|
serializedPoolContainer.Add(objectPool.ObjectType.Name, objectList);
|
||||||
|
@ -31,6 +32,26 @@ namespace ln.json.reflection
|
||||||
return serializedPoolContainer;
|
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)
|
public void Unserialize(JSONObject serializedPoolContainer)
|
||||||
{
|
{
|
||||||
foreach (ObjectPool objectPool in ObjectPoolContainer.Pools)
|
foreach (ObjectPool objectPool in ObjectPoolContainer.Pools)
|
||||||
|
@ -40,10 +61,49 @@ namespace ln.json.reflection
|
||||||
JSONArray serializedObjects = serializedPoolContainer[objectPool.ObjectType.Name] as JSONArray;
|
JSONArray serializedObjects = serializedPoolContainer[objectPool.ObjectType.Name] as JSONArray;
|
||||||
foreach (JSONObject serializedObject in serializedObjects.Children)
|
foreach (JSONObject serializedObject in serializedObjects.Children)
|
||||||
{
|
{
|
||||||
objectPool.Add(JSONMapper.DefaultMapper.FromJson(serializedObject, objectPool.ObjectType));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue