From 26f250b0b36e7b2aaf8ba8f2f7c14266e20a148d Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Wed, 22 Nov 2023 20:40:42 -0500 Subject: [PATCH] Use NumberHandling in JsonSerializerOptions instead of LegacyStreamer --- TwitchDownloaderCore/Chat/ChatJson.cs | 18 +++--------------- TwitchDownloaderCore/TwitchObjects/ChatRoot.cs | 8 -------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/TwitchDownloaderCore/Chat/ChatJson.cs b/TwitchDownloaderCore/Chat/ChatJson.cs index bdfe1bc4..ec1143e8 100644 --- a/TwitchDownloaderCore/Chat/ChatJson.cs +++ b/TwitchDownloaderCore/Chat/ChatJson.cs @@ -7,9 +7,9 @@ using System.Text; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; -using TwitchDownloaderCore.Extensions; using TwitchDownloaderCore.Tools; using TwitchDownloaderCore.TwitchObjects; @@ -20,6 +20,7 @@ public static class ChatJson private static readonly JsonSerializerOptions _jsonSerializerOptions = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString, AllowTrailingCommas = true }; @@ -57,20 +58,7 @@ public static async Task DeserializeAsync(string filePath, bool getCom if (jsonDocument.RootElement.TryGetProperty("streamer", out JsonElement streamerElement)) { - if (returnChatRoot.FileInfo.Version > new ChatRootVersion(1, 0, 0)) - { - returnChatRoot.streamer = streamerElement.Deserialize(options: _jsonSerializerOptions); - } - else - { - var legacyStreamer = streamerElement.Deserialize(options: _jsonSerializerOptions); - returnChatRoot.streamer = legacyStreamer.id.ValueKind switch - { - JsonValueKind.Number => new Streamer { name = legacyStreamer.name, id = legacyStreamer.id.GetInt32() }, - JsonValueKind.String => new Streamer { name = legacyStreamer.name, id = int.Parse(legacyStreamer.id.GetString()!) }, - _ => null // Fallback to UpgradeChatJson() - }; - } + returnChatRoot.streamer = streamerElement.Deserialize(options: _jsonSerializerOptions); } if (jsonDocument.RootElement.TryGetProperty("video", out JsonElement videoElement)) diff --git a/TwitchDownloaderCore/TwitchObjects/ChatRoot.cs b/TwitchDownloaderCore/TwitchObjects/ChatRoot.cs index 743add7c..36fa5b73 100644 --- a/TwitchDownloaderCore/TwitchObjects/ChatRoot.cs +++ b/TwitchDownloaderCore/TwitchObjects/ChatRoot.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Text.Json; using System.Text.Json.Serialization; namespace TwitchDownloaderCore.TwitchObjects @@ -12,13 +11,6 @@ public class Streamer public int id { get; set; } } - public class LegacyStreamer - { - public string name { get; set; } - /// Some old chats use a string instead of an integer. - public JsonElement id { get; set; } - } - [DebuggerDisplay("{display_name}")] public class Commenter {