Skip to content

Commit

Permalink
Fix old chats with stringified streamer ids being undeserializable (#843
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ScrubN authored Oct 7, 2023
1 parent 68a6b85 commit fefd3d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 15 additions & 1 deletion TwitchDownloaderCore/Chat/ChatJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -66,7 +67,20 @@ public static class ChatJson

if (jsonDocument.RootElement.TryGetProperty("streamer", out JsonElement streamerElement))
{
returnChatRoot.streamer = streamerElement.Deserialize<Streamer>(options: _jsonSerializerOptions);
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()
};
}
}

if (jsonDocument.RootElement.TryGetProperty("video", out JsonElement videoElement))
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderCore/TwitchObjects/ChatRoot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace TwitchDownloaderCore.TwitchObjects
Expand All @@ -11,6 +12,13 @@ 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 fefd3d1

Please sign in to comment.