diff --git a/Refit/SystemTextJsonContentSerializer.cs b/Refit/SystemTextJsonContentSerializer.cs index d4c56301f..fe0f66a8a 100644 --- a/Refit/SystemTextJsonContentSerializer.cs +++ b/Refit/SystemTextJsonContentSerializer.cs @@ -80,51 +80,24 @@ public class ObjectToInferredTypesConverter : JsonConverter { public override object? Read( - ref Utf8JsonReader reader, - Type typeToConvert, - JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.True) - { - return true; - } - - if (reader.TokenType == JsonTokenType.False) - { - return false; - } - - if (reader.TokenType == JsonTokenType.Number) - { - if (reader.TryGetInt64(out var l)) - { - return l; - } - - return reader.GetDouble(); - } - - if (reader.TokenType == JsonTokenType.String) - { - if (reader.TryGetDateTime(out var datetime)) - { - return datetime; - } - - return reader.GetString(); - } - - // Use JsonElement as fallback. - // Newtonsoft uses JArray or JObject. - using var document = JsonDocument.ParseValue(ref reader); - return document.RootElement.Clone(); - } + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options) => reader.TokenType switch + { + JsonTokenType.True => true, + JsonTokenType.False => false, + JsonTokenType.Number when reader.TryGetInt64(out var l) => l, + JsonTokenType.Number => reader.GetDouble(), + JsonTokenType.String when reader.TryGetDateTime(out var datetime) => datetime, + JsonTokenType.String => reader.GetString(), + _ => JsonDocument.ParseValue(ref reader).RootElement.Clone() + }; public override void Write( Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) => - throw new InvalidOperationException("Should not get here."); + JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); } }