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

官方文档

获取JSON库

  • 如果以.NET Core为目标,请安装.NET Core 3.0及以上版本,该版本提供了新的JSON库和ASP.NET Core集成。
  • 如果以.NET Standard或.NET Framework为目标。安装System.Text.Json NuGet软件包(确保安装.NET Framework4.6.0或更高版本)。为了与ASP.NET Core集成,必须以.NET Core 3.0为目标。

NET Core 3.0中JSON特性

新的JSON API通过使用Span进行性能优化,并且可以直接处理UTF-8,而无需转码为UTF-16 string 实例。这两个方面对于ASP.NET Core都是至关重要的,在ASP.NET Core中,吞吐量是关键要求。使用System.Text.Json,可以将速度提高大约1.3倍至5倍。

使用System.Text.Json

using System.Text.Json;
using System.Text.Json.Serialization;

使用序列化器Serializer

  • 学习.Net Core最好的方式是查看源码,下面是JsonSerializer Serialize的部分源码:
namespace System.Text.Json
{
public static partial class JsonSerializer
{
/// <summary>
/// Convert the provided value into a <see cref="System.String"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="options">Options to control the conversion behavior.</param>
/// <remarks>Using a <see cref="System.String"/> is not as efficient as using UTF-8
/// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes"/>
/// and <see cref="SerializeAsync"/>.
/// </remarks>
public static string Serialize<TValue>(TValue value, JsonSerializerOptions options = null)
{
return ToStringInternal(value, typeof(TValue), options);
} /// <summary>
/// Convert the provided value into a <see cref="System.String"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> representation of the value.</returns>
/// <param name="value">The value to convert.</param>
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
/// <param name="options">Options to control the conversion behavior.</param>
/// <remarks>Using a <see cref="System.String"/> is not as efficient as using UTF-8
/// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes"/>
/// and <see cref="SerializeAsync"/>.
/// </remarks>
public static string Serialize(object value, Type inputType, JsonSerializerOptions options = null)
{
VerifyValueAndType(value, inputType); return ToStringInternal(value, inputType, options);
} private static string ToStringInternal(object value, Type inputType, JsonSerializerOptions options)
{
return WriteCoreString(value, inputType, options);
}
}
}

可以看到序列化最终调用的是ToStringInternal这个方法。

  • 示例
class Sample
{
public DateTimeOffset Date { get; set; }
public string Summary { get; set; }
} string Serialize(Sample value)
{
return JsonSerializer.Serialize<Sample>(value);
}
//obj
string SerializeObj(object value)
{
return JsonSerializer.Serialize(value);
}

JsonSerializerOptions用于在序列化时设置配置, 例如处理注释,null处理,尾随逗号和命名策略。

 public static string ToJson(this object obj)
{
var options = new JsonSerializerOptions() { IgnoreNullValues = true, WriteIndented = true, AllowTrailingCommas = true };
return JsonSerializer.Serialize(obj, options);
}

使用反序列化器Deserialize

 Sample Deserialize(string json)
{
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true
}; return JsonSerializer.Deserialize<Sample>(json, options);
}

自定义属性来控制序列化行为

可以使用自定义属性来控制序列化行为,例如,忽略属性或者指定属性的名称:

class Sample
{
//忽略
[JsonIgnore]
public DateTimeOffset Date { get; set; }
//指定名称
[JsonPropertyName("test")]
public string Summary { get; set; }
}

使用DOM

有时,不想反序列化JSON但仍然希望对其内容进行结构化访问,这时可以使用JsonDocument

var options = new JsonDocumentOptions
{
AllowTrailingCommas = true
};
using (JsonDocument document = JsonDocument.Parse(json, options))
{
//todo
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
DateTimeOffset date = element.GetProperty("date").GetDateTimeOffset();
//todo
}
}

使用Utf8JsonWriter

var options = new JsonWriterOptions
{
Indented = true
}; using (var stream = new MemoryStream())
{
using (var writer = new Utf8JsonWriter(stream, options))
{
writer.WriteStartObject();
writer.WriteString("date", DateTimeOffset.UtcNow);
writer.WriteEndObject();
} string json = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(json);
}

使用Utf8JsonReader

byte[] data = Encoding.UTF8.GetBytes(json);
Utf8JsonReader reader = new Utf8JsonReader(data, isFinalBlock: true, state: default); while (reader.Read())
{
Console.Write(reader.TokenType); switch (reader.TokenType)
{
case JsonTokenType.PropertyName:
case JsonTokenType.String:
{
string text = reader.GetString();
Console.Write(" ");
Console.Write(text);
break;
} case JsonTokenType.Number:
{
int value = reader.GetInt32();
Console.Write(" ");
Console.Write(value);
break;
}
} Console.WriteLine();
}

System.Text.Json中的DateTime和DateTimeOffset支持

The System.Text.Json library parses and writes DateTime and DateTimeOffset values according to the ISO 8601:-2019 extended profile. Converters provide custom support for serializing and deserializing with JsonSerializer. Custom support can also be implemented when using Utf8JsonReader and Utf8JsonWriter.

具体请参考官方文档

示例:

 public class DateTimeConverterUsingDateTimeParse : JsonConverter<DateTime>
{
/// <summary>
/// 日期格式
/// </summary>
public string dateTimeFormat { get; }
/// <summary>
/// ctor
/// </summary>
/// <param name="dateTimeFormat"></param>
public DateTimeConverterUsingDateTimeParse(string dateTimeFormat)
{
this.dateTimeFormat = dateTimeFormat;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(DateTime));
return DateTime.Parse(reader.GetString());
} public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(dateTimeFormat));
}
}
 public static class JsonHelper
{
/// <summary>Converts to json.</summary>
/// <param name="obj">The object.</param>
/// <param name="dateTimeFormat">The date time format.</param>
/// <returns>System.String.</returns>
public static string ToJson(this object obj, string dateTimeFormat = "yyyy-MM-dd")
{
var options = new JsonSerializerOptions() { IgnoreNullValues = true, WriteIndented = true };
options.Converters.Add(new DateTimeConverterUsingDateTimeParse(dateTimeFormat));
return JsonSerializer.Serialize(obj, options);
}
}

总结

  • 在.NET Core 3.0中,System.Text.Json API提供对JSON的内置支持,包括reader/writer,只读DOM和序列化器/反序列化器。
  • 主要目标是实现高性能和低内存分配。
  • ASP.NET Core 3.0包括对System.Text.Json的支持,默认情况下启用。

补充

有博友提问了下图中的几个问题:

当前API版本反序列化不会将JSON字符串中的数字强制转换;但是JsonConverter转换器功能可以逐个属性有效地选择转换:

public class NumberToStringConverter : JsonConverter<int>
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var number = 0;
if (reader.TokenType == JsonTokenType.String)
{
if (Int32.TryParse(reader.GetString(), out number))
return number;
}
return reader.GetInt32();
} public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
} class Sample
{
public string Date { get; set; }
public int Summary { get; set; }
}
var json = "{\"Date\":\"2019-10-31\",\"Summary\":\"1\"}";
var options = new JsonSerializerOptions() { IgnoreNullValues = true, WriteIndented = true };
options.Converters.Add(new NumberToStringConverter());
var deserialize = JsonSerializer.Deserialize<Sample>(json, options);

当前API版本支持System.Text.Json 支持Dictionary<key,value>序列化。

Dictionary<string, object> dictionary = new Dictionary<string, object>
{
{"1", 2}
};
var serialize = JsonSerializer.Serialize(dictionary);
var temjson = "{\"1\":2}";
var deserializeDictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(temjson);

在.Net Core 3.0中尝试新的System.Text.Json API的更多相关文章

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

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

  2. 【译】.NET Core 3.0 中的新变化

    .NET Core 3.0 是 .NET Core 平台的下一主要版本.本文回顾了 .Net Core 发展历史,并展示了它是如何从基本支持 Web 和数据工作负载的版本 1,发展成为能够运行 Web ...

  3. [转]【译】.NET Core 3.0 中的新变化

    .NET Core 3.0 是 .NET Core 平台的下一主要版本.本文回顾了 .Net Core 发展历史,并展示了它是如何从基本支持 Web 和数据工作负载的版本 1,发展成为能够运行 Web ...

  4. ASP.NET Core 3.0中使用动态控制器路由

    原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...

  5. 在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据

    在.Net Core 3.0中 内置了一套Json序列化/反序列化方案,默认可以不再依赖,不再支持   Newtonsoft.Json. 但是.NET Core 3.0 System.Text.Jso ...

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

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

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

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

  8. 探索 ASP.Net Core 3.0系列三:ASP.Net Core 3.0中的Service provider validation

    前言:在本文中,我将描述ASP.NET Core 3.0中新的“validate on build”功能. 这可以用来检测您的DI service provider是否配置错误. 具体而言,该功能可检 ...

  9. 避免在ASP.NET Core 3.0中为启动类注入服务

    本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingE ...

随机推荐

  1. SpringBootSecurity学习(01)网页版登录入门介绍

    Web应用安全管理 Web应用的安全管理,主要包括两个方面的内容,一个是用户身份的认证,即用户登录的设计,二是用户授权,即一个用户在一个应用系统中能够执行哪些操作的权限管理.权限管理的设计一般使用角色 ...

  2. springboot系列之04-提高开发效率必备工具lombok

    未经允许,不得转载 原作者:字母哥博客 本文完整系列出自:springboot深入浅出系列 一.前置说明 本节大纲 使用lombok插件的好处 如何安装lombok插件 使用lombok提高开发效率 ...

  3. 【SQL server基础】初步学习存储过程(好学易懂)

    -------------------------------------------------------------------------- ------------------------- ...

  4. 只要听说过电脑的人都能看懂的网上pdf全书获取项目

    作者:周奇 最近我要获取<概统>的教材自学防挂科(线代已死),于是我看到 htt链ps:/链/max链.book接118接.com接/html/2018/0407/160495927.sh ...

  5. Kafka 异步消息也会阻塞?记一次 Dubbo 频繁超时排查过程

    线上某服务 A 调用服务 B 接口完成一次交易,一次晚上的生产变更之后,系统监控发现服务 B 接口频繁超时,后续甚至返回线程池耗尽错误 Thread pool is EXHAUSTED.因为服务 B ...

  6. SpringCache - 请求级别缓存的简易实现

    前言 在SpringCache缓存初探中我们研究了如何利用spring cache已有的几种实现快速地满足我们对于缓存的需求.这一次我们有了新的更个性化的需求,想在一个请求的生命周期里实现缓存. 需求 ...

  7. 鱼和熊掌可兼得?一文看懂又拍云 SCDN

    转眼已是 9102 年,参与工作多年的二狗子凭借他聪明的脑瓜和孜孜不倦的钻研精神,成为了某中型企业的资深网站管理员.不同于一般的"网管",二狗子自然是业内最优秀的那一类. 但是,最 ...

  8. Linux被中断的系统调用

    慢系统调用,指的是可能永远无法返回,从而使进程永远阻塞的系统调用,比如无客户连接时的accept.无输入时的read都属于慢速系统调用. 在Linux中,当阻塞于某个慢系统调用的进程捕获一个信号,则该 ...

  9. 服务器配置https协议,三种免费的方法

    最近想搞一个网站玩玩,发布网站用https协议已经是大势所趋了.例如微信小程序,不使用https协议根本不让接入.所以,分享一下我尝试过的三种方法. 1.Linux自签(OPENSSL生成SSL自签证 ...

  10. guava缓存批量获取的一个坑

    摘要 Guava Cache是Google开源的Java工具集库Guava里的一款缓存工具,一直觉得使用起来比较简单,没想到这次居然还踩了一个坑 背景 功能需求抽象出来很简单,就是将数据库的查询sth ...