.NET Core中的System.Text.Json和新的内置JSON支持

In a world where JSON (JavaScript Object Notation) is everywhere it's long been somewhat frustrating that .NET didn't have built-in JSON support. JSON.NET is great and has served us well but it's remained a 3rd party dependency for basic stuff like an ASP.NET web site or a simple console app.

在一个无处不在的JSON(JavaScript对象表示法)的世界中,.NET没有内置的JSON支持早已令人感到沮丧。 JSON.NET很棒,为我们服务很好,但它仍然是ASP.NET网站或简单控制台应用程序等基本内容的第三方依赖。

Back in 2018 plans were announced to move JSON into .NET Core 3.0 as an intrinsic supported feature, and while they're at it, get double the performance or more with Span<T> support and no memory allocations. ASP.NET in .NET Core 3.0 removes the JSON.NET dependency but still allows you to add it back in a single line if you'd like.

早在2018年,就宣布了计划将JSON迁移到.NET Core 3.0中,作为一项固有的受支持功能,而在使用此功能时,通过Span <T>支持和不分配内存的性能将提高一倍甚至更多。 .NET Core 3.0中的ASP.NET删除了JSON.NET依赖关系,但仍允许您根据需要将其添加回一行。

NOTE: This is all automatic and built in with .NET Core 3.0, but if you’re targeting .NET Standard or .NET Framework. Install the System.Text.Json NuGet package (make sure to include previews and install version 4.6.0-preview6.19303.8 or higher). In order to get the integration with ASP.NET Core, you must target .NET Core 3.0.

注意:这都是自动的,并且是使用.NET Core 3.0内置的,但是如果您要使用.NET Standard或.NET Framework的话。 安装System.Text.Json NuGet软件包(确保包括预览并安装版本4.6.0-preview6.19303.8或更高版本)。 为了与ASP.NET Core集成,必须以.NET Core 3.0为目标。

It's very clean as well. Here's a simple example.

也很干净。 这是一个简单的例子。

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

namespace verysmall
{
class WeatherForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
}

class Program
{
static void Main(string[] args)
{
var w = new WeatherForecast() { Date = DateTime.Now, TemperatureC = 30, Summary = "Hot" };
Console.WriteLine(JsonSerializer.Serialize<WeatherForecast>(w));
}
}
}

The default options result in minified JSON as well.

默认选项也会导致缩小的JSON。

{"Date":"2019-07-27T00:58:17.9478427-07:00","TemperatureC":30,"Summary":"Hot"}      

Of course, when you're returning JSON from a Controller in ASP.NET it's all automatic and with .NET Core 3.0 it'll automatically use the new System.Text.Json unless you override it.

当然,当您从ASP.NET中的Controller返回JSON时,它是完全自动的,并且在.NET Core 3.0中,它将自动使用新的System.Text.Json,除非您将其覆盖。

Here's an example where we pull out some fake Weather data (5 randomly created reports) and return the array.

这是一个示例,其中我们提取了一些虚假的Weather数据(5个随机创建的报告)并返回数组。

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}

The application/json is used and JSON is returned by default. If the return type was just string, we'd get text/plain. Check out this YouTube video to learn more details about System.Text.Json works and how it was designed. I'm looking forward to working with it more!

默认使用application / json并返回JSON。 如果返回类型只是字符串,我们将获得文本/纯文本。 观看此YouTube视频,以了解有关System.Text.Json的工作原理以及如何设计的更多详细信息。 我期待着与它合作!

Sponsor: Get the latest JetBrains Rider with WinForms designer, Edit & Continue, and an IL (Intermediate Language) viewer. Preliminary C# 8.0 support, rename refactoring for F#-defined symbols across your entire solution, and Custom Themes are all included.

赞助商:使用WinForms设计器,“编辑并继续”和IL(中间语言)查看器,获取最新的JetBrains Rider 。 初步的C#8.0支持,整个解决方案中F#定义符号的重命名重构以及自定义主题都包括在内。

翻译自: https://www.hanselman.com/blog/systemtextjson-and-new-builtin-json-support-in-net-core

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值