Мы создали объект даты с помощью Newtonsoft WriteStartConstructor. Но мы не могли сделать то же самое с System.Text.Json.

https://dotnetfiddle.net/XxP4gy

enter image description here

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json;

var myObj = new { DOB = DateTime.Now.AddYears(-20) };


Console.WriteLine("Newtonsoft (WriteStartConstructor) ->\t\t" + JsonConvert.SerializeObject(myObj, new NewtonsoftDateTimeConverter()));

var options = new JsonSerializerOptions();
options.Converters.Add(new SystemTextJsonDateTimeConverter());

Console.WriteLine("System.Text.Json (WriteStartObject) ->\t\t" + System.Text.Json.JsonSerializer.Serialize(myObj, options));


Console.ReadKey();

public class NewtonsoftDateTimeConverter : DateTimeConverterBase
{
    public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        DateTime val = (DateTime)value;
        writer.WriteStartConstructor("Date");
        writer.WriteValue(val.Year);
        writer.WriteValue(val.Month - 1);
        writer.WriteValue(val.Day);
        writer.WriteValue(val.Hour);
        writer.WriteValue(val.Minute);
        writer.WriteValue(val.Second);
        writer.WriteValue(0);
        writer.WriteEndConstructor();
    }
}

public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStartObject("Date");
        writer.WriteNumberValue(value.Year);
        writer.WriteNumberValue(value.Month - 1);
        writer.WriteNumberValue(value.Day);
        writer.WriteNumberValue(value.Hour);
        writer.WriteNumberValue(value.Minute);
        writer.WriteNumberValue(value.Second);
        writer.WriteNumberValue(0);
        writer.WriteEndObject();
    }
}
1
Palanikumar 27 Ноя 2021 в 08:31

1 ответ

Лучший ответ

Вы должны использовать WriteRawValue с параметром skipInputValidation, установленным на true. Например:

public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteRawValue($"new Date({value.Year},{value.Month - 1},{value.Day},{value.Hour},{value.Minute},{value.Second},0)", skipInputValidation: true);
    }
}

Я использовал это, и результат на вашем скрипте:

Newtonsoft ->        {"DOB":new Date(2001,10,27,5,57,10,0)}
System.Text.Json ->    {"DOB":new Date(2001,10,27,5,57,10,0)}
1
Andy 27 Ноя 2021 в 08:59