From b22d2d4d22ecdc4b019fd7595d6c5bb0ab42d91b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 7 Nov 2021 00:46:35 +0100 Subject: [PATCH] slight GenericDataHolder (and extensions) improvements and reversions --- MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs | 4 ++-- MLEM.Data/Json/JsonTypeSafeWrapper.cs | 4 ++-- MLEM/Misc/GenericDataHolder.cs | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs index d5d88e2..c432604 100644 --- a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs +++ b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs @@ -30,12 +30,12 @@ namespace MLEM.Data.Json { /// public T GetData(string key) { if (this.data != null && this.data.TryGetValue(key, out var val)) - return val.GetValue(); + return (T) val.Value; return default; } /// - public IEnumerable GetDataKeys() { + public IReadOnlyCollection GetDataKeys() { if (this.data == null) return Array.Empty(); return this.data.Keys; diff --git a/MLEM.Data/Json/JsonTypeSafeWrapper.cs b/MLEM.Data/Json/JsonTypeSafeWrapper.cs index 968e269..d6327e1 100644 --- a/MLEM.Data/Json/JsonTypeSafeWrapper.cs +++ b/MLEM.Data/Json/JsonTypeSafeWrapper.cs @@ -5,8 +5,8 @@ using Newtonsoft.Json; namespace MLEM.Data.Json { /// - /// A json type-safe wrapper can be used to wrap any objects of any type before submitting them to a non-specifically typed or that will be serialized using a in cases where is not set to . - /// If any object is not wrapped in this manner, and it has a custom , the value deserialized from it might not have the same type as the originally serialized object. This behavior can be observed, for example, when serializing a of entries, one of which is a : The will be serialized as a and, upon deserialization, will remain a . + /// A json type-safe wrapper can be used to wrap any objects that have a custom which stores them as a primitive type and that are serialized using a in cases where is not set to . + /// If these objects are not wrapped in this manner, the value deserialized from it might not have the same type as the originally serialized object. This behavior can be observed, for example, when serializing a of entries, one of which is a : The will be serialized as a and, upon deserialization, will remain a . /// In general, wrapping objects in this manner is only useful in rare cases, where custom data of an unexpected or unknown type is stored. /// See for an example of how this class can be used, and see this stackoverflow answer for more information on the problem that this class solves: https://stackoverflow.com/a/38798114. /// diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs index 224eace..3adb1aa 100644 --- a/MLEM/Misc/GenericDataHolder.cs +++ b/MLEM/Misc/GenericDataHolder.cs @@ -3,7 +3,11 @@ using System.Collections.Generic; using System.Runtime.Serialization; namespace MLEM.Misc { - /// + /// + /// Represents an object that can hold generic key-value based data. + /// A lot of MLEM components extend this class to allow for users to add additional data to them easily. + /// This implemention uses an underlying that only keeps track of non-default values. + /// [DataContract] public class GenericDataHolder : IGenericDataHolder { @@ -30,7 +34,7 @@ namespace MLEM.Misc { } /// - public IEnumerable GetDataKeys() { + public IReadOnlyCollection GetDataKeys() { if (this.data == null) return Array.Empty(); return this.data.Keys; @@ -63,7 +67,7 @@ namespace MLEM.Misc { /// Returns all of the generic data that this object stores. /// /// The generic data on this object - IEnumerable GetDataKeys(); + IReadOnlyCollection GetDataKeys(); } } \ No newline at end of file