From a75d2f75a0d4ba711449c1707e96ca6ea3e90c77 Mon Sep 17 00:00:00 2001 From: dmolinari Date: Sat, 18 Apr 2026 09:47:16 -0300 Subject: [PATCH] feat(udt-011): DateOnlyJsonConverter as yyyy-MM-dd --- .../SIGCM2.Api/Json/DateOnlyJsonConverter.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/api/SIGCM2.Api/Json/DateOnlyJsonConverter.cs diff --git a/src/api/SIGCM2.Api/Json/DateOnlyJsonConverter.cs b/src/api/SIGCM2.Api/Json/DateOnlyJsonConverter.cs new file mode 100644 index 0000000..ca1b1e1 --- /dev/null +++ b/src/api/SIGCM2.Api/Json/DateOnlyJsonConverter.cs @@ -0,0 +1,31 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace SIGCM2.Api.Json; + +/// +/// JSON converter for that uses the "yyyy-MM-dd" ISO format. +/// +/// UDT-011: Ensures Cat2 date fields (VigenciaDesde, etc.) never serialize as +/// "2026-05-01T00:00:00" or with a UTC suffix "Z", which would mislead consumers +/// into treating civil Argentine dates as absolute UTC instants. +/// +public sealed class DateOnlyJsonConverter : JsonConverter +{ + private const string DateFormat = "yyyy-MM-dd"; + + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var str = reader.GetString(); + if (str is null) + throw new JsonException("DateOnly value cannot be null."); + + return DateOnly.ParseExact(str, DateFormat, CultureInfo.InvariantCulture); + } + + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(DateFormat, CultureInfo.InvariantCulture)); + } +}