Skip to content

Commit

Permalink
Added a new JSON converter to handle nullable DateTime values. This w…
Browse files Browse the repository at this point in the history
…as to handle the case for AACQU, which brought all values back as null, except for the company name. (#75)

Co-authored-by: Tim McCarthy <[email protected]>
  • Loading branch information
tmccart1 and Tim McCarthy authored Oct 11, 2020
1 parent b4937de commit 7f72bcd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
3 changes: 3 additions & 0 deletions IEXSharp/Helper/ExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ internal static JsonSerializerOptions JsonSerializerOptions
get
{
if (jsonSerializerOptions != null)
{
return jsonSerializerOptions;
}
else
{
jsonSerializerOptions = new JsonSerializerOptions
Expand All @@ -34,6 +36,7 @@ internal static JsonSerializerOptions JsonSerializerOptions
jsonSerializerOptions.Converters.Add(new Int64Converter());
jsonSerializerOptions.Converters.Add(new DecimalConverter());
jsonSerializerOptions.Converters.Add(new StringConverter());
jsonSerializerOptions.Converters.Add(new DateTimeConverter());
return jsonSerializerOptions;
}
}
Expand Down
68 changes: 57 additions & 11 deletions IEXSharp/Helper/JSONConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,36 @@ public override string Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
else if (reader.TokenType == JsonTokenType.True)
{
return "true";
}
else if (reader.TokenType == JsonTokenType.False)
{
return "false";
}
else if (reader.TokenType == JsonTokenType.Number)
{
if (reader.TryGetInt64(out long l))
{
return l.ToString();
else return reader.GetDecimal().ToString();
}
else
{
return reader.GetDecimal().ToString();
}
}
else if (reader.TokenType == JsonTokenType.Null)
{
return null;
else throw new JsonException();
}
else
{
throw new JsonException();
}
}

public override void Write(Utf8JsonWriter writer, string value,
Expand All @@ -42,14 +58,15 @@ public class Int32Converter : JsonConverter<int?>
{
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();
}

Expand All @@ -67,6 +84,37 @@ public override void Write(Utf8JsonWriter writer, int? input,
}
}

public class DateTimeConverter : JsonConverter<DateTime?>
{
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String &&
typeToConvert == typeof(DateTime?))
{
var stringValue = reader.GetString();
if (DateTime.TryParse(stringValue, out var value))
{
return value;
}
return null;
}
throw new JsonException();
}

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

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class Int64Converter : JsonConverter<long?>
{
Expand All @@ -79,13 +127,12 @@ public class Int64Converter : JsonConverter<long?>
{
return value;
}

return null;
}

if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt64();

}
throw new JsonException();
}

Expand Down Expand Up @@ -115,13 +162,12 @@ public class DecimalConverter : JsonConverter<decimal?>
{
return value;
}

return null;
}

if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetDecimal();

}
throw new JsonException();
}

Expand Down
8 changes: 6 additions & 2 deletions IEXSharpTest/Cloud/CoreData/StockResearchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Setup()
[Test]
[TestCase("AACG")]
[TestCase("AACQ")]
[TestCase("AACQU")]
[TestCase("AAPL")]
[TestCase("FB")]
public async Task AdvancedStatsAsyncTest(string symbol)
Expand All @@ -28,8 +29,10 @@ public async Task AdvancedStatsAsyncTest(string symbol)
Assert.IsNull(response.ErrorMessage);
Assert.IsNotNull(response.Data);

Assert.NotNull(response.Data.marketcap);
Assert.NotNull(response.Data.beta);
// Removing these, as AACQU brings back everything as null, except for the company name
//Assert.NotNull(response.Data.marketcap);
//Assert.NotNull(response.Data.beta);
Assert.NotNull(response.Data.companyName);
}

[Test]
Expand Down Expand Up @@ -101,6 +104,7 @@ public async Task InstitutionalOwnerShipAsyncTest(string symbol)
[Test]
[TestCase("AACG")]
[TestCase("AACQ")]
[TestCase("AACQU")]
[TestCase("AAPL")]
[TestCase("FB")]
public async Task KeyStatsAsyncTest(string symbol)
Expand Down

0 comments on commit 7f72bcd

Please sign in to comment.