【问题标题】:system text json use standard deserialization系统文本 json 使用标准反序列化
【发布时间】:2021-04-15 23:42:53
【问题描述】:

我使用的是 .net core 5,而不是 NEWTONSOFT。我正在编写一个自定义序列化程序,因为我的 json 的大小写输出很奇怪;但我不介意使用标准的解串器。

我有以下代码,遵循关于编写自定义 jsonConverters 的微软指南:

public class ThingConverter : JsonConverter<Thing>{
  public override void Write(Utf8JsonWriter writer, Thing t, JsonSerializerOptions options){
    // do stuff, this is fine
  }
  public override Thing Read(ref Utf8Reader reader, Type typeToConvert, JsonSerializerOptions options){
    // i want the DEFAULT behaviour here.
  }
}

有人知道如何为读者设置默认行为吗?

我尝试过的事情:

  • return JsonSerializer.Deserialize&lt;Thing&gt;(ref reader, options) -> 给出一个无限循环
  • 添加public override bool CanConvert(Type objectType){return false;} -> 引发运行时错误,无法转换

fwiw,我正在使用声明

[JsonConverter(typeof(ThingConverter))]
public class Thing{
}

全局注册转换器。

【问题讨论】:

标签: c# json


【解决方案1】:

如果您只想更改属性名称,请使用[JsonPropertyName("Name")]https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多