0

In my ASP.NET Core app, I have the following setup in Startup:

services.AddNewtonsoftJson(options =>
{
    options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
    options.SerializerSettings.DateFormatString = "yyyy/MM/dd"; // <-- HERE

});

The line marked with "HERE" comment sets a global way of handling DateTime serialization. This is what I need in 99% of the cases. However, I have an endpoint where I do not want to format dates this way and I want to retain time information as well (in ISO format for example). Is there any way to override the global setting somehow for specific endpoint or model?

I tried applying [JsonConverter(typeof(IsoDateTimeConverter))] attribute to my model's DateTime properties, but that didn't change anything.

dbc
  • 104,963
  • 20
  • 228
  • 340
mnj
  • 2,539
  • 3
  • 29
  • 58
  • check this out: https://stackoverflow.com/questions/56429027/how-to-ignore-datetimezonehandling-utc-for-some-fields-like-date-of-birth – MC LinkTimeError Nov 15 '22 at 10:30
  • @MCLinkTimeError As I see, there's no definitive answer, but I'll check this out anyway. – mnj Nov 15 '22 at 11:34
  • @MCLinkTimeError, that issue does not answer my question unfortunately – mnj Nov 15 '22 at 14:10
  • `[JsonConverter(typeof(IsoDateTimeConverter))]` is not working when applied to specific properties because `JsonTextReader` has already recognized the incoming JSON strings as `DateTime` objects. To prevent that, you can add `[JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)]` to your model, where `DateParseHandlingConverter` comes from [this answer](https://stackoverflow.com/a/50631270/3744182) to [Json.NET deserializing DateTimeOffset value fails for DateTimeOffset.MinValue without timezone](https://stackoverflow.com/q/50628374/3744182). – dbc Nov 15 '22 at 15:42
  • 1
    But that's kind of a workaround, it would be cleaner to control the settings per endpoint as your question asks. What precise version of asp.net / asp.net core are you using? The answer will depend on the version, I suspect. For instance, see [this answer](https://stackoverflow.com/a/52623772/3744182) by Kirk Larkin to [Change the JSON serialization settings of a single ASP.NET Core controller](https://stackoverflow.com/q/52605946/3744182). – dbc Nov 15 '22 at 15:43
  • Also, do you need to control settings just for serialization, or also for model binding / deserialization? – dbc Nov 15 '22 at 19:49

1 Answers1

0

It turned out I was applying the attribute wrong. I'm using records. Instead of doing that:

public record Abc(
   [JsonConverter(typeof(IsoDateTimeConverter))]
   DateTime DateTime)

I should have done that:

public record Abc(
   [property: JsonConverter(typeof(IsoDateTimeConverter))]
   DateTime DateTime)

That way, the attribute gets applied to the property, and not constructor parameter. My record gets serialized with ISO time format, even though the global setting is different.

mnj
  • 2,539
  • 3
  • 29
  • 58