Skip to content

Commit

Permalink
SlackTypeConverter logs to ILogger instead of Console
Browse files Browse the repository at this point in the history
  • Loading branch information
soxtoby committed Aug 10, 2023
1 parent 0a5c2f2 commit ef350c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion SlackNet/Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static JsonSerializerSettings SerializerSettings(ISlackTypeResolver slac
{
new EnumNameConverter(namingStrategy),
new TimeSpanConverter(),
new SlackTypeConverter(slackTypeResolver)
new SlackTypeConverter(slackTypeResolver, logger)
}
};
}
Expand Down
44 changes: 22 additions & 22 deletions SlackNet/Serialization/SlackTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace SlackNet;
class SlackTypeConverter : JsonConverter
{
private readonly ISlackTypeResolver _slackTypeResolver;
private readonly ILogger _log;
[ThreadStatic] private static bool _isInsideRead;
[ThreadStatic] private static JsonReader _reader;

public SlackTypeConverter(ISlackTypeResolver slackTypeResolver)
public SlackTypeConverter(ISlackTypeResolver slackTypeResolver, ILogger logger)
{
_slackTypeResolver = slackTypeResolver;
_log = logger.ForSource<SlackTypeConverter>();
}

public override bool CanRead => !_isInsideRead || !string.IsNullOrEmpty(_reader.Path);
Expand All @@ -35,17 +37,13 @@ private object ReadJson(JsonReader reader, Type objectType, JsonSerializer seria
while (reader.TokenType == JsonToken.Comment)
reader.Read();

switch (reader.TokenType)
{
case JsonToken.Null:
return null;
case JsonToken.StartObject:
return ReadObject(reader, objectType, serializer);
case JsonToken.StartArray:
return ReadArray(reader, objectType, serializer);
default:
return ReadInner(CreateAnotherReader(JToken.Load(reader), reader), objectType, serializer);
}
return reader.TokenType switch
{
JsonToken.Null => null,
JsonToken.StartObject => ReadObject(reader, objectType, serializer),
JsonToken.StartArray => ReadArray(reader, objectType, serializer),
_ => ReadInner(CreateAnotherReader(JToken.Load(reader), reader), objectType, serializer)
};
}

private IList ReadArray(JsonReader reader, Type targetType, JsonSerializer serializer)
Expand All @@ -56,9 +54,10 @@ private IList ReadArray(JsonReader reader, Type targetType, JsonSerializer seria
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
list.Add(ReadJson(reader, elementType, serializer));

if (!targetType.IsArray) return list;
if (!targetType.IsArray)
return list;

var array = Array.CreateInstance(targetType.GetElementType(), list.Count);
var array = Array.CreateInstance(targetType.GetElementType()!, list.Count);
list.CopyTo(array, 0);

return array;
Expand Down Expand Up @@ -96,7 +95,8 @@ private static JsonReader CreateAnotherReader(JToken jObject, JsonReader reader)

private Type GetType(JToken jObject, Type parentType)
{
if (jObject.Value<uint>("reply_to") > 0) return typeof(Reply);
if (jObject.Value<uint>("reply_to") > 0)
return typeof(Reply);

var type = GetType(jObject, "type", parentType);
return GetType(jObject, "subtype", type);
Expand All @@ -105,10 +105,12 @@ private Type GetType(JToken jObject, Type parentType)
private Type GetType(JToken jObject, string typeProperty, Type baseType)
{
var slackType = jObject.Value<string>(typeProperty);
return slackType == null ? baseType : _slackTypeResolver.FindType(baseType, slackType);
return slackType == null
? baseType
: _slackTypeResolver.FindType(baseType, slackType);
}

private static object ReadInner(JsonReader reader, Type objectType, JsonSerializer serializer)
private object ReadInner(JsonReader reader, Type objectType, JsonSerializer serializer)
{
_reader = reader;
_isInsideRead = true;
Expand All @@ -118,7 +120,7 @@ private static object ReadInner(JsonReader reader, Type objectType, JsonSerializ
}
catch (Exception ex)
{
Console.Write($"Couldn't deserialize, fallback to default value. " + ex.Source);
_log.Serialization(ex, "Error deserializing to {ObjectType}, falling back to default value", objectType.FullName);
return DefaultValue(objectType);
}
finally
Expand All @@ -127,10 +129,8 @@ private static object ReadInner(JsonReader reader, Type objectType, JsonSerializ
}
}

private static object DefaultValue(Type type)
{
return type.GetTypeInfo().IsValueType
private static object DefaultValue(Type type) =>
type.GetTypeInfo().IsValueType
? Activator.CreateInstance(type)
: null;
}
}

0 comments on commit ef350c3

Please sign in to comment.