using System.Text.Json; namespace ZB.MOM.WW.MxGateway.Server.Security.Authentication; public static class ApiKeyConstraintSerializer { private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, WriteIndented = false, }; /// Serializes API key constraints to JSON, or returns null if the constraints are empty. /// The constraints to serialize. /// The serialized JSON, or when the constraints are empty. public static string? Serialize(ApiKeyConstraints constraints) { ArgumentNullException.ThrowIfNull(constraints); return constraints.IsEmpty ? null : JsonSerializer.Serialize(constraints, JsonOptions); } /// Deserializes API key constraints from JSON, or returns empty constraints if JSON is null or whitespace. /// The JSON string to deserialize. /// The deserialized constraints, or when is null/whitespace. /// /// Members absent from the JSON take their default: rows persisted before /// existed carry no dashboard_tags /// member and deserialize to an untagged key, unchanged in every other respect. /// public static ApiKeyConstraints Deserialize(string? json) { if (string.IsNullOrWhiteSpace(json)) { return ApiKeyConstraints.Empty; } return JsonSerializer.Deserialize(json, JsonOptions) ?? ApiKeyConstraints.Empty; } }