Class JsonDictionary<V>
This class is intended to wrap Dictionary<string,V> objects that are to be serialized, specifically with the Newtonsoft Json serializer (this may apply
to other serializers too, but I haven't tested any others to see how they behave).
The reason this is needed is because the Newtonsoft serializer emits its JSON based on the order that the KVPs of a dictionary are returned via the
dictionary's GetEnumerator() method. This order is typically the same as the order in which objects were added to the dictionary, but that order may
change as values are updated or removed, when the GC runs, etc. Regardless, the point is that the order is unpredictable and entirely non-deterministic.
This means that two dictionaries having exactly the same key/value pairs added to them may not get serialized identically, making it impossible to compare
their JSON representations for object equality later (e.g. if they're stored in a db somewhere).
This class addresses this problem by ensuring that every call to GetEnumerator() receives the KVPs ordered by the string key. It will also detect if
any of the values in those KVPs are IDictionary<string,V>, and if so, will wrap them with another JsonDictionary<V> before returning, so that sub-dictionaries
will also be serialized consistently.
Inheritance
JsonDictionary<V>
Assembly: Estrelica.Utility.dll
Syntax
[DoNotObfuscateType]
public class JsonDictionary<V> : IDictionary<string, V>, ICollection<KeyValuePair<string, V>>, IEnumerable<KeyValuePair<string, V>>, IEnumerable
Type Parameters
Constructors
Declaration
public JsonDictionary(IDictionary<string, V> wrappedDictionary = null)
Parameters
Properties
Declaration
public int Count { get; }
Property Value
Declaration
public bool IsReadOnly { get; }
Property Value
Declaration
public V this[string key] { get; set; }
Parameters
Property Value
Declaration
public ICollection<string> Keys { get; }
Property Value
Declaration
public ICollection<V> Values { get; }
Property Value
Methods
Declaration
public void Add(KeyValuePair<string, V> item)
Parameters
Declaration
public void Add(string key, V value)
Parameters
Declaration
public bool Contains(KeyValuePair<string, V> item)
Parameters
Returns
Declaration
public bool ContainsKey(string key)
Parameters
Returns
Declaration
public void CopyTo(KeyValuePair<string, V>[] array, int arrayIndex)
Parameters
Declaration
public IEnumerator<KeyValuePair<string, V>> GetEnumerator()
Returns
Declaration
public bool Remove(KeyValuePair<string, V> item)
Parameters
Returns
Declaration
public bool Remove(string key)
Parameters
Returns
Declaration
public bool TryGetValue(string key, out V value)
Parameters
Returns
Extension Methods