Search code examples
c#asp.net-coreswagger

System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported


I am using .NET 6, ASP.NET Core Web API 6.

enter image description here

db PostgreSQL 15.

DROP TABLE IF EXISTS account_object;
CREATE TABLE account_object
(
    id                             uuid DEFAULT uuid_generate_v4 () 
    birth_date                     date,
   
    created                        timestamp with time zone     not null,
    tenant_id                      character varying(36)
);

Model

using System;
using System.Collections.Generic;

namespace acc.Models
{
   
    public partial class AccountObject
    {
        public Guid Id { get; set; }
     
     
        public DateOnly? BirthDate { get; set; }
       
        public DateTime Created { get; set; }
        public string? TenantId { get; set; }        
    }
}

controller

using acc.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace acc.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountObjectController : ControllerBase
    {
        private readonly ILogger<AccountObjectController> _logger;
        private readonly acc200Context _db;

        public AccountObjectController(ILogger<AccountObjectController> logger, acc200Context acc200Context)
        {
            _logger = logger;
            _db = acc200Context;
        }

        [HttpGet("{tenant_id}")]
        public IEnumerable<AccountObject> Get(string tenant_id)
        {
            return _db.AccountObjects.Where(x => x.TenantId == tenant_id).ToArray();
        }

    }
}

Error

System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported. The unsupported member type is located on type 'System.Nullable`1[System.DateOnly]'. Path: $.BirthDate.
 ---> System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported.
   at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Converters.NullableConverter`1.Write(Utf8JsonWriter writer, Nullable`1 value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Converters.ArrayConverter`2.OnWriteResume(Utf8JsonWriter writer, TElement[] array, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at ...

Image https://i.sstatic.net/Tdow8.png

How to fix?


Solution

  • If you are using .NET 7, this feature has been added and you do not need to change anything.

    See Source generation improvements in the link: https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-7/


    If you are using .NET 6, implement the code below:

    Yes, DateOnly and TimeOnly serialization is not supported right now. There is a workaround

    public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
    {
        public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return DateOnly.FromDateTime(reader.GetDateTime());
        }
    
        public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
        {
            var isoDate = value.ToString("O");
            writer.WriteStringValue(isoDate);
        }
    }
    

    And ensure you added new converter

    public class Startup
    {
       public void ConfigureServices(IServiceCollection services)
       {
          services.AddControllers()
                .AddJsonOptions(options =>
                {
                   options.JsonSerializerOptions.Converters.Add(new DateOnlyJsonConverter());
                });
    
          // ...
       }
    }