Skip to content

Commit

Permalink
Merge pull request #1088 from reactiveui/net5-converter-fix
Browse files Browse the repository at this point in the history
Update inferred types converter for 5.0
  • Loading branch information
clairernovotny authored Feb 23, 2021
2 parents 249f23f + c3e96b2 commit 5951e93
Showing 1 changed file with 13 additions and 40 deletions.
53 changes: 13 additions & 40 deletions Refit/SystemTextJsonContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,51 +80,24 @@ public class ObjectToInferredTypesConverter
: JsonConverter<object>
{
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);
}

}
Expand Down

0 comments on commit 5951e93

Please sign in to comment.