diff --git a/CHANGELOG.md b/CHANGELOG.md index 28037f1..08c12ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,10 @@ Additions Fixes - Fixed tooltips not being bounded correctly for viewports that don't start at the origin +### MLEM.Data +Improvements +- Made fields and methods in StaticJsonConverter protected to allow extending it + ## 7.1.1 ### MLEM diff --git a/MLEM.Data/Json/StaticJsonConverter.cs b/MLEM.Data/Json/StaticJsonConverter.cs index 3d13dbd..567d970 100644 --- a/MLEM.Data/Json/StaticJsonConverter.cs +++ b/MLEM.Data/Json/StaticJsonConverter.cs @@ -15,19 +15,30 @@ namespace MLEM.Data.Json { /// The type of the object to convert public class StaticJsonConverter : JsonConverter { - private readonly Dictionary entries; - private readonly Dictionary inverse; - private readonly bool throwOnRead; + /// + /// The entries, or registry, that this static json converter uses. + /// + protected readonly Dictionary Entries; + /// + /// The inverse of this static json converter's , mapping the values to their respective keys. + /// Note that this dictionary is only created once and is not automatically updated should the be modified. + /// + protected readonly Dictionary Inverse; + /// + /// Whether to throw a in if a key is missing, or throw a if a stored json value is not a string. + /// If this is , returns instead. + /// + protected readonly bool ThrowOnRead; /// /// Creates a new static json converter using the given underlying . /// - /// The dictionary to use + /// The entries, or registry, that this static json converter uses. /// Whether to throw a in if a key is missing, or throw a if a stored json value is not a string. If this is , returns instead. public StaticJsonConverter(Dictionary entries, bool throwOnRead = false) { - this.entries = entries; - this.inverse = StaticJsonConverter.CreateInverse(entries); - this.throwOnRead = throwOnRead; + this.Entries = entries; + this.Inverse = StaticJsonConverter.CreateInverse(entries); + this.ThrowOnRead = throwOnRead; } /// @@ -62,7 +73,7 @@ namespace MLEM.Data.Json { writer.WriteNull(); return; } - if (!this.inverse.TryGetValue(value, out var key)) + if (!this.Inverse.TryGetValue(value, out var key)) throw new KeyNotFoundException($"Cannot write {value} that is not a registered entry"); writer.WriteValue(key); } @@ -78,16 +89,24 @@ namespace MLEM.Data.Json { if (reader.Value == null) return default; if (reader.TokenType != JsonToken.String) { - if (this.throwOnRead) + if (this.ThrowOnRead) throw new JsonSerializationException($"Expected a string value for {reader.Value}, got a {reader.TokenType}"); return default; } - if (!this.entries.TryGetValue((string) reader.Value, out var ret) && this.throwOnRead) + if (!this.Entries.TryGetValue((string) reader.Value, out var ret) && this.ThrowOnRead) throw new KeyNotFoundException($"Could not find registered entry for {reader.Value}"); return ret; } - private static Dictionary GetEntries( + /// + /// Returns the entries, or registry, that this static json converter should use, using reflection to find the passed in the passed . + /// + /// The type to find the entries in. + /// The name of the field or property that the entries are in within the given . + /// The found property or field's value. + /// Thrown if there is no property or field with the in the given . + /// Thrown if the value of the is not of the expected type. + protected static Dictionary GetEntries( #if NET6_0_OR_GREATER [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] #endif @@ -99,7 +118,13 @@ namespace MLEM.Data.Json { return value as Dictionary ?? throw new InvalidCastException($"{value} is not of expected type {typeof(T)}"); } - private static Dictionary CreateInverse(Dictionary entries) { + /// + /// Creates the inverse version of the passed , mapping the values to their keys, and throws an exception if there are any duplicate values. + /// + /// The entries to create an inverse of. + /// The inverse of the given , mapping the values to their keys. + /// Thrown if the contains duplicate values, making it impossible to create a valid inverse. + protected static Dictionary CreateInverse(Dictionary entries) { var ret = new Dictionary(); foreach (var entry in entries) { if (ret.ContainsKey(entry.Value))