Skip to content

Commit

Permalink
Support de-serialization of nullable numbers (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
amine-mejaouel authored Oct 4, 2020
1 parent b3e16b9 commit f6aee3c
Showing 1 changed file with 60 additions and 21 deletions.
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 override void Write(Utf8JsonWriter writer, string value,
}

/// <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

0 comments on commit f6aee3c

Please sign in to comment.