Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support de-serialization of nullable numbers #71

Merged
Merged
Changes from all commits
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
81 changes: 60 additions & 21 deletions IEXSharp/Helper/JSONConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,109 @@ public class StringConverter : JsonConverter<string>
}

/// <summary> Allow both quoted and unquoted numbers on deserialize. </summary>
public class Int32Converter : JsonConverter<int>
public class Int32Converter : 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)
{
string stringValue = reader.GetString();
if (int.TryParse(stringValue, out int value))
var stringValue = reader.GetString();
if (int.TryParse(stringValue, out var value))
return value;

return null;
}
else if (reader.TokenType == JsonTokenType.Number)

if (reader.TokenType == JsonTokenType.Number)
return reader.GetInt32();

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, int value,
JsonSerializerOptions options) => writer.WriteNumberValue(value);
public override void Write(Utf8JsonWriter writer, int? 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 Int64Converter : JsonConverter<long>
public class Int64Converter : JsonConverter<long?>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string stringValue = reader.GetString();
if (long.TryParse(stringValue, out long value))
var stringValue = reader.GetString();
if (long.TryParse(stringValue, out var value))
{
return value;
}

return null;
}
else if (reader.TokenType == JsonTokenType.Number)

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

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, long value,
JsonSerializerOptions options) => writer.WriteNumberValue(value);
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 class DecimalConverter : JsonConverter<decimal?>
{
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override decimal? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string stringValue = reader.GetString();
if (decimal.TryParse(stringValue, out decimal value))
var stringValue = reader.GetString();
if (decimal.TryParse(stringValue, out var value))
{
return value;
}

return null;
}
else if (reader.TokenType == JsonTokenType.Number)

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

throw new JsonException();
}

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

/// <summary>
Expand Down