From fd81939259cff7f489ebb48da83b998218bfd909 Mon Sep 17 00:00:00 2001 From: linvi Date: Thu, 8 Sep 2016 10:37:04 +0100 Subject: [PATCH] #75 improved the serialization/deserialization exception message --- .../{JsonExtensions.cs => JsonSerializer.cs} | 34 +++++++++++-------- Tweetinvi/Tweetinvi.csproj | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) rename Tweetinvi/{JsonExtensions.cs => JsonSerializer.cs} (87%) diff --git a/Tweetinvi/JsonExtensions.cs b/Tweetinvi/JsonSerializer.cs similarity index 87% rename from Tweetinvi/JsonExtensions.cs rename to Tweetinvi/JsonSerializer.cs index 01f3d7642..d01999dc7 100644 --- a/Tweetinvi/JsonExtensions.cs +++ b/Tweetinvi/JsonSerializer.cs @@ -13,7 +13,7 @@ namespace Tweetinvi { - public static class JsonExtensions + public static class JsonSerializer { private interface IJsonSerializer { @@ -21,14 +21,14 @@ private interface IJsonSerializer object GetDeserializedObject(string json); } - private class JsonSerializer : IJsonSerializer + private class InternalJsonSerializer : IJsonSerializer where T1 : class where T2 : class { private readonly Func _getSerializableObject; private readonly Func _deserializer; - public JsonSerializer(Func getSerializableObject, Func deserializer) + public InternalJsonSerializer(Func getSerializableObject, Func deserializer) { _getSerializableObject = getSerializableObject; _deserializer = deserializer; @@ -49,7 +49,7 @@ public object GetDeserializedObject(string json) private static readonly Dictionary _getSerializableObject; private static Dictionary _getFromDeserializeObject; - static JsonExtensions() + static JsonSerializer() { _jsonConvert = typeof(JsonConvert) .GetMethods() @@ -76,7 +76,7 @@ static JsonExtensions() _getSerializableObject = new Dictionary(); // ReSharper disable RedundantTypeArgumentsOfMethod - //Map(u => u.TweetDTO, tweetFactory.GenerateTweetFromJson); + Map(u => u.TweetDTO, tweetFactory.GenerateTweetFromJson); Map(u => u.UserDTO, userFactory.GenerateUserFromJson); Map(m => m.MessageDTO, messageFactory.GenerateMessageFromJson); Map(l => l.TwitterListDTO, twitterListFactory.GenerateListFromJson); @@ -96,13 +96,13 @@ public static string ToJson(this T obj) where T : class public static string ToJson(this T1 obj, Func getSerializableObject) where T1 : class where T2 : class { - var serializer = new JsonSerializer(getSerializableObject, null); + var serializer = new InternalJsonSerializer(getSerializableObject, null); return ToJson(obj, serializer); } public static string ToJson(this T obj, Func getSerializableObject) where T1 : class where T2 : class { - var serializer = new JsonSerializer(getSerializableObject, null); + var serializer = new InternalJsonSerializer(getSerializableObject, null); return ToJson(obj, serializer); } @@ -156,7 +156,11 @@ private static string ToJson(T obj, IJsonSerializer serializer) } catch (Exception ex) { - throw new Exception("The type provided is probably not compatible with Tweetinvi Json serializer.", ex); + throw new Exception( + "The type provided is probably not compatible with Tweetinvi Json serializer." + + "If you think this class should be serializable by default please report on github.com/linvi/tweetinvi.", + ex + ); } } @@ -169,7 +173,7 @@ public static T ConvertJsonTo(this string json) where T : class public static T1 ConvertJsonTo(this string json, Func deserialize) where T1 : class where T2 : class { - var serializer = new JsonSerializer(null, deserialize); + var serializer = new InternalJsonSerializer(null, deserialize); return ConvertJsonTo(json, serializer); } @@ -227,13 +231,15 @@ private static T ConvertJsonTo(this string json, IJsonSerializer serializer) } catch (Exception ex) { - throw new Exception("The type provided is probably not compatible with Tweetinvi Json serializer.", ex); + throw new Exception( + "The type provided is probably not compatible with Tweetinvi Json serializer." + + "If you think this class should be deserializable by default please report on github.com/linvi/tweetinvi.", + ex + ); } } - - private static IJsonSerializer GetSerializer(Type type) { var serializer = GetSerializerFromNonCollectionType(type); @@ -286,11 +292,11 @@ public static void Map(Func getSerializableObject, Func(getSerializableObject, deserialize); + _getSerializableObject[typeof(T1)] = new InternalJsonSerializer(getSerializableObject, deserialize); } else { - _getSerializableObject.Add(typeof(T1), new JsonSerializer(getSerializableObject, deserialize)); + _getSerializableObject.Add(typeof(T1), new InternalJsonSerializer(getSerializableObject, deserialize)); } } } diff --git a/Tweetinvi/Tweetinvi.csproj b/Tweetinvi/Tweetinvi.csproj index 34044017b..f69ff1e23 100644 --- a/Tweetinvi/Tweetinvi.csproj +++ b/Tweetinvi/Tweetinvi.csproj @@ -55,7 +55,7 @@ - +