gpt4 book ai didi

c# - System.Text.Json 将空字符串全局序列化为空字符串

转载 作者:行者123 更新时间:2023-12-04 04:19:23 28 4
gpt4 key购买 nike

将代码从 newtonsoft json 迁移到 system.text.json
我需要所有可为空的字符串呈现为空字符串。
我写了以下转换器,但所有空字符串值仍然呈现为空。
对于空字符串值,不会调用 Write 方法。断点永远不会被击中。

public class EmptyStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> Convert.ToString(reader.GetString(), CultureInfo.CurrentCulture);

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (writer == null)
throw new ArgumentNullException(nameof(writer));
writer.WriteStringValue(value ?? "");
}
}
启动代码
services.AddControllers()
.AddJsonOptions(option =>
{
option.JsonSerializerOptions.Converters.Add(new EmptyStringConverter());
});
控制台示例
class Program
{
static void Main(string[] args)
{
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new EmptyStringConverter());
var json = JsonSerializer.Serialize(new Model() { FirstName = null }, jsonSerializerOptions);
}
}

public class EmptyStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> Convert.ToString(reader.GetString(), CultureInfo.CurrentCulture);

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (writer == null)
throw new ArgumentNullException(nameof(writer));
writer.WriteStringValue(value ?? "");
}
}

public class Model
{
public string FirstName { get; set; }
}

最佳答案

在 .NET 5.0 中 这可以通过覆盖 JsonConverter<T>.HandleNull 来完成并返回 true :

public class EmptyStringConverter : JsonConverter<string>
{
public override bool HandleNull => true;

public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType == JsonTokenType.Null ? "" : reader.GetString();

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) =>
writer.WriteStringValue(value ?? "");
}
更多请见 Handle null values .
演示 fiddle here .
在 .NET Core 3.x 中 这没有实现。来自 Handle null values在 .NET Core 3.x 中:

Handle null values

By default, the serializer handles null values as follows:

  • For reference types and Nullable<T> types:

    • It does not pass null to custom converters on serialization.
    • It does not pass JsonTokenType.Null to custom converters on deserialization.
    • It returns a null instance on deserialization.
    • It writes null directly with the writer on serialization.
  • For non-nullable value types:

    • It passes JsonTokenType.Null to custom converters on deserialization. (If no custom converter is available, a JsonException exception is thrown by the internal converter for the type.)

This null-handling behavior is primarily to optimize performance by skipping an extra call to the converter. In addition, it avoids forcing converters for nullable types to check for null at the start of every Read and Write method override.

关于c# - System.Text.Json 将空字符串全局序列化为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59792850/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com