using System.Text.Json; namespace Distribution.Features.GeneralLedger; /// Small readers for the GL service's ApiResponse envelope ({statusCode, success, message, data}). public static class GlJson { public static JsonElement? Data(GeneralLedgerResult result) => result.Body is { ValueKind: JsonValueKind.Object } body && Get(body, "data") is { } data ? data : null; public static string? Message(GeneralLedgerResult result) => result.Body is { ValueKind: JsonValueKind.Object } body ? Get(body, "message")?.GetString() ?? Get(body, "Message")?.GetString() : null; /// Case-insensitive property lookup (GL success bodies are camelCase, its error bodies PascalCase). public static JsonElement? Get(JsonElement element, string name) { if (element.ValueKind != JsonValueKind.Object) return null; foreach (var p in element.EnumerateObject()) if (string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase)) return p.Value; return null; } public static string? GetString(JsonElement element, string name) { var v = Get(element, name); return v is null || v.Value.ValueKind == JsonValueKind.Null ? null : v.Value.ValueKind == JsonValueKind.String ? v.Value.GetString() : v.Value.ToString(); } public static long? GetLong(JsonElement element, string name) => long.TryParse(GetString(element, name), out var n) ? n : null; }