12

tl;dr: How can I add the DateOnly JsonConverter to the application configuration of a .net6 web api?

I have an object with DateOnly properties on it and I'm returning it from the controller. When I make my request I get the error "Serialization and deserialization of 'System.DateOnly' instances are not supported".

I understand there are limitations with de/serialization of DateOnly and TimeOnly in .net6 (https://github.com/dotnet/runtime/issues/53539) and I followed the recommended use of a custom JsonConverter. However the problem I am having is where I initialize the converter.

I tried adding the converter in the web application builder....


builder.Services
    .AddControllers()
    .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new DateOnlyConverter()));

but no luck.

Then I tried configuring it specifically

builder.Services.Configure<JsonSerializerOptions>(options =>
    options.Converters.Add(new DateOnlyConverter()));

no luck there either.

So I tried returning Results.Json from the controller with the converter in the options Results.Json(status, options). Even that did not work.

What did work though was adding an attribute directly to the property

[property: JsonConverter(typeof(DateOnlyConverter))]
    DateOnly DateOrdered,

I would rather not have to add this attribute to every use of DateOnly and am really unclear as to what is preventing the converter from being called when it's configured as part of WebApplication. I've done similar things in .net5 applications, and assume some nuance between IHostBuilder and WebApplicationBuilder could be causing the issue.

Edit: It's probably worth noting I am using the new minimal apis. But even Microsoft points out Json serialization is configurable in the docs here. The docs show slightly different syntax than what is commonly used, builder.Services.Configure<JsonOptions>, so I assumed maybe this was the problem. Perhaps the minimal apis had configuration handled differently. But not so. At least, using the suggested configuration in the minimal api docs did not solve the problem.

dbc
  • 104,963
  • 20
  • 228
  • 340
Crustyeyelids
  • 410
  • 4
  • 9
  • Can you please post short repro somewhere (at github :-) with full setup, controller and models. – Guru Stron Nov 24 '21 at 17:15
  • Did you see [this](https://github.com/dotnet/runtime/issues/53539#issuecomment-965275504) implementation of converters? It's on your link. Doesn't work either? – AndrewSilver Nov 24 '21 at 17:28
  • https://gitlab.com/disperdercreare/net6-api-test – Crustyeyelids Nov 24 '21 at 17:34
  • @AndrewSilver I did see that implementation. It's the converter I used. The converter works fine, the problem calling it on serialization. It seems to ignore it unless I add the JsonConverter attribute directly to the property. – Crustyeyelids Nov 24 '21 at 17:36

1 Answers1

9

Okay, after enough searching I was able to find the answer. Yes, configuration for the minimal apis is separate. The documentation was right above where they described configuring minimal api's json serialization using builder.Services.Configure<JsonOptions>(options => ...). Hopefully this answer will help anyone else in the future too impatient to read the documentation closely.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Crustyeyelids
  • 410
  • 4
  • 9