Newtonsoft.Json与System.Text.Json区别

在 Newtonsoft.Json中可以使用例如
.AddJsonOptions(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
})

方式设置接收/序列化时间格式,但在.net core 3.1中System.Text.Json是没有自带方式进行转换,这就需要自定义Converter实现时间转换

官方GitHub地址

自定义DateTimeJsonConverter

    public class DateTimeJsonConverter : JsonConverter<DateTime>
{
private readonly string _dateFormatString;
public DateTimeJsonConverter()
{
_dateFormatString = "yyyy-MM-dd HH:mm:ss";
} public DateTimeJsonConverter(string dateFormatString)
{
_dateFormatString = dateFormatString;
} public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
} public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToUniversalTime().ToString(_dateFormatString));
}
}

JsonTokenType枚举类型共有以下几种

  /// <summary>Defines the various JSON tokens that make up a JSON text.</summary>
public enum JsonTokenType : byte
{
/// <summary>There is no value (as distinct from <see cref="F:System.Text.Json.JsonTokenType.Null" />).</summary>
None,
/// <summary>The token type is the start of a JSON object.</summary>
StartObject,
/// <summary>The token type is the end of a JSON object.</summary>
EndObject,
/// <summary>The token type is the start of a JSON array.</summary>
StartArray,
/// <summary>The token type is the end of a JSON array.</summary>
EndArray,
/// <summary>The token type is a JSON property name.</summary>
PropertyName,
/// <summary>The token type is a comment string.</summary>
Comment,
/// <summary>The token type is a JSON string.</summary>
String,
/// <summary>The token type is a JSON number.</summary>
Number,
/// <summary>The token type is the JSON literal true.</summary>
True,
/// <summary>The token type is the JSON literal false.</summary>
False,
/// <summary>The token type is the JSON literal null.</summary>
Null,
}

services.AddMvc 中 Newtonsoft.Json与System.Text.Json 配置区别

.netcore 2.1

            services
//全局配置Json序列化处理
.AddJsonOptions(options =>
{
//忽略循环引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//不使用驼峰样式的key
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//设置时间格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
})

.netcore 3.1

            services.AddControllers()
.AddJsonOptions(options =>
{
//设置时间格式
options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
//设置bool获取格式
options.JsonSerializerOptions.Converters.Add(new BoolJsonConverter());
//不使用驼峰样式的key
options.JsonSerializerOptions.PropertyNamingPolicy = null;
//不使用驼峰样式的key
options.JsonSerializerOptions.DictionaryKeyPolicy = null;
//获取或设置要在转义字符串时使用的编码器
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
});

bool型转换问题

.netcore 3.1中接收的json中不能将 "true"/"false"识别为boolean的True/False,这也需要自定义Converter实现bool转换

namespace Kdniao.Core.Utility
{
public class BoolJsonConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False)
return reader.GetBoolean(); return bool.Parse(reader.GetString());
} public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
}

System.Text.Json 自定义Converter实现时间转换的更多相关文章

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

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

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

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

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

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

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

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

  5. 【译】System.Text.Json 的下一步是什么

    .NET 5.0 最近发布了,并带来了许多新特性和性能改进.System.Text.Json 也不例外.我们改进了性能和可靠性,并使熟悉 Newtonsoft.Json 的人更容易采用它.在这篇文章中 ...

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

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

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

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

  8. c# System.Text.Json 精讲

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...

  9. .NET 5的System.Text.Json的JsonDocument类讲解

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...

随机推荐

  1. Java容器解析系列(14) IdentityHashMap详解

    IdentityHashMap,使用什么的跟HashMap相同,主要不同点在于: 数据结构:使用一个数组table来存储 key:value,table[2k] 为key, table[2k + 1] ...

  2. go编写简单接口的过程

    环境 系统 Windows server 2016 Datacener go version go1.13.3 windows/amd64 数据库 Microsoft SQL Server 2014( ...

  3. Vmware上安装Linux(centos7)图文教程

    Vmware上安装Linux(centos7)图文教程   一.准备安装文件(vmware && centos7 镜像) 1.下载  vmware workstations :链接: ...

  4. python笔记11

    今日内容 函数小高级 lambda 表达式 内置函数 内容回顾 函数基本结构 参数 形参 基本参数:def func(a1,a2):pass 默认值:def func(a1,a2=123):pass ...

  5. pycharm版本下载地址

    https://www.runoob.com/w3cnote/pycharm-windows-install.html   下载社区版本,因为免费 其余按照默认安装即可

  6. MySQL Router单点隐患通过Keepalived实现

    目录 一.介绍 二.环境准备 三.安装步骤 3.1下载软件包,解压 3.2源码安装 3.3配置keepalived 3.4修改keepalived配置文件 3.5启动keepalived 3.6查看V ...

  7. SVN: 聚合工程下的子工程无法使用 svn:ignore

    当想将聚合工程manager下子工程没用的一些文件使用svn:ignore,发现该功能不能使用 这是因为SVN 服务器上还没有这些子工程的文件夹,只有聚合工程的文件夹,所以SVN认为在服务器上这些代码 ...

  8. Topics类型配置

    配置项 备注 segment.bytes 分段文件大小,最大2GB segment.ms 强制新建段文件间隔阀值时间 segment.jitter.ms 段文件抖动时间 segment.index.b ...

  9. vuex之getter(二)

    说明 使用vue,如果想对data数据派生一些状态,我们就用到计算属性或者侦听器,同样vux想要派生state中的一些状态,可以在store中定义一个getters属性,它相当于state的计算属性. ...

  10. 用Python在Linux下调用新中新DKQ-A16D读卡器,读二代证数据

    1.背景 最近在研究二代证读卡器,手头上的设备是新中新DKQ-A16D,在官网(https://www.onecardok.com.cn/download)逛了一圈,发现Win下的示例,浏览器插件很多 ...