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

  使用起来其实也很简单,就是有些地方要注意,比如在我们的对象实体中有中文的话,直接序列化时中文会被转换成编码格式,时间格式序列化时会被转成默认的格式等。

  下面就直接上Demo的代码了

using System;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode; namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); var user = new User{
Id= ,
Name = "张三李四",
Gender = Gender.Male,
Email="Jesen@qq.com",
CreatedTime = DateTime.Now
}; JsonSerializerOptions options = new JsonSerializerOptions();
options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); //解决中文序列化被编码的问题
options.Converters.Add(new DateTimeConverter()); //解决时间格式序列化的问题 var serializeString = JsonSerializer.Serialize(user, options);
Console.WriteLine(serializeString); User obj = JsonSerializer.Deserialize<User>(serializeString,options); Console.ReadLine();
}
} class User
{
public int Id { get; set; } public string Name { get; set; } [JsonConverter(typeof(JsonStringEnumConverter))] //解决枚举序列化时被转成数值的问题
public Gender Gender { get; set; } public string Email { get; set; } public DateTime CreatedTime { get; set; }
} enum Gender
{
Male, FaleMale
} public class DateTimeConverter : JsonConverter<DateTime>
{
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.ToString("yyyy-MM-dd HH:mm:ss"));
}
} public class DateTimeNullableConverter : JsonConverter<DateTime?>
{
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return string.IsNullOrEmpty(reader.GetString()) ? default(DateTime?) : DateTime.Parse(reader.GetString());
} public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
}
} }

.net core中关于System.Text.Json的使用的更多相关文章

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

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

  2. 使用.Net6中的System.Text.Json遇到几个常见问题及解决方案

    前言 以前.NetCore是不内置JSON库的,所以大家都用Newtonsoft的JSON库,而且也确实挺好用的,不过既然官方出了标准库,那更方便更值得我们多用用,至少不用每次都nuget安装Newt ...

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

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

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

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

  5. [译]试用新的System.Text.Json API

    译注 可能有的小伙伴已经知道了,在.NET Core 3.0中微软加入了对JSON的内置支持. 一直以来.NET开发者们已经习惯使用Json.NET这个强大的库来处理JSON. 那么.NET为什么要增 ...

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

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

  7. C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize

    In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...

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

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

  9. Net core 2.x 升级 3.0 使用自带 System.Text.Json 时区 踩坑经历

    .Net Core 3.0 更新的东西很多,这里就不多做解释了,官方和博园大佬写得很详细 关于 Net Core 时区问题,在 2.1 版本的时候,因为用的是 Newtonsoft.Json,配置比较 ...

随机推荐

  1. 坑:jmeter代理服务器录制脚本出现target controller is configured to "use recording Controller" but no such controller exists...

    配置好代理服务器后,运行代理服务器 run 报错: target controller is configured to "use recording Controller" bu ...

  2. Large-Scale Oil Palm Tree Detection from High-Resolution Satellite Images Using Two-Stage Convolutional Neural Networks(worldview油棕树检测)

    不是目标检测也不是语义分割,两步CNN指的是,采集的数据是一堆点,以点为中心的65*65和17*17图像范围大小来判断这个点是否是油棕树.第一步就是判断65*65的范围是否为(油棕树植被群,其他植被/ ...

  3. Visual Detail Augmented Mapping for Small Aerial Target Detection(航片动态小目标检测)

    1.介绍 航片里小目标占总像元数不足1%,普通目标检测算法如YOLO会有很多错误,主要原因有3点: 1.航片的无关背景占多数 2.目标大小由于飞行高度和拍摄角度不同 3.航片中的小移动目标和噪音会混淆 ...

  4. kubernetes 1.14安装部署helm插件

    简单介绍: Helm其实就是一个基于Kubernetes的程序包(资源包)管理器,它将一个应用的相关资源组织成为Charts,并通过Charts管理程序包.再简单点说,可以当做RHEL/CentOS系 ...

  5. localstorage和cookie的设置方法和获取方法

    1.设置localStorage window.localStorage.setItem(vm.mobileSelf,JSON.stringify(contactInfo)); vm.mobileSe ...

  6. Java NIO?看这一篇就够了!

    现在使用NIO的场景越来越多,很多网上的技术框架或多或少的使用NIO技术,譬如Tomcat,Jetty.学习和掌握NIO技术已经不是一个JAVA攻城狮的加分技能,而是一个必备技能.在前面2篇文章< ...

  7. Software Project Management_JUnit && Maven

    Task1: Develop the project “HelloWorld” -A .java program: Just print out “Hello” + your name; -A tes ...

  8. C格式字符串转为二叉树

    最近在LeetCode做题,二叉树出现错误时不好排查,于是自己写了一个函数,将前序遍历格式字串转换成二叉树. 形如 "AB#D##C##" 的字符串,"#"表示 ...

  9. sublime text3中如何使用PHP编译系统

     [WinError 2] 系统找不到指定的文件 编译错误原因,是因为编译器没有配置 第一步: 添加php可执行程序所在目录到系统环境变量(具体方法此处省略,使用本文下面的说明中的方法,此步骤可以省略 ...

  10. Android中jsoup的混淆规则【转】

    Android中jsoup的混淆规则版权声明:转载必须注明本文转自严振杰的博客:http://blog.yanzhenjie.com 说实话这篇文章的标题和内容我觉得很水,所以读者们要是也觉得这篇文章 ...