如何将类字段与 System.Text.Json.JsonSerializer 一起使用?

How to use class fields with System.Text.Json.JsonSerializer?(如何将类字段与 System.Text.Json.JsonSerializer 一起使用?)
本文介绍了如何将类字段与 System.Text.Json.JsonSerializer 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我最近将一个解决方案升级为全部 .NET Core 3,并且我有一个需要类变量为字段的类.这是一个问题,因为新的 System.Text.Json.JsonSerializer 不支持序列化或反序列化字段,而是只处理属性.

I recently upgraded a solution to be all .NET Core 3 and I have a class that requires the class variables to be fields. This is a problem since the new System.Text.Json.JsonSerializer doesn't support serializing nor deserializing fields but only handles properties instead.

有什么方法可以保证下例中的两个最终类具有相同的精确值?

Is there any way to ensure that the two final classes in the example below have the same exact values?

using System.Text.Json;

public class Car
{
    public int Year { get; set; } // does serialize correctly
    public string Model; // doesn't serialize correctly
}

static void Problem() {
    Car car = new Car()
    {
        Model = "Fit",
        Year = 2008,
    };
    string json = JsonSerializer.Serialize(car); // {"Year":2008}
    Car carDeserialized = JsonSerializer.Deserialize<Car>(json);

    Console.WriteLine(carDeserialized.Model); // null!
}

推荐答案

.NET Core 3.x 中,System.Text.Json 不序列化字段.从 文档:

In .NET Core 3.x, System.Text.Json does not serialize fields. From the docs:

.NET Core 3.1 中的 System.Text.Json 不支持字段.自定义转换器可以提供此功能.

Fields are not supported in System.Text.Json in .NET Core 3.1. Custom converters can provide this functionality.

.NET 5 及更高版本中,可以通过设置 JsonSerializerOptions.IncludeFieldstrue 或通过标记要序列化的字段[JsonInclude]:

In .NET 5 and later, public fields can be serialized by setting JsonSerializerOptions.IncludeFields to true or by marking the field to serialize with [JsonInclude]:

using System.Text.Json;

static void Main()
{
    var car = new Car { Model = "Fit", Year = 2008 };

    // Enable support
    var options = new JsonSerializerOptions { IncludeFields = true };

    // Pass "options"
    var json = JsonSerializer.Serialize(car, options);

    // Pass "options"
    var carDeserialized = JsonSerializer.Deserialize<Car>(json, options);

    Console.WriteLine(carDeserialized.Model); // Writes "Fit"
}

public class Car
{
    public int Year { get; set; }
    public string Model;
}

详情见:

  • 如何在 .NET 中序列化和反序列化(编组和解组)JSON:包括字段.

问题 #34558 和 #876.

这篇关于如何将类字段与 System.Text.Json.JsonSerializer 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)