// #nullable enable using System; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; namespace CarpaNet.Models; /// /// Represents a Lexicon JSON document. /// public sealed class LexiconDocument { /// /// The Lexicon language version (currently always 1). /// [JsonPropertyName("lexicon")] public int Lexicon { get; set; } /// /// The NSID of this Lexicon. /// [JsonPropertyName("id")] public string Id { get; set; } = string.Empty; /// /// Optional description of this Lexicon. /// [JsonPropertyName("description")] public string? Description { get; set; } /// /// The definitions in this Lexicon, keyed by name. /// [JsonPropertyName("defs")] public Dictionary Defs { get; set; } = new(); } /// /// Represents a single definition within a Lexicon. /// public sealed class LexiconDefinition { /// /// The type of this definition. /// [JsonPropertyName("type")] public string Type { get; set; } = string.Empty; /// /// Optional description. /// [JsonPropertyName("description")] public string? Description { get; set; } // Record-specific [JsonPropertyName("key")] public string? Key { get; set; } [JsonPropertyName("record")] public LexiconDefinition? Record { get; set; } // Object-specific [JsonPropertyName("properties")] public Dictionary? Properties { get; set; } /// /// Raw JSON value for the "required" field. Can be either a string array (object-level) /// or a boolean (property-level, used by some third-party lexicons). /// [JsonPropertyName("required")] public JsonElement? RequiredRaw { get; set; } /// /// Gets the required property names list (from object-level "required": ["field1", "field2"]). /// Returns null if the field is absent or is a boolean. /// [JsonIgnore] public List? Required { get { if (RequiredRaw == null || RequiredRaw.Value.ValueKind != JsonValueKind.Array) { return null; } var list = new List(); foreach (var item in RequiredRaw.Value.EnumerateArray()) { if (item.ValueKind == JsonValueKind.String) { list.Add(item.GetString()!); } } return list; } } /// /// Gets whether this property is individually marked as required via "required": true. /// Used by some third-party lexicons on individual property definitions. /// [JsonIgnore] public bool IsRequired => RequiredRaw?.ValueKind == JsonValueKind.True; [JsonPropertyName("nullable")] public List? Nullable { get; set; } // Array-specific [JsonPropertyName("items")] public LexiconDefinition? Items { get; set; } [JsonPropertyName("minLength")] public int? MinLength { get; set; } [JsonPropertyName("maxLength")] public int? MaxLength { get; set; } // String-specific [JsonPropertyName("format")] public string? Format { get; set; } [JsonPropertyName("enum")] public List? Enum { get; set; } [JsonPropertyName("knownValues")] public List? KnownValues { get; set; } [JsonPropertyName("default")] public JsonElement? Default { get; set; } [JsonPropertyName("const")] public JsonElement? Const { get; set; } [JsonPropertyName("maxGraphemes")] public int? MaxGraphemes { get; set; } [JsonPropertyName("minGraphemes")] public int? MinGraphemes { get; set; } // Integer-specific [JsonPropertyName("minimum")] public long? Minimum { get; set; } [JsonPropertyName("maximum")] public long? Maximum { get; set; } // Ref-specific [JsonPropertyName("ref")] public string? Ref { get; set; } // Union-specific [JsonPropertyName("refs")] public List? Refs { get; set; } [JsonPropertyName("closed")] public bool? Closed { get; set; } // Blob-specific [JsonPropertyName("accept")] public List? Accept { get; set; } [JsonPropertyName("maxSize")] public long? MaxSize { get; set; } // Query/Procedure-specific [JsonPropertyName("parameters")] public LexiconDefinition? Parameters { get; set; } [JsonPropertyName("input")] public LexiconIO? Input { get; set; } [JsonPropertyName("output")] public LexiconIO? Output { get; set; } [JsonPropertyName("errors")] public List? Errors { get; set; } // Subscription-specific [JsonPropertyName("message")] public LexiconMessage? Message { get; set; } } /// /// Represents input/output schema for queries and procedures. /// public sealed class LexiconIO { [JsonPropertyName("description")] public string? Description { get; set; } [JsonPropertyName("encoding")] public string Encoding { get; set; } = string.Empty; [JsonPropertyName("schema")] public LexiconDefinition? Schema { get; set; } } /// /// Represents an error definition. /// public sealed class LexiconError { [JsonPropertyName("name")] public string Name { get; set; } = string.Empty; [JsonPropertyName("description")] public string? Description { get; set; } } /// /// Represents a subscription message schema. /// public sealed class LexiconMessage { [JsonPropertyName("description")] public string? Description { get; set; } [JsonPropertyName("schema")] public LexiconDefinition? Schema { get; set; } }