diff --git a/IEXSharp/Helper/JSONConverters.cs b/IEXSharp/Helper/JSONConverters.cs index 12af9fe0..e29d64c9 100644 --- a/IEXSharp/Helper/JSONConverters.cs +++ b/IEXSharp/Helper/JSONConverters.cs @@ -35,70 +35,109 @@ public override void Write(Utf8JsonWriter writer, string value, } /// Allow both quoted and unquoted numbers on deserialize. - public class Int32Converter : JsonConverter + public class Int32Converter : JsonConverter { - 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(); + } + } } /// Allow both quoted and unquoted numbers on deserialize. - public class Int64Converter : JsonConverter + public class Int64Converter : JsonConverter { - 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(); + } + } } /// Allow both quoted and unquoted numbers on deserialize. - public class DecimalConverter : JsonConverter + public class DecimalConverter : JsonConverter { - 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(); + } + } } ///