Skip to content

Commit

Permalink
Use NumberHandling in JsonSerializerOptions instead of LegacyStreamer
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Nov 23, 2023
1 parent ce1ef57 commit 26f250b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
18 changes: 3 additions & 15 deletions TwitchDownloaderCore/Chat/ChatJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,6 +20,7 @@ public static class ChatJson
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
AllowTrailingCommas = true
};

Expand Down Expand Up @@ -57,20 +58,7 @@ public static async Task<ChatRoot> 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<Streamer>(options: _jsonSerializerOptions);
}
else
{
var legacyStreamer = streamerElement.Deserialize<LegacyStreamer>(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<Streamer>(options: _jsonSerializerOptions);
}

if (jsonDocument.RootElement.TryGetProperty("video", out JsonElement videoElement))
Expand Down
8 changes: 0 additions & 8 deletions TwitchDownloaderCore/TwitchObjects/ChatRoot.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,13 +11,6 @@ public class Streamer
public int id { get; set; }
}

public class LegacyStreamer
{
public string name { get; set; }
/// <remarks>Some old chats use a string instead of an integer.</remarks>
public JsonElement id { get; set; }
}

[DebuggerDisplay("{display_name}")]
public class Commenter
{
Expand Down

0 comments on commit 26f250b

Please sign in to comment.