From ecc0c3b963d10bdece5485eb058a23ea4dee1405 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 19 Jul 2024 21:35:47 +0200 Subject: [PATCH] Allow static json converters to write null values --- CHANGELOG.md | 2 +- MLEM.Data/Json/StaticJsonConverter.cs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index affef7c..8fdba87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,7 +63,7 @@ Improvements ### MLEM.Data Improvements - **Moved extension methods into matching namespaces to avoid unexpected suggestions** -- Allow static json converters to throw an exception if a key does not exist +- Allow static json converters to write null values and to throw an exception if a key does not exist Removals - **Removed obsolete types DynamicEnumConverter, CopyExtensions, DynamicEnum, NetBufferSerializer, and NetExtensions** diff --git a/MLEM.Data/Json/StaticJsonConverter.cs b/MLEM.Data/Json/StaticJsonConverter.cs index 851328f..3d13dbd 100644 --- a/MLEM.Data/Json/StaticJsonConverter.cs +++ b/MLEM.Data/Json/StaticJsonConverter.cs @@ -58,8 +58,12 @@ namespace MLEM.Data.Json { /// The value. /// The calling serializer. public override void WriteJson(JsonWriter writer, T value, JsonSerializer serializer) { + if (value == null) { + writer.WriteNull(); + return; + } if (!this.inverse.TryGetValue(value, out var key)) - throw new InvalidOperationException($"Cannot write {value} that is not a registered entry"); + throw new KeyNotFoundException($"Cannot write {value} that is not a registered entry"); writer.WriteValue(key); }