System.NotSupportedException:不支持“System.DateOnly”实例的序列化和反序列化

问题描述 投票:0回答:4

我正在使用 .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)
);

型号

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; }        
    }
}

控制器

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();
        }

    }
}

错误

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 ...

图片https://i.sstatic.net/Tdow8.png

如何修复?

c# asp.net-core swagger
4个回答
44
投票

如果您使用的是.NET 7,则已添加此功能,无需更改任何内容。

请参阅链接中的源生成改进https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-7/


如果您使用的是 .NET 6,请实现以下代码:

是的,目前不支持 DateOnlyTimeOnly 序列化。有一个解决方法

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);
    }
}

并确保您添加了新的转换器

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

      // ...
   }
}

4
投票

现在,如果您使用的是较新版本的 .NET Core,即 .NET 7,您只需将 System.Text.Json 更新到最新版本,一切就可以正常工作,无需额外的转换器


1
投票

这效果很好。

请注意您是否使用较新版本的 .NET Core;没有 Startup 的,那么您可以将其添加到 Program.cs 中,如下所示:

 builder.Services.AddControllers()
                .AddJsonOptions(options =>
                {
                    options.JsonSerializerOptions.Converters.Add(new DateOnlyJsonConverter());
                });

0
投票

如果您使用的是 .NET 6、ASP.NET Core Web API 6。

你需要安装一个包

<PackageReference Include="System.Text.Json" Version="7.0.2" />

您不会收到此错误

System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported. Path: $.availRequest.journeyDate | LineNumber: 0 | BytePositionInLine: 244.
© www.soinside.com 2019 - 2024. All rights reserved.