From 39138446ea003e3297ac537910813880668f9fa6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 7 Nov 2021 00:23:48 +0100 Subject: [PATCH] added some more utility to JsonTypeSafeWrapper and revert SetData changes --- .../Json/JsonTypeSafeGenericDataHolder.cs | 8 +++--- MLEM.Data/Json/JsonTypeSafeWrapper.cs | 25 ++++++++++++++++++- MLEM/Misc/GenericDataHolder.cs | 7 +++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs index bfb98b5..d5d88e2 100644 --- a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs +++ b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs @@ -16,21 +16,21 @@ namespace MLEM.Data.Json { private Dictionary data; /// - public void SetData(string key, T data) { - if (EqualityComparer.Default.Equals(data, default)) { + public void SetData(string key, object data) { + if (data == default) { if (this.data != null) this.data.Remove(key); } else { if (this.data == null) this.data = new Dictionary(); - this.data[key] = new JsonTypeSafeWrapper(data); + this.data[key] = JsonTypeSafeWrapper.Of(data); } } /// public T GetData(string key) { if (this.data != null && this.data.TryGetValue(key, out var val)) - return (T) val.Value; + return val.GetValue(); return default; } diff --git a/MLEM.Data/Json/JsonTypeSafeWrapper.cs b/MLEM.Data/Json/JsonTypeSafeWrapper.cs index f6191ec..968e269 100644 --- a/MLEM.Data/Json/JsonTypeSafeWrapper.cs +++ b/MLEM.Data/Json/JsonTypeSafeWrapper.cs @@ -8,7 +8,7 @@ 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 . /// 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 and can be used, and see this stackoverflow answer for more information on the problem that this class solves: https://stackoverflow.com/a/38798114. + /// 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. /// public abstract class JsonTypeSafeWrapper { @@ -17,6 +17,28 @@ namespace MLEM.Data.Json { /// public abstract object Value { get; } + /// + /// Returns the current of this , typecast to the given type . + /// If this 's type is incompatible with the given type, the type's default value is returned instead. + /// + /// The type of value to return + /// The , castt to the given type if compatible, otherwise default + public T GetValue() { + return this.Value is T t ? t : default; + } + + /// + /// Creates a new from the given value. + /// The type parameter of the returned wrapper will be equal to the of the passed. + /// If a for a specific type, known at comepile type, should be created, you can use . + /// + /// The value to wrap + /// A with a type matching the type of + public static JsonTypeSafeWrapper Of(object value) { + var type = typeof(JsonTypeSafeWrapper<>).MakeGenericType(value.GetType()); + return (JsonTypeSafeWrapper) Activator.CreateInstance(type, value); + } + } /// @@ -30,6 +52,7 @@ namespace MLEM.Data.Json { /// /// Creates a new json type-safe wrapper instance that wraps the given . + /// If the type of the value is unknown at compile time, can be used instead. /// /// The value to wrap public JsonTypeSafeWrapper(T value) { diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs index 120d3d9..224eace 100644 --- a/MLEM/Misc/GenericDataHolder.cs +++ b/MLEM/Misc/GenericDataHolder.cs @@ -11,8 +11,8 @@ namespace MLEM.Misc { private Dictionary data; /// - public void SetData(string key, T data) { - if (EqualityComparer.Default.Equals(data, default)) { + public void SetData(string key, object data) { + if (data == default) { if (this.data != null) this.data.Remove(key); } else { @@ -49,8 +49,7 @@ namespace MLEM.Misc { /// /// The key to store the data by /// The data to store in the object - /// The type of the data to store - void SetData(string key, T data); + void SetData(string key, object data); /// /// Returns a piece of generic data of the given type on this object.