Skip to content

Commit

Permalink
started using new JsonSerializerDefaults.Web in System.Text.Json 5.0.1
Browse files Browse the repository at this point in the history
- https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializerdefaults?view=net-5.0#fields
- removed JSONConverters: Int32, Int64, Decimal, Decimal?
- updated dependencies to EventSource 4.0.0, Polly 7.2.2, System.Text.Json 5.0.1
  • Loading branch information
vslee committed Apr 22, 2021
1 parent bfff65f commit 21e22b1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 145 deletions.
10 changes: 1 addition & 9 deletions IEXSharp/Helper/ExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,8 @@ internal static JsonSerializerOptions JsonSerializerOptions
}
else
{
jsonSerializerOptions = new JsonSerializerOptions
{
IgnoreNullValues = true,
PropertyNameCaseInsensitive = true,
};
jsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
jsonSerializerOptions.Converters.Add(new DictionaryDatetimeTValueConverter());
jsonSerializerOptions.Converters.Add(new Int32Converter());
jsonSerializerOptions.Converters.Add(new Int64Converter());
jsonSerializerOptions.Converters.Add(new DecimalConverter());
jsonSerializerOptions.Converters.Add(new DecimalNullableConverter());
jsonSerializerOptions.Converters.Add(new StringConverter());
jsonSerializerOptions.Converters.Add(new DateTimeConverter());
return jsonSerializerOptions;
Expand Down
133 changes: 0 additions & 133 deletions IEXSharp/Helper/JSONConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,6 @@ public override void Write(Utf8JsonWriter writer, string value,
JsonSerializerOptions options) => writer.WriteStringValue(value);
}

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class Int32Converter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
if (int.TryParse(stringValue, out var value))
{
return value;
}
return null;
}
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}
throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, int? input,
JsonSerializerOptions options)
{
if (input.HasValue)
{
writer.WriteNumberValue(input.Value);
}
else
{
writer.WriteNullValue();
}
}
}

public class DateTimeConverter : JsonConverter<DateTime?>
{
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down Expand Up @@ -122,104 +87,6 @@ public override void Write(Utf8JsonWriter writer, DateTime? input,
}
}

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class Int64Converter : JsonConverter<long?>
{
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
if (long.TryParse(stringValue, out var value))
{
return value;
}
return null;
}
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt64();
}
throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, long? input,
JsonSerializerOptions options)
{
if (input.HasValue)
{
writer.WriteNumberValue(input.Value);
}
else
{
writer.WriteNullValue();
}
}
}

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class DecimalConverter : JsonConverter<decimal>
{
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
if (decimal.TryParse(stringValue, out var value))
{
return value;
}
throw new JsonException();
}
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetDecimal();
}
throw new JsonException();
}

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

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class DecimalNullableConverter : JsonConverter<decimal?>
{
public override decimal? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
if (decimal.TryParse(stringValue, out var value))
{
return value;
}
return null;
}
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetDecimal();
}
throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, decimal? input,
JsonSerializerOptions options)
{
if (input.HasValue)
{
writer.WriteNumberValue(input.Value);
}
else
{
writer.WriteNullValue();
}
}
}

/// <summary>
/// Allow dictionaries with key of type DateTime (System.Text.Json only supports keys of type string)
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions IEXSharp/IEXSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LaunchDarkly.EventSource" Version="3.3.2" />
<PackageReference Include="Polly" Version="7.2.1" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="LaunchDarkly.EventSource" Version="4.0.0" />
<PackageReference Include="Polly" Version="7.2.2" />
<PackageReference Include="System.Text.Json" Version="5.0.1" />

<!--development dependency, which means it is only used during build, the final NuGet package does not have a dependency on SourceLink-->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
Expand Down
1 change: 1 addition & 0 deletions IEXSharpTest/Cloud/CoreData/StockPricesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task DelayedQuoteAsyncTest(string symbol)
[TestCase("AAPL", ChartRange.Max)]
[TestCase("AAPL", ChartRange.Ytd)]
[TestCase("AAPL", ChartRange.OneMonth)]
[TestCase("WWR", ChartRange.TwoYears)]
public async Task HistoricalPriceAsync(string symbol,
ChartRange range = ChartRange.OneMonth, QueryStringBuilder qsb = null)
{
Expand Down

0 comments on commit 21e22b1

Please sign in to comment.