From 2fe54da1c55220f32c98a38e1daf8df7f7c98732 Mon Sep 17 00:00:00 2001 From: Simon Gruber Date: Tue, 14 Apr 2026 16:55:08 +0200 Subject: [PATCH] Ignore null values --- ToonSharp/ToonSerializer.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ToonSharp/ToonSerializer.cs b/ToonSharp/ToonSerializer.cs index 2ddebac..e1af8a2 100644 --- a/ToonSharp/ToonSerializer.cs +++ b/ToonSharp/ToonSerializer.cs @@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace ToonSharp; @@ -9,6 +10,11 @@ namespace ToonSharp; /// public static class ToonSerializer { + private static readonly JsonSerializerOptions jsonOptions = new() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + /// /// Converts the provided value to a TOON string. /// @@ -21,7 +27,7 @@ public static class ToonSerializer options ??= ToonSerializerOptions.Default; // Convert to JsonNode first for normalization - var jsonNode = JsonSerializer.SerializeToNode(value); + var jsonNode = JsonSerializer.SerializeToNode(value, jsonOptions); var writer = new ToonWriter(options); return writer.Write(jsonNode); @@ -75,7 +81,7 @@ public static class ToonSerializer public static TValue? Deserialize(string toon, ToonSerializerOptions? options = null) { var jsonNode = Deserialize(toon, options); - return jsonNode is null ? default : jsonNode.Deserialize(); + return jsonNode is null ? default : jsonNode.Deserialize(jsonOptions); } ///