District stepped pit experience when using 3.0 Net core 2.x upgrade comes System.Text.Json

.Net Core 3.0 update a lot of things, do not do more to explain here, the official bigwigs very detailed and Expo Garden

About Net Core time zone problem, in version 2.1, when used as a Newtonsoft.Json, more convenient configuration

AddJsonOptions(opt => {
                opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
            })

But with System.Text.Json not so convenient, and turned for a long time, only to find a JsonConverts, I wrote a converter to do when a transfer zone

using System.Text.Json.Serialization;
using System.Text.Json;

namespace LuciusLiang.Pwanshop.Api
{
    public class JsonDateTimeConvert : JsonConverter<DateTime>
    {
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var paramString = reader.GetString();

            var localDateTime = Convert.ToDateTime(paramString);

            return localDateTime;
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
}

This problem because they do test the boundaries of discovery, so it does not fully tested whether there are other problems, please leave a message if you have found, thanks.

Guess you like

Origin www.cnblogs.com/luciusliang/p/11617432.html