diff --git a/ln.json.csproj b/ln.json.csproj index 402e9c0..ce97897 100644 --- a/ln.json.csproj +++ b/ln.json.csproj @@ -54,6 +54,7 @@ + diff --git a/mapping/JSONExceptionMapping.cs b/mapping/JSONExceptionMapping.cs new file mode 100644 index 0000000..be964aa --- /dev/null +++ b/mapping/JSONExceptionMapping.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/mapping/JSONMapper.cs b/mapping/JSONMapper.cs index eecc3ff..e61cc5c 100644 --- a/mapping/JSONMapper.cs +++ b/mapping/JSONMapper.cs @@ -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()))); }