Skip to content

Commit

Permalink
Return empty string instead of null to fix read error in KWGT
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Nov 4, 2024
1 parent c4fc174 commit 311c084
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Converters/DateTimeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace F1Widget.Converters;

public class DateTimeJsonConverter : JsonConverter<DateTime>
public class DateTimeJsonConverter : JsonConverter<DateTime?>
{
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
private const string Format = "dd MMM HH:mm";

public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!DateTime.TryParseExact(reader.GetString(), Format, Culture, DateTimeStyles.None, out var dateTime))
{
Expand All @@ -19,8 +19,10 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
return dateTime;
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("dd MMM HH:mm"));
writer.WriteStringValue(value == null ? string.Empty : value.Value.ToString(Format));
}

public override bool HandleNull => true;
}
Binary file modified widgets/F1Widget_Mclaren_2024_Chrome.kwgt
Binary file not shown.

0 comments on commit 311c084

Please sign in to comment.