using System.Text.Json; namespace StickyBoard.Core.Common; public static class JsonExtensions { /// /// Serializes any object or dictionary into a compact JSON string. /// Returns "{}" for null objects. /// public static string ToJson(this object? obj) { if (obj is null) return "{}"; return JsonSerializer.Serialize(obj, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false }); } /// /// Parses a JSON string into a JsonDocument, returning an empty object on error. /// public static JsonDocument ToJsonDocument(this object? obj) { try { var json = obj?.ToJson() ?? "{}"; return JsonDocument.Parse(json); } catch { return JsonDocument.Parse("{}"); } } }