Skip to content

Commit d17d9e8

Browse files
committed
Add converter options that not to throw on exception in converters, Add json hex string to color conversion
1 parent d6644ee commit d17d9e8

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

API.Test/ExtensionsTests/DateTimeExtensionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public static void FromWebUntisTimeFormatTest()
4343
// Example 1
4444
string E1_Date = "2023-05-31";
4545
string E1_Time = "2000";
46-
DateTime E1_DateTime = new DateTime().FromWebUntisTimeFormat(E1_Date, E1_Time);
46+
DateTime? E1_DateTime = new DateTime().FromWebUntisTimeFormat(E1_Date, E1_Time);
4747
Assert.That(new DateTime(2023, 5, 31, 20, 0, 0), Is.EqualTo(E1_DateTime));
4848

4949
// Example 2
5050
string E2_Date = "2023-06-01";
5151
string E2_Time = "740";
52-
DateTime E2_DateTime = new DateTime().FromWebUntisTimeFormat(E2_Date, E2_Time);
52+
DateTime? E2_DateTime = new DateTime().FromWebUntisTimeFormat(E2_Date, E2_Time);
5353
Assert.That(new DateTime(2023, 6, 1, 7, 40, 0), Is.EqualTo(E2_DateTime));
5454
}
5555
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Text.RegularExpressions;
9+
using System.Threading.Tasks;
10+
using WebUntisAPI.Client.Extensions;
11+
12+
namespace WebUntisAPI.Client.Converter
13+
{
14+
/// <summary>
15+
/// A json converter to convert a <see cref="Color"/> to the Hex color format (RRGGBB)
16+
/// </summary>
17+
public sealed class ColorJsonConverter : JsonConverter
18+
{
19+
/// <inheritdoc/>
20+
public override bool CanConvert(Type objectType) => objectType == typeof(Color) || objectType == typeof(string);
21+
22+
/// <inheritdoc/>
23+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
24+
{
25+
// Get value
26+
JToken jToken = JToken.Load(reader);
27+
string value = jToken.Value<string>();
28+
29+
// Convert value
30+
return new Color().FromHexColorFormat(value, false) ?? (object)value;
31+
}
32+
33+
/// <inheritdoc/>
34+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => ((Color)value).ToHexColorFormat();
35+
}
36+
}

WebUntisAPI.Client/Extensions/ColorExtensions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ public static string ToHexColorFormat(this Color color)
2727
/// Convert a color string to the Hexadecimal format: RRGGBB
2828
/// </summary>
2929
/// <param name="color"></param>
30+
/// <param name="colorString">The string to convert</param>
31+
/// <param name="throwOnException">Throw a exception on validation error. When not to throw on exception and an exception happened is the return value <see langword="null"/></param>
3032
/// <returns>The created color</returns>
3133
/// <exception cref="ArgumentException">Thrown when the <paramref name="colorString"/> isn't in the correct format</exception>
32-
public static Color FromHexColorFormat(this Color color, string colorString)
34+
public static Color? FromHexColorFormat(this Color color, string colorString, bool throwOnException = true)
3335
{
3436
if (!Regex.IsMatch(colorString, @"[\da-fA-F]{6}"))
35-
throw new ArgumentException("The color string isn't in a correct format", nameof(colorString));
37+
{
38+
if (!throwOnException)
39+
return null;
3640

41+
throw new ArgumentException("The color string isn't in a correct format", nameof(colorString));
42+
}
43+
3744
// Convert the hex string to the single color channels
3845
int red = Convert.ToByte(colorString.Substring(0, 2), 16);
3946
int green = Convert.ToByte(colorString.Substring(2, 2), 16);

WebUntisAPI.Client/Extensions/DateTimeExtensions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public static void ToWebUntisTimeFormat(this DateTime dateTime, out string dateS
4242
/// <param name="dateTime">Instance</param>
4343
/// <param name="dateString">Date string</param>
4444
/// <param name="timeString">Time string</param>
45-
/// <returns>The new instance that contains the given time</returns>
45+
/// <param name="throwOnException">Throw a exception on validation error</param>
46+
/// <returns>The new instance that contains the given time. When not to throw on exception and an exception happened is the return value <see langword="null"/></returns>
4647
/// <exception cref="FormatException">Thrown when one of the given strings isn't in the right format</exception>
47-
public static DateTime FromWebUntisTimeFormat(this DateTime dateTime, string dateString, string timeString)
48+
public static DateTime? FromWebUntisTimeFormat(this DateTime dateTime, string dateString, string timeString, bool throwOnException = true)
4849
{
4950
Regex dateRegex = new Regex(@"^\d{4}-(0\d|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$"); // Regex for the WebUntis date format
5051
Regex timeRegex = new Regex(@"^(\d|1\d|2[0-3])[0-5]\d$"); // Regex for the WebUntis time format
@@ -54,7 +55,13 @@ public static DateTime FromWebUntisTimeFormat(this DateTime dateTime, string dat
5455
bool isTimeValid = timeRegex.IsMatch(timeString);
5556

5657
if (!isDateValid || !isTimeValid)
58+
{
59+
if (!throwOnException)
60+
return null;
61+
5762
throw new FormatException($"The string {(isDateValid ? timeString : dateString)} isn't in the valid format!");
63+
}
64+
5865

5966
// Parse the numbers in the string to value
6067
int year = int.Parse(dateString.Substring(0, 4));

0 commit comments

Comments
 (0)