master
Harald Wolff 2020-03-03 17:05:31 +01:00
parent 53d1138cb9
commit 1557eef64c
5 changed files with 82 additions and 7 deletions

View File

@ -0,0 +1,10 @@
using System;
namespace ln.json.attributes
{
public class ReflectedAttribute : Attribute
{
public ReflectedAttribute()
{
}
}
}

View File

@ -56,6 +56,7 @@
<Compile Include="mapping\JSONDictionaryMapping.cs" />
<Compile Include="mapping\JSONExceptionMapping.cs" />
<Compile Include="mapping\JSONIPv6Mapping.cs" />
<Compile Include="attributes\ReflectedAttribute.cs" />
<Compile Include="reflection\ObjectPoolContainerSerializer.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -17,9 +17,9 @@ namespace ln.json.mapping
Type elementType;
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)

View File

@ -12,6 +12,7 @@ using System.Reflection;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics.Tracing;
using ln.types.reflection;
namespace ln.json.mapping
{
public class JSONMapper
@ -149,9 +150,12 @@ namespace ln.json.mapping
else if (targetType.IsGenericType)
{
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<,>)))
{

View File

@ -1,6 +1,7 @@
using System;
using ln.types.reflection;
using ln.json.mapping;
using ln.types;
namespace ln.json.reflection
{
public class ObjectPoolContainerSerializer
@ -20,9 +21,9 @@ namespace ln.json.reflection
{
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);
@ -31,6 +32,26 @@ namespace ln.json.reflection
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)
@ -40,10 +61,49 @@ namespace ln.json.reflection
JSONArray serializedObjects = serializedPoolContainer[objectPool.ObjectType.Name] as JSONArray;
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);
}
}
}