using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; namespace SVSim.EmulatedEntrypoint.Models.Dtos.Common; /// /// Reads a JSON string OR number as a nullable string, tolerating prod's polymorphic id fields. /// rotation_id on a /load/index UserDeck is a numeric string ("10008") for real /// MyRotation decks but a bare number (0) for unset slots — and the global /// AllowReadingFromString only covers the string→number direction, not number→string, so a /// plain string? property 400s on the numeric form. Null stays null; numbers serialize via /// invariant culture so a captured 0 round-trips to "0". /// public sealed class FlexibleStringConverter : JsonConverter { public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.TokenType switch { JsonTokenType.Null => null, JsonTokenType.String => reader.GetString(), JsonTokenType.Number when reader.TryGetInt64(out var n) => n.ToString(CultureInfo.InvariantCulture), JsonTokenType.Number => reader.GetDouble().ToString(CultureInfo.InvariantCulture), _ => throw new JsonException($"Unexpected token {reader.TokenType} for a string-or-number field.") }; public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) => writer.WriteStringValue(value); }