1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

slight GenericDataHolder (and extensions) improvements and reversions

This commit is contained in:
Ell 2021-11-07 00:46:35 +01:00
parent 39138446ea
commit b22d2d4d22
3 changed files with 11 additions and 7 deletions

View file

@ -30,12 +30,12 @@ namespace MLEM.Data.Json {
/// <inheritdoc />
public T GetData<T>(string key) {
if (this.data != null && this.data.TryGetValue(key, out var val))
return val.GetValue<T>();
return (T) val.Value;
return default;
}
/// <inheritdoc />
public IEnumerable<string> GetDataKeys() {
public IReadOnlyCollection<string> GetDataKeys() {
if (this.data == null)
return Array.Empty<string>();
return this.data.Keys;

View file

@ -5,8 +5,8 @@ using Newtonsoft.Json;
namespace MLEM.Data.Json {
/// <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"/>.
/// 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"/>.
/// 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 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.
/// 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>

View file

@ -3,7 +3,11 @@ using System.Collections.Generic;
using System.Runtime.Serialization;
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]
public class GenericDataHolder : IGenericDataHolder {
@ -30,7 +34,7 @@ namespace MLEM.Misc {
}
/// <inheritdoc />
public IEnumerable<string> GetDataKeys() {
public IReadOnlyCollection<string> GetDataKeys() {
if (this.data == null)
return Array.Empty<string>();
return this.data.Keys;
@ -63,7 +67,7 @@ namespace MLEM.Misc {
/// Returns all of the generic data that this object stores.
/// </summary>
/// <returns>The generic data on this object</returns>
IEnumerable<string> GetDataKeys();
IReadOnlyCollection<string> GetDataKeys();
}
}