Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code review feedback.
  • Loading branch information
CodeBlanch committed Feb 10, 2020
commit 44d65ebb06f55f49c19996c623cf06439e17dc13
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,29 @@ public static partial class CustomConverterTests
/// <summary>
/// Allow both string and number values on deserialize.
/// </summary>
private class Int32Converter : JsonConverterFactory
private class Int32Converter : JsonConverter<int>
{
public override bool CanConvert(Type typeToConvert)
=> typeToConvert == typeof(int);

public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
=> new InternalInt32Converter();

private class InternalInt32Converter : JsonConverter<int>
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
if (reader.TokenType == JsonTokenType.String)
{
if (reader.TokenType == JsonTokenType.String)
string stringValue = reader.GetString();
if (int.TryParse(stringValue, out int value))
{
string stringValue = reader.GetString();
if (int.TryParse(stringValue, out int value))
{
return value;
}
return value;
}
else if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
else if (reader.TokenType == JsonTokenType.Number)
{
writer.WriteNumberValue(value);
return reader.GetInt32();
}

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}

Expand Down