From 757cbe1a680f0091ba906feb5c4619138d0a7f5b Mon Sep 17 00:00:00 2001 From: Anon Date: Sun, 22 Mar 2026 19:53:15 +0100 Subject: [PATCH] Fix ParseJson crash on non-JSON plain-text input (e.g. vanilla MOTD) When the MC server sends a plain-text string (not valid JSON) for fields like the ServerData MOTD, JsonNode.Parse() throws JsonReaderException. This was a regression introduced by the System.Text.Json modernization. Fix: catch JsonException in ParseJson() and return JsonValue.Create(json) so plain-text strings are treated as literal string values rather than crashing. Tested against a real Minecraft 1.21.11 server (offline mode): MCC connects and stays connected without crashing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- MinecraftClient/Json.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/MinecraftClient/Json.cs b/MinecraftClient/Json.cs index 827a23f4..da3aa838 100644 --- a/MinecraftClient/Json.cs +++ b/MinecraftClient/Json.cs @@ -17,9 +17,15 @@ public static class Json /// /// Parse a JSON string into a mutable DOM. /// Returns null for null, empty, or whitespace-only input. + /// Returns a wrapping the raw string when the input + /// is not valid JSON (e.g. a plain-text Minecraft MOTD or chat message). /// - public static JsonNode? ParseJson(string? json) => - string.IsNullOrWhiteSpace(json) ? null : JsonNode.Parse(json); + public static JsonNode? ParseJson(string? json) + { + if (string.IsNullOrWhiteSpace(json)) return null; + try { return JsonNode.Parse(json); } + catch (JsonException) { return JsonValue.Create(json); } + } /// /// Escape a string for embedding inside a JSON string literal.