From 7955aefdfd61fcc28420e30640628890c15ba781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=88=E5=B0=8F=E8=8D=B7?= Date: Sun, 25 Dec 2022 22:49:25 +0800 Subject: [PATCH] Fixed incorrect json converter resulting in startup failure --- .../Net/Converters/NullableUInt32Converter.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Kook.Net.Rest/Net/Converters/NullableUInt32Converter.cs b/src/Kook.Net.Rest/Net/Converters/NullableUInt32Converter.cs index 02ad07c1..ef7df4c0 100644 --- a/src/Kook.Net.Rest/Net/Converters/NullableUInt32Converter.cs +++ b/src/Kook.Net.Rest/Net/Converters/NullableUInt32Converter.cs @@ -7,10 +7,18 @@ internal class NullableUInt32Converter : JsonConverter { public override uint? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - string value = reader.GetString(); - return !string.IsNullOrWhiteSpace(value) && uint.TryParse(value, out uint result) - ? result - : null; + switch (reader.TokenType) + { + case JsonTokenType.String: + string value = reader.GetString(); + return !string.IsNullOrWhiteSpace(value) && uint.TryParse(value, out uint result) + ? result + : null; + case JsonTokenType.Number: + return reader.GetUInt32(); + default: + throw new JsonException($"{nameof(NullableUInt32Converter)} expects string or number token, but got {reader.TokenType}"); + } } public override void Write(Utf8JsonWriter writer, uint? value, JsonSerializerOptions options)