forked from ln-dotnet/ln.json
Add support for JSONExceptionMapping
parent
1a24ea939e
commit
28446ab628
|
@ -54,6 +54,7 @@
|
|||
<Compile Include="attributes\JSONMappingAttribute.cs" />
|
||||
<Compile Include="mapping\JSONEnumMapping.cs" />
|
||||
<Compile Include="mapping\JSONDictionaryMapping.cs" />
|
||||
<Compile Include="mapping\JSONExceptionMapping.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="mapping\" />
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONExceptionMapping : JSONMapping
|
||||
{
|
||||
public JSONExceptionMapping()
|
||||
: base(typeof(Exception))
|
||||
{
|
||||
}
|
||||
public JSONExceptionMapping(Type exceptionType)
|
||||
: base(exceptionType)
|
||||
{
|
||||
}
|
||||
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value)
|
||||
{
|
||||
JSONObject jObject = new JSONObject();
|
||||
Exception e = value as Exception;
|
||||
|
||||
jObject["ExceptionType"] = mapper.ToJson(value.GetType().Name);
|
||||
jObject["Message"] = mapper.ToJson(e.Message);
|
||||
jObject["InnerException"] = mapper.ToJson(e.InnerException);
|
||||
|
||||
return jObject;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -132,13 +132,18 @@ namespace ln.json.mapping
|
|||
{
|
||||
Add(new JSONEnumMapping(targetType));
|
||||
}
|
||||
else if (targetType.IsSubclassOf(typeof(Exception)))
|
||||
{
|
||||
Add(new JSONExceptionMapping(targetType));
|
||||
}
|
||||
else if (targetType.IsGenericType)
|
||||
{
|
||||
Type genericTypeDefinition = targetType.GetGenericTypeDefinition();
|
||||
if (genericTypeDefinition.Equals(typeof(IEnumerable<>)))
|
||||
{
|
||||
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
||||
} else if (genericTypeDefinition.Equals(typeof(Dictionary<,>)))
|
||||
}
|
||||
else if (genericTypeDefinition.Equals(typeof(Dictionary<,>)))
|
||||
{
|
||||
Add((JSONMapping)Activator.CreateInstance(typeof(JSONDictionaryMapping<,>).MakeGenericType(targetType.GetGenericArguments())));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue