diff --git a/Source/ZoomNet/Json/BooleanConverter.cs b/Source/ZoomNet/Json/BooleanConverter.cs index 5b0b5944..e63fb15b 100644 --- a/Source/ZoomNet/Json/BooleanConverter.cs +++ b/Source/ZoomNet/Json/BooleanConverter.cs @@ -14,11 +14,17 @@ public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer { switch (reader.TokenType) { + case JsonTokenType.None: + case JsonTokenType.Null: + throw new JsonException($"Unable to convert a null value into a boolean value"); + case JsonTokenType.True: case JsonTokenType.False: return reader.GetBoolean(); + case JsonTokenType.Number: return reader.GetByte() == 1; + default: throw new JsonException($"Unable to convert the content of {reader.TokenType.ToEnumString()} JSON node into a boolean value"); } diff --git a/Source/ZoomNet/Json/DateTimeConverter.cs b/Source/ZoomNet/Json/DateTimeConverter.cs index 593963fa..9eebc008 100644 --- a/Source/ZoomNet/Json/DateTimeConverter.cs +++ b/Source/ZoomNet/Json/DateTimeConverter.cs @@ -13,10 +13,16 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso { switch (reader.TokenType) { + case JsonTokenType.None: + case JsonTokenType.Null: + case JsonTokenType.String when string.IsNullOrEmpty(reader.GetString()): + throw new JsonException($"Unable to convert a null value to DateTime"); + case JsonTokenType.String: return reader.GetDateTime(); + default: - throw new Exception($"Unable to convert {reader.TokenType.ToEnumString()} to DateTime"); + throw new JsonException($"Unable to convert {reader.TokenType.ToEnumString()} to DateTime"); } } diff --git a/Source/ZoomNet/Json/KeyValuePairConverter.cs b/Source/ZoomNet/Json/KeyValuePairConverter.cs index b055092a..bc50af19 100644 --- a/Source/ZoomNet/Json/KeyValuePairConverter.cs +++ b/Source/ZoomNet/Json/KeyValuePairConverter.cs @@ -59,7 +59,7 @@ public override KeyValuePair[] Read(ref Utf8JsonReader reader, T return values.ToArray(); } - throw new Exception("Unable to read Key/Value pair"); + throw new JsonException("Unable to read Key/Value pair"); } public override void Write(Utf8JsonWriter writer, KeyValuePair[] value, JsonSerializerOptions options) diff --git a/Source/ZoomNet/Json/MeetingConverter.cs b/Source/ZoomNet/Json/MeetingConverter.cs index aba36e7f..01e31066 100644 --- a/Source/ZoomNet/Json/MeetingConverter.cs +++ b/Source/ZoomNet/Json/MeetingConverter.cs @@ -28,7 +28,7 @@ public override Meeting Read(ref Utf8JsonReader reader, Type typeToConvert, Json case MeetingType.RecurringNoFixedTime: return rootElement.ToObject(options); default: - throw new Exception($"{meetingType} is an unknown meeting type"); + throw new JsonException($"{meetingType} is an unknown meeting type"); } } } diff --git a/Source/ZoomNet/Json/NullableDateTimeConverter.cs b/Source/ZoomNet/Json/NullableDateTimeConverter.cs index 53410a9b..d3faf33a 100644 --- a/Source/ZoomNet/Json/NullableDateTimeConverter.cs +++ b/Source/ZoomNet/Json/NullableDateTimeConverter.cs @@ -9,6 +9,8 @@ namespace ZoomNet.Json /// internal class NullableDateTimeConverter : ZoomNetJsonConverter { + private readonly DateTimeConverter _dateTimeConverter = new DateTimeConverter(); + public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) @@ -17,17 +19,16 @@ internal class NullableDateTimeConverter : ZoomNetJsonConverter case JsonTokenType.Null: case JsonTokenType.String when string.IsNullOrEmpty(reader.GetString()): return null; - case JsonTokenType.String: - return reader.GetDateTime(); + default: - throw new Exception($"Unable to convert {reader.TokenType.ToEnumString()} to nullable DateTime"); + return _dateTimeConverter.Read(ref reader, typeToConvert, options); } } public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options) { - if (value.HasValue) writer.WriteStringValue(value.Value.ToZoomFormat()); - else writer.WriteNullValue(); + if (!value.HasValue) writer.WriteNullValue(); + else _dateTimeConverter.Write(writer, value.Value, options); } } } diff --git a/Source/ZoomNet/Json/NullableHexColorConverter.cs b/Source/ZoomNet/Json/NullableHexColorConverter.cs index 8c2f3c02..7d4f596e 100644 --- a/Source/ZoomNet/Json/NullableHexColorConverter.cs +++ b/Source/ZoomNet/Json/NullableHexColorConverter.cs @@ -20,26 +20,28 @@ public override bool CanConvert(Type typeToConvert) /// public override Color? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var str = reader.GetString(); - if (string.IsNullOrEmpty(str)) - return null; - str = str.Replace("#", string.Empty); - if (str.Length == 6) - str = $"FF{str}"; - return int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var argB) - ? Color.FromArgb(argB) - : null; + switch (reader.TokenType) + { + case JsonTokenType.None: + case JsonTokenType.Null: + case JsonTokenType.String when string.IsNullOrEmpty(reader.GetString()): + return null; + + default: + { + var str = reader.GetString().Replace("#", string.Empty); + if (str.Length == 6) str = $"FF{str}"; + return int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var argB) + ? Color.FromArgb(argB) + : null; + } + } } /// public override void Write(Utf8JsonWriter writer, Color? value, JsonSerializerOptions options) { - if (value == null) - { - writer.WriteNullValue(); - return; - } - - writer.WriteStringValue($"#{value.Value.R:X2}{value.Value.G:X2}{value.Value.B:X2}"); + if (!value.HasValue) writer.WriteNullValue(); + else writer.WriteStringValue($"#{value.Value.R:X2}{value.Value.G:X2}{value.Value.B:X2}"); } } diff --git a/Source/ZoomNet/Json/ParticipantDeviceConverter.cs b/Source/ZoomNet/Json/ParticipantDeviceConverter.cs index 869f1cc3..ff26e552 100644 --- a/Source/ZoomNet/Json/ParticipantDeviceConverter.cs +++ b/Source/ZoomNet/Json/ParticipantDeviceConverter.cs @@ -28,7 +28,7 @@ public override ParticipantDevice[] Read(ref Utf8JsonReader reader, Type typeToC return items; default: - throw new Exception("Unable to convert to ParticipantDevice"); + throw new JsonException("Unable to convert to ParticipantDevice"); } } diff --git a/Source/ZoomNet/Json/WebhookEventConverter.cs b/Source/ZoomNet/Json/WebhookEventConverter.cs index 9a1f5dff..a881c475 100644 --- a/Source/ZoomNet/Json/WebhookEventConverter.cs +++ b/Source/ZoomNet/Json/WebhookEventConverter.cs @@ -280,7 +280,7 @@ public override Event Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe webHookEvent = endpointUrlValidationEvent; break; default: - throw new Exception($"{eventType} is an unknown event type"); + throw new JsonException($"{eventType} is an unknown event type"); } webHookEvent.EventType = eventType; @@ -306,7 +306,7 @@ private static KeyValuePair ConvertJsonPropertyToKeyValuePair(Js if (value.TryGetInt64(out var longValue)) return new KeyValuePair(key, longValue); if (value.TryGetInt32(out var intValue)) return new KeyValuePair(key, intValue); if (value.TryGetInt16(out var shortValue)) return new KeyValuePair(key, shortValue); - throw new Exception($"Property {key} appears to contain a numerical value but we are unable to determine to exact type"); + throw new JsonException($"Property {key} appears to contain a numerical value but we are unable to determine to exact type"); default: return new KeyValuePair(key, value.GetRawText()); } } diff --git a/Source/ZoomNet/Json/WebinarConverter.cs b/Source/ZoomNet/Json/WebinarConverter.cs index bf84d616..5ae880e2 100644 --- a/Source/ZoomNet/Json/WebinarConverter.cs +++ b/Source/ZoomNet/Json/WebinarConverter.cs @@ -25,7 +25,7 @@ public override Webinar Read(ref Utf8JsonReader reader, Type typeToConvert, Json case WebinarType.RecurringNoFixedTime: return rootElement.ToObject(options); default: - throw new Exception($"{webinarType} is an unknown webinar type"); + throw new JsonException($"{webinarType} is an unknown webinar type"); } } }