Skip to content

Commit

Permalink
Standardize null handling and error handling in custom Json converters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Apr 12, 2024
1 parent 4599542 commit 9a90d26
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
6 changes: 6 additions & 0 deletions Source/ZoomNet/Json/BooleanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
8 changes: 7 additions & 1 deletion Source/ZoomNet/Json/DateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/ZoomNet/Json/KeyValuePairConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override KeyValuePair<string, string>[] 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<string, string>[] value, JsonSerializerOptions options)
Expand Down
2 changes: 1 addition & 1 deletion Source/ZoomNet/Json/MeetingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override Meeting Read(ref Utf8JsonReader reader, Type typeToConvert, Json
case MeetingType.RecurringNoFixedTime:
return rootElement.ToObject<RecurringMeeting>(options);
default:
throw new Exception($"{meetingType} is an unknown meeting type");
throw new JsonException($"{meetingType} is an unknown meeting type");
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Source/ZoomNet/Json/NullableDateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace ZoomNet.Json
/// <seealso cref="ZoomNetJsonConverter{T}"/>
internal class NullableDateTimeConverter : ZoomNetJsonConverter<DateTime?>
{
private readonly DateTimeConverter _dateTimeConverter = new DateTimeConverter();

public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
Expand All @@ -17,17 +19,16 @@ internal class NullableDateTimeConverter : ZoomNetJsonConverter<DateTime?>
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);
}
}
}
34 changes: 18 additions & 16 deletions Source/ZoomNet/Json/NullableHexColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ public override bool CanConvert(Type typeToConvert)
/// <inheritdoc />
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;
}
}
}

/// <inheritdoc />
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}");
}
}
2 changes: 1 addition & 1 deletion Source/ZoomNet/Json/ParticipantDeviceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/ZoomNet/Json/WebhookEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -306,7 +306,7 @@ private static KeyValuePair<string, object> ConvertJsonPropertyToKeyValuePair(Js
if (value.TryGetInt64(out var longValue)) return new KeyValuePair<string, object>(key, longValue);
if (value.TryGetInt32(out var intValue)) return new KeyValuePair<string, object>(key, intValue);
if (value.TryGetInt16(out var shortValue)) return new KeyValuePair<string, object>(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<string, object>(key, value.GetRawText());
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ZoomNet/Json/WebinarConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override Webinar Read(ref Utf8JsonReader reader, Type typeToConvert, Json
case WebinarType.RecurringNoFixedTime:
return rootElement.ToObject<RecurringWebinar>(options);
default:
throw new Exception($"{webinarType} is an unknown webinar type");
throw new JsonException($"{webinarType} is an unknown webinar type");
}
}
}
Expand Down

0 comments on commit 9a90d26

Please sign in to comment.