The service I'm working with returns an empty array instead of null for objects. Which causes errors during deserialization.
System.Text.Json.JsonException : The JSON value could not be converted to Models.Error. Path: $.errors | LineNumber: 8 | BytePositionInLine: 13.
Sample #1:
{
"data": {
"code": 100,
"message": "Success",
"authority": "A00000000000000000000000000112233444",
"fee_type": "Payer",
"fee": 10
},
"errors": []
}
Sample #2:
{
"data": [],
"errors": {
"code": -9,
"message": "The input params invalid, validation error."
}
}
This is what it came up with:
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();
}
}
I want to use the converter for the data as well:
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; }
}
I guess this would've been really easy in Json.NET but unfortunately I can't use it here.
CodePudding user response:
A quick workaround is to create a non-generic version of your converter:
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();
}
}
And use it for the generic property:
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; }
}