使用 System.Text.Json 转换不一致的 json 值

Converting inconsistent json values using System.Text.Json

我正在使用的服务 returns 一个空数组而不是对象的 null。这会在反序列化期间导致错误。

System.Text.Json.JsonException : The JSON value could not be converted to Models.Error. Path: $.errors | LineNumber: 8 | BytePositionInLine: 13.

示例 #1:

{
  "data": {
    "code": 100,
    "message": "Success",
    "authority": "A00000000000000000000000000112233444",
    "fee_type": "Payer",
    "fee": 10
  },
  "errors": []
}

示例 #2:

{
  "data": [],
  "errors": {
    "code": -9,
    "message": "The input params invalid, validation error."
  }
}

这是它想出的:

internal class InconsistentConverter<T> : JsonConverter<T>
{
    public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.StartArray)
        {
            _ = reader.Read();
            return default;
        }

        return JsonSerializer.Deserialize<T>(ref reader, options);
    }

    public override void Write(Utf8JsonWriter writer, T objectToWrite, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

我也想对数据使用转换器:

public class Result<T>
    where T : class
{
    [JsonPropertyName("data")]
    [JsonConverter(typeof(InconsistentConverter<T>))] // can't do this
    public T? Data { get; set; }

    [JsonPropertyName("errors")]
    [JsonConverter(typeof(InconsistentConverter<Error>))] // this works
    public Error? Error { get; set; }
}

我想这在 Json.NET 中会很容易,但不幸的是我不能在这里使用它。

一个快速的解决方法是创建一个非通用版本的转换器:

internal class InconsistentConverter : JsonConverter<object>
{
    public override bool CanConvert(Type typeToConvert) => true;

    public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.StartArray)
        {
            _ = reader.Read();
            return default;
        }

        var deserialize = JsonSerializer.Deserialize(ref reader, typeToConvert, options);
        return deserialize;
    }

    public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

并将其用于通用 属性:

public class Result<T>
    where T : class
{
    [JsonPropertyName("data")]
    [JsonConverter(typeof(InconsistentConverter))]
    public T? Data { get; set; }

    [JsonPropertyName("errors")]
    [JsonConverter(typeof(InconsistentConverter<Error>))] // this works
    public Error? Error { get; set; }
}