From 51ff4a3b95ab38dd46c88f4e5482bf78449bb88c Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 9 May 2025 07:09:57 +0200 Subject: msc4222 fixes --- LibMatrix.EventTypes/Spec/IgnoredUserListEventContent.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'LibMatrix.EventTypes') diff --git a/LibMatrix.EventTypes/Spec/IgnoredUserListEventContent.cs b/LibMatrix.EventTypes/Spec/IgnoredUserListEventContent.cs index 3643b8c..59e17c9 100644 --- a/LibMatrix.EventTypes/Spec/IgnoredUserListEventContent.cs +++ b/LibMatrix.EventTypes/Spec/IgnoredUserListEventContent.cs @@ -16,12 +16,16 @@ public class IgnoredUserListEventContent : EventContent { [JsonExtensionData] public Dictionary? AdditionalData { get; set; } = []; - public T GetAdditionalData(string key) { + public T? GetAdditionalData(string key) where T : class { if (AdditionalData == null || !AdditionalData.TryGetValue(key, out var value)) - throw new KeyNotFoundException($"Key '{key}' not found in AdditionalData."); + return null; + if (value is T tValue) return tValue; - throw new InvalidCastException($"Value for key '{key}' cannot be cast to type '{typeof(T)}'. Cannot continue."); + if (value is JsonElement jsonElement) + return jsonElement.Deserialize(); + + throw new InvalidCastException($"Value for key '{key}' ({value.GetType()}) cannot be cast to type '{typeof(T)}'. Cannot continue."); } } } \ No newline at end of file -- cgit 1.5.1