using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using MLEM.Misc;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
///
/// An represents an object that can hold generic key-value based data.
/// This class uses for each object stored to ensure that objects with a custom get deserialized as an instance of their original type if is not set to .
///
[DataContract]
public class JsonTypeSafeGenericDataHolder : IGenericDataHolder {
[DataMember(EmitDefaultValue = false)]
private Dictionary data;
///
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] = JsonTypeSafeWrapper.Of(data);
}
}
///
public T GetData(string key) {
if (this.data != null && this.data.TryGetValue(key, out var val))
return (T) val.Value;
return default;
}
///
public IReadOnlyCollection GetDataKeys() {
if (this.data == null)
return Array.Empty();
return this.data.Keys;
}
}
}