I have an object as a body on POST request in my ASP.NET Core 8 application. The object is not serialized, and we found out that the property that is causing all is one with type ZonedDateTime and it has a value:
“completedTimestamp”: “2023-05-18T14:39:55.303+02:00[Europe/Budapest]”,
We are using Newtonsodt.Json library and configured the serializer settings as:
<code>public static IMvcBuilder ConfigureApiBehaviorOptions(this IMvcBuilder builder)
{
builder.AddNewtonsoftJson(options =>
{
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture("yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'", DateTimeZoneProviders.Serialization);
options.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
options.SerializerSettings.DateParseHandling = DateParseHandling.None;
options.SerializerSettings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
});
return builder;
}
</code>
<code>public static IMvcBuilder ConfigureApiBehaviorOptions(this IMvcBuilder builder)
{
builder.AddNewtonsoftJson(options =>
{
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture("yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'", DateTimeZoneProviders.Serialization);
options.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
options.SerializerSettings.DateParseHandling = DateParseHandling.None;
options.SerializerSettings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
});
return builder;
}
</code>
public static IMvcBuilder ConfigureApiBehaviorOptions(this IMvcBuilder builder)
{
builder.AddNewtonsoftJson(options =>
{
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture("yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'", DateTimeZoneProviders.Serialization);
options.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
options.SerializerSettings.DateParseHandling = DateParseHandling.None;
options.SerializerSettings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
});
return builder;
}
When the request hits the controller my object is null, if I remove all properties of ZonedDateTime it got mapped.
If I try this witha a console app it works. My console app:
<code>var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'",
DateTimeZoneProviders.Serialization
);
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
settings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
var json = "{rnt"completedTimestamp": "2023-05-18T14:39:55.303+02:00[Europe/Budapest]"rn}";
var data = JsonConvert.DeserializeObject<Data>(json, settings);
</code>
<code>var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'",
DateTimeZoneProviders.Serialization
);
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
settings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
var json = "{rnt"completedTimestamp": "2023-05-18T14:39:55.303+02:00[Europe/Budapest]"rn}";
var data = JsonConvert.DeserializeObject<Data>(json, settings);
</code>
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"yyyy'-'MM'-'dd'T'HH':'mm':'ss;FFFo<Z+HH:mm>'['z']'",
DateTimeZoneProviders.Serialization
);
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
settings.Converters.Add(new NodaPatternConverter<ZonedDateTime>(pattern));
var json = "{rnt"completedTimestamp": "2023-05-18T14:39:55.303+02:00[Europe/Budapest]"rn}";
var data = JsonConvert.DeserializeObject<Data>(json, settings);
thnx