.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. 设计模式——<面向对象设计原则以及23种设计模式分类>

    一.面向对象八大设计原则: 1.依赖倒置原则(DIP) 高层模块(稳定)不应该依赖于低层模块(变化),二者都应该依赖于抽象(稳定) . 抽象(稳定)不应该依赖于实现细节(变化) ,实现细节应该依赖于抽 ...

  2. HBase(一) —— 基本概念及使用

    一.安装&启动 下载 https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.1.8/ 快速开始文档,HBase2.1.8 http://hba ...

  3. ldap和phpldapadmin的安装部署

    LDAP 安装 一.安装LDAP 1. 安装包 yum install openssl-devel gcc libtool-ltdl-devel -y yum install openldap-ser ...

  4. 第06组 Beta冲刺(5/5)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 继续维护后端代码 准备beta版本的答辩 GitHub签入记录 接下来的计划 整理任务, ...

  5. Alpha2

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11885037.html 作业博客 :https://edu.cnblogs.com/campus/f ...

  6. tmp 字典合并

    tmp  

  7. notepadd++中,如何根据某个字符将一行内容切割成多行?

    背景描述: 今天在做个事情,遇到下面字符,就想将每个词,如cluster,zookeeper都单独的一行 [cluster, activemq, controller, brokers, zookee ...

  8. docker安装并运行mongo

    拉镜像: [mall@VM_0_7_centos ~]$ sudo docker pull mongo:3.2 [sudo] password for mall: 3.2: Pulling from ...

  9. 【linux基础】ubuntu16.04 安装后,屏幕分辨率过低且不可调节

    前言 安装CUDA和NVIDIA驱动之后,屏幕分辨率过低且不可调节,尝试了几种方法都没成功解决,特此讲解决方法记录下来. 解决过程 注销进入登录界面,按ctrl+alt+F1进入命令行终端,输入账户名 ...

  10. [LeetCode] 210. Course Schedule II 课程安排II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...