Minecraft-Console-Client/MinecraftClient/Json.cs
copilot-swe-agent[bot] 15aabd9423 Replace legacy custom JSON parser with System.Text.Json
- Rewrite Json.cs to use System.Text.Json.Nodes (JsonNode, JsonObject, JsonArray)
- Add JsonNodeExtensions.GetStringValue() for backward-compatible string access
- Update all 14 consumer files to use the new JsonNode API
- Remove ~300 lines of hand-rolled JSON parsing code from 2013
- Replace KeyUtils.EscapeString with delegation to Json.EscapeString

Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com>
Agent-Logs-Url: https://github.com/milutinke/Minecraft-Console-Client/sessions/afcd1b7b-ea23-4a0d-bb46-a90b623406fc
2026-03-22 16:39:26 +00:00

47 lines
No EOL
1.5 KiB
C#

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace MinecraftClient;
/// <summary>
/// JSON utilities backed by System.Text.Json.
/// </summary>
public static class Json
{
private static readonly JsonSerializerOptions s_escapeOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
/// <summary>
/// Parse a JSON string into a mutable <see cref="JsonNode"/> DOM.
/// </summary>
public static JsonNode? ParseJson(string json) => JsonNode.Parse(json);
/// <summary>
/// Escape a string for embedding inside a JSON string literal.
/// Uses System.Text.Json serialization and strips the surrounding quotes.
/// </summary>
public static string EscapeString(string src) =>
JsonSerializer.Serialize(src, s_escapeOptions)[1..^1];
}
/// <summary>
/// Extension helpers for <see cref="JsonNode"/> that replicate the access patterns
/// of the former <c>JSONData.StringValue</c> property.
/// </summary>
public static class JsonNodeExtensions
{
/// <summary>
/// Return the string representation of any JSON value.
/// Strings are returned without quotes; numbers, booleans, and null
/// are returned as their text representation.
/// </summary>
public static string GetStringValue(this JsonNode? node) => node switch
{
null => "null",
JsonValue val when val.TryGetValue<string>(out var s) => s,
_ => node.ToJsonString()
};
}