mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
slight GenericDataHolder (and extensions) improvements and reversions
This commit is contained in:
parent
39138446ea
commit
b22d2d4d22
3 changed files with 11 additions and 7 deletions
|
@ -30,12 +30,12 @@ namespace MLEM.Data.Json {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public T GetData<T>(string key) {
|
public T GetData<T>(string key) {
|
||||||
if (this.data != null && this.data.TryGetValue(key, out var val))
|
if (this.data != null && this.data.TryGetValue(key, out var val))
|
||||||
return val.GetValue<T>();
|
return (T) val.Value;
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IEnumerable<string> GetDataKeys() {
|
public IReadOnlyCollection<string> GetDataKeys() {
|
||||||
if (this.data == null)
|
if (this.data == null)
|
||||||
return Array.Empty<string>();
|
return Array.Empty<string>();
|
||||||
return this.data.Keys;
|
return this.data.Keys;
|
||||||
|
|
|
@ -5,8 +5,8 @@ using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace MLEM.Data.Json {
|
namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A json type-safe wrapper can be used to wrap any objects of any type before submitting them to a non-specifically typed <see cref="List{T}"/> or <see cref="Dictionary{T,T}"/> that will be serialized using a <see cref="JsonSerializer"/> in cases where <see cref="JsonSerializer.TypeNameHandling"/> is not set to <see cref="TypeNameHandling.None"/>.
|
/// A json type-safe wrapper can be used to wrap any objects that have a custom <see cref="JsonConverter"/> which stores them as a primitive type and that are serialized using a <see cref="JsonSerializer"/> in cases where <see cref="JsonSerializer.TypeNameHandling"/> is not set to <see cref="TypeNameHandling.None"/>.
|
||||||
/// If any object is not wrapped in this manner, and it has a custom <see cref="JsonConverter"/>, 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 <see cref="List{T}"/> of <see cref="object"/> entries, one of which is a <see cref="TimeSpan"/>: The <see cref="TimeSpan"/> will be serialized as a <see cref="string"/> and, upon deserialization, will remain a <see cref="string"/>.
|
/// 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 <see cref="List{T}"/> of <see cref="object"/> entries, one of which is a <see cref="TimeSpan"/>: The <see cref="TimeSpan"/> will be serialized as a <see cref="string"/> and, upon deserialization, will remain a <see cref="string"/>.
|
||||||
/// In general, wrapping objects in this manner is only useful in rare cases, where custom data of an unexpected or unknown type is stored.
|
/// 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 <see cref="JsonTypeSafeGenericDataHolder"/> 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.
|
/// See <see cref="JsonTypeSafeGenericDataHolder"/> 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -3,7 +3,11 @@ using System.Collections.Generic;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
namespace MLEM.Misc {
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// 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 <see cref="IGenericDataHolder"/> implemention uses an underlying <see cref="Dictionary{String,Object}"/> that only keeps track of non-default values.
|
||||||
|
/// </summary>
|
||||||
[DataContract]
|
[DataContract]
|
||||||
public class GenericDataHolder : IGenericDataHolder {
|
public class GenericDataHolder : IGenericDataHolder {
|
||||||
|
|
||||||
|
@ -30,7 +34,7 @@ namespace MLEM.Misc {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IEnumerable<string> GetDataKeys() {
|
public IReadOnlyCollection<string> GetDataKeys() {
|
||||||
if (this.data == null)
|
if (this.data == null)
|
||||||
return Array.Empty<string>();
|
return Array.Empty<string>();
|
||||||
return this.data.Keys;
|
return this.data.Keys;
|
||||||
|
@ -63,7 +67,7 @@ namespace MLEM.Misc {
|
||||||
/// Returns all of the generic data that this object stores.
|
/// Returns all of the generic data that this object stores.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The generic data on this object</returns>
|
/// <returns>The generic data on this object</returns>
|
||||||
IEnumerable<string> GetDataKeys();
|
IReadOnlyCollection<string> GetDataKeys();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue