From ef350c303db5ccae3ef68843fc4a918aa5700437 Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Fri, 11 Aug 2023 03:19:02 +1000 Subject: [PATCH] SlackTypeConverter logs to ILogger instead of Console --- SlackNet/Default.cs | 2 +- SlackNet/Serialization/SlackTypeConverter.cs | 44 ++++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/SlackNet/Default.cs b/SlackNet/Default.cs index ab932c28..567cc347 100644 --- a/SlackNet/Default.cs +++ b/SlackNet/Default.cs @@ -40,7 +40,7 @@ private static JsonSerializerSettings SerializerSettings(ISlackTypeResolver slac { new EnumNameConverter(namingStrategy), new TimeSpanConverter(), - new SlackTypeConverter(slackTypeResolver) + new SlackTypeConverter(slackTypeResolver, logger) } }; } diff --git a/SlackNet/Serialization/SlackTypeConverter.cs b/SlackNet/Serialization/SlackTypeConverter.cs index 99316f28..137da96b 100644 --- a/SlackNet/Serialization/SlackTypeConverter.cs +++ b/SlackNet/Serialization/SlackTypeConverter.cs @@ -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(); } public override bool CanRead => !_isInsideRead || !string.IsNullOrEmpty(_reader.Path); @@ -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) @@ -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; @@ -96,7 +95,8 @@ private static JsonReader CreateAnotherReader(JToken jObject, JsonReader reader) private Type GetType(JToken jObject, Type parentType) { - if (jObject.Value("reply_to") > 0) return typeof(Reply); + if (jObject.Value("reply_to") > 0) + return typeof(Reply); var type = GetType(jObject, "type", parentType); return GetType(jObject, "subtype", type); @@ -105,10 +105,12 @@ private Type GetType(JToken jObject, Type parentType) private Type GetType(JToken jObject, string typeProperty, Type baseType) { var slackType = jObject.Value(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; @@ -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 @@ -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; - } } \ No newline at end of file