.Net Core 3.0 更新的东西很多,这里就不多做解释了,官方和博园大佬写得很详细

关于 Net Core 时区问题,在 2.1 版本的时候,因为用的是 Newtonsoft.Json,配置比较方便

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

但是用 System.Text.Json 就没那么方便了,翻了半天,只找到个 JsonConverts ,自己写了一个转换器去做转时区

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

然后在 StartUp 中添加进 JsonOptions

            services.AddControllers(opt =>
{
opt.InputFormatters.Add(new TextPlainInputFormatter());
}).AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
opt.JsonSerializerOptions.Converters.Add(new JsonDateTimeConvert()); }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

因为自己做边界测试发现的这个问题,所以也没有全面测试过是否存在其他问题,如果有发现请留言,感谢。

Net core 2.x 升级 3.0 使用自带 System.Text.Json 时区 踩坑经历的更多相关文章

  1. 在.Net Core 3.0中尝试新的System.Text.Json API

    .NET Core 3.0提供了一个名为System.Text.Json的全新命名空间,它支持reader/writer,文档对象模型(DOM)和序列化程序.在此博客文章中,我将介绍它如何工作以及如何 ...

  2. .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法

    行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少 ...

  3. [.Net Core 3.0+/.Net 5] System.Text.Json中时间格式化

    简介 .Net Core 3.0开始全新推出了一个名为System.Text.Json的Json解析库,用于序列化和反序列化Json,此库的设计是为了取代Json.Net(Newtonsoft.Jso ...

  4. .NET Core 内置的 System.Text.Json 使用注意

    System.Text.Json 是 .NET Core 3.0 新引入的高性能 json 解析.序列化.反序列化类库,武功高强,但毕竟初入江湖,炉火还没纯青,使用时需要注意,以下是我们在实现使用中遇 ...

  5. .netcore3.0 System.Text.Json 日期格式化

    .netcore3.0 的json格式化不再默认使用Newtonsoft.Json,而是使用自带的System.Text.Json来处理. 理由是System.Text.Json 依赖更少,效率更高. ...

  6. .net core中关于System.Text.Json的使用

    在.Net Framework的时候序列化经常使用Newtonsoft.Json插件来使用,而在.Net Core中自带了System.Text.Json,号称性能更好,今天抽空就来捣鼓一下. 使用起 ...

  7. openssl1.0在mac下的编译安装(踩坑精华)

    之前做了一次brew版本升级,然后用pip3安装的一个python命令就无法执行了(涉及到openssl库),执行就会报一个错误. ImportError: dlopen(/usr/local/Cel ...

  8. vue-cli3.0创建项目报npm install --loglevel error 踩坑的那把辛酸泪

    创建项目 vue create vue-pro 然后如下图 一开始以为是npm的问题,卸载了Mac的node ,安装nvm,然后再安装node (可参考: Mac中nvm的安装和使用   https: ...

  9. asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

    在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...

随机推荐

  1. 【大数据作业十一】分布式并行计算MapReduce

    作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapReduce的功 ...

  2. MVC设计模式和三层架构

    JavaEE设计模式 1.传统设计模式(现在几乎不再使用): Jsp + javaBean, JavaBean用来对应数据库中的表,jsp负责显示界面.接受请求.处理业务.访问数据库. 弊端: 业务多 ...

  3. 绿色地狱 - 纽博格林赛道详解 | Nürburgring

    Nürburgring - Green Hell [統哥] 車迷人生必去一趟的德國紐柏林賽道之旅 F1赛道通常短而宽,一是为了观赏性,二是为了安全. 而Nürburgring赛道则是F1赛道的极端反面 ...

  4. Redis系列 | Redis5.0 新特性

  5. 【转】史上最强Tomcat8性能优化

    https://blog.csdn.net/ThinkWon/article/details/102744033 文章目录授人以鱼不如授人以渔目的服务器资源Tomcat配置优化Linux环境安装运行T ...

  6. array_slice

    array_slice  分割数组, 效果相当于 substr 类似字符串操作

  7. [LeetCode] 291. Word Pattern II 词语模式 II

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  8. soapUI使用-调用post方法

    前言 soapUI的安装请查看此链接:https://www.cnblogs.com/linxiu-0925/p/10138771.html 使用步骤 1打开soapUI 2新建一个项目:New SO ...

  9. robot:当用例失败时执行关键字(发送短信)

    使用场景: 当用例失败时需要通知对应人员,则需要在Teardown中,使用关键字Run Keyword If Test Failed Send Message关键字为自定义关键字,${content} ...

  10. 使用consul实现分布式服务注册和发现--redis篇

    安装consul client consul 客户端检脚本 ====================================================================== ...