Fix Json.ParseJson crash on empty/null input

ParseJson now returns null for null, empty, or whitespace-only input
instead of throwing JsonReaderException. This matches the behavior of
the old hand-rolled parser and is needed because MC protocol packets
may contain empty strings where JSON text is expected (e.g. empty chat
messages in DeathCombatEvent packets).

Discovered during end-to-end testing against a Minecraft 1.21.11
protocol server.

Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com>
Agent-Logs-Url: https://github.com/milutinke/Minecraft-Console-Client/sessions/34df723e-4a63-45a0-a942-e119d81a575b
This commit is contained in:
copilot-swe-agent[bot] 2026-03-22 17:22:08 +00:00
parent 64ccdcb39b
commit d2c1cbf2a5

View file

@ -16,8 +16,10 @@ public static class Json
/// <summary>
/// Parse a JSON string into a mutable <see cref="JsonNode"/> DOM.
/// Returns null for null, empty, or whitespace-only input.
/// </summary>
public static JsonNode? ParseJson(string json) => JsonNode.Parse(json);
public static JsonNode? ParseJson(string? json) =>
string.IsNullOrWhiteSpace(json) ? null : JsonNode.Parse(json);
/// <summary>
/// Escape a string for embedding inside a JSON string literal.