32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
|
|
using System.Globalization;
|
||
|
|
using System.Text.Json;
|
||
|
|
using System.Text.Json.Serialization;
|
||
|
|
|
||
|
|
namespace SIGCM2.Api.Json;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// JSON converter for <see cref="DateOnly"/> 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.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
|
||
|
|
{
|
||
|
|
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));
|
||
|
|
}
|
||
|
|
}
|