Skip to content

Commit

Permalink
#75 improved the serialization/deserialization exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
linvi committed Sep 8, 2016
1 parent ecd1070 commit fd81939
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions Tweetinvi/JsonExtensions.cs → Tweetinvi/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

namespace Tweetinvi
{
public static class JsonExtensions
public static class JsonSerializer
{
private interface IJsonSerializer
{
object GetSerializableObject(object source);
object GetDeserializedObject(string json);
}

private class JsonSerializer<T1, T2> : IJsonSerializer
private class InternalJsonSerializer<T1, T2> : IJsonSerializer
where T1 : class
where T2 : class
{
private readonly Func<T1, T2> _getSerializableObject;
private readonly Func<string, T1> _deserializer;

public JsonSerializer(Func<T1, T2> getSerializableObject, Func<string, T1> deserializer)
public InternalJsonSerializer(Func<T1, T2> getSerializableObject, Func<string, T1> deserializer)
{
_getSerializableObject = getSerializableObject;
_deserializer = deserializer;
Expand All @@ -49,7 +49,7 @@ public object GetDeserializedObject(string json)
private static readonly Dictionary<Type, IJsonSerializer> _getSerializableObject;
private static Dictionary<Type, IJsonSerializer> _getFromDeserializeObject;

static JsonExtensions()
static JsonSerializer()
{
_jsonConvert = typeof(JsonConvert)
.GetMethods()
Expand All @@ -76,7 +76,7 @@ static JsonExtensions()
_getSerializableObject = new Dictionary<Type, IJsonSerializer>();

// ReSharper disable RedundantTypeArgumentsOfMethod
//Map<ITweet, ITweetDTO>(u => u.TweetDTO, tweetFactory.GenerateTweetFromJson);
Map<ITweet, ITweetDTO>(u => u.TweetDTO, tweetFactory.GenerateTweetFromJson);
Map<IUser, IUserDTO>(u => u.UserDTO, userFactory.GenerateUserFromJson);
Map<IMessage, IMessageDTO>(m => m.MessageDTO, messageFactory.GenerateMessageFromJson);
Map<ITwitterList, ITwitterListDTO>(l => l.TwitterListDTO, twitterListFactory.GenerateListFromJson);
Expand All @@ -96,13 +96,13 @@ public static string ToJson<T>(this T obj) where T : class

public static string ToJson<T1, T2>(this T1 obj, Func<T1, T2> getSerializableObject) where T1 : class where T2 : class
{
var serializer = new JsonSerializer<T1, T2>(getSerializableObject, null);
var serializer = new InternalJsonSerializer<T1, T2>(getSerializableObject, null);
return ToJson(obj, serializer);
}

public static string ToJson<T, T1, T2>(this T obj, Func<T1, T2> getSerializableObject) where T1 : class where T2 : class
{
var serializer = new JsonSerializer<T1, T2>(getSerializableObject, null);
var serializer = new InternalJsonSerializer<T1, T2>(getSerializableObject, null);
return ToJson(obj, serializer);
}

Expand Down Expand Up @@ -156,7 +156,11 @@ private static string ToJson<T>(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
);
}
}

Expand All @@ -169,7 +173,7 @@ public static T ConvertJsonTo<T>(this string json) where T : class

public static T1 ConvertJsonTo<T1, T2>(this string json, Func<string, T2> deserialize) where T1 : class where T2 : class
{
var serializer = new JsonSerializer<T2, object>(null, deserialize);
var serializer = new InternalJsonSerializer<T2, object>(null, deserialize);
return ConvertJsonTo<T1>(json, serializer);
}

Expand Down Expand Up @@ -227,13 +231,15 @@ private static T ConvertJsonTo<T>(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);
Expand Down Expand Up @@ -286,11 +292,11 @@ public static void Map<T1, T2>(Func<T1, T2> getSerializableObject, Func<string,
{
if (_getSerializableObject.ContainsKey(typeof(T1)))
{
_getSerializableObject[typeof(T1)] = new JsonSerializer<T1, T2>(getSerializableObject, deserialize);
_getSerializableObject[typeof(T1)] = new InternalJsonSerializer<T1, T2>(getSerializableObject, deserialize);
}
else
{
_getSerializableObject.Add(typeof(T1), new JsonSerializer<T1, T2>(getSerializableObject, deserialize));
_getSerializableObject.Add(typeof(T1), new InternalJsonSerializer<T1, T2>(getSerializableObject, deserialize));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tweetinvi/Tweetinvi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<Compile Include="Injectinvi\AutofacContainer.cs" />
<Compile Include="Injectinvi\AutofacThreadContainer.cs" />
<Compile Include="Injectinvi\OverridableContainer.cs" />
<Compile Include="JsonExtensions.cs" />
<Compile Include="JsonSerializer.cs" />
<Compile Include="Json\AccountJson.cs" />
<Compile Include="Json\FriendshipJson.cs" />
<Compile Include="Json\GeoJson.cs" />
Expand Down

0 comments on commit fd81939

Please sign in to comment.