.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. ES6 数组方法 forEach map filter find every some reduce

    1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...

  2. Edusoho之X-Auth-Token

    昨天这篇文章Edusoho之Basic Authentication提到了X-Auth-Token.今天我主要讲的是Edusoho之X-Auth-Token的请求API方式. 至于为什么建议不要用HT ...

  3. 阿里云服务器安装配置配置MySQL

    1.先更新软件 输入 yum -y update 2.下载MySql安装包 rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el ...

  4. python去除BOM头\ufeff等特殊字符

    1.\ufeff 字节顺序标记 去掉\ufeff,只需改一下编码就行,把UTF-8编码改成UTF-8-sigwith open(file_path, mode='r', encoding='UTF-8 ...

  5. AOP的定义和原理

    一.本课目标 理解Spring AOP的原理 掌握Spring AOP的七个术语 二.面向切面编程(AOP)  AOP的思想是,不去动原来的代码,而是基于原来代码产生代理对象,通过代理的方法,去包装原 ...

  6. [转]Oracle 查询表外键相关信息

    原文地址:https://www.csdn.net/gather_27/MtTaUgxsNzYxMi1ibG9n.html 查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询): s ...

  7. 用Java和Nodejs获取http30X跳转后的url

    用Java和Nodejs获取http30X跳转后的url 转 https://calfgz.github.io/blog/2018/05/http-redirect-java-node.html 30 ...

  8. (mac)Idea安装配置maven

    一.mac安装配置maven 1)官网下载(http://maven.apache.org/download.cgi) binary   .tar.gz 下载解压到某处 2)配置环境变量 $ open ...

  9. Nginx 配置 HTTP 代理

    配置如下 #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; ...

  10. IISExpress.无法启动IIS Express Web 服务器.Starting IIS Express... IIS Express is running

    x 提示: 无法启动IIS Express Web 服务器. 来自IIS Express的输出: Starting IIS Express... IIS Express is running 总结: ...