I'm trying to serialize a dictionary within a class and the keys inside the CustomAttributes dictionary are getting formatted even though I've provided the ProcessDictionaryKeys parameter as false.
I've added [JsonProperty] as shown below:
[JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })]
public IDictionary<string, string> CustomAttributes { get; set; }
my CustomAttributes data looks like this:
CustomAttributes = new Dictionary<string, string>()
{
{"Custom Attribute 1", "1"},
{"CustomAttribute 2", "2"}
}
and the JSON which is produced looks like:
custom_attributes\":{\"custom Attribute 1\":\"1\",\"customAttribute 2\":\"2\"
As you can see, the first letter of each of the dictionary keys are being uncapitalised. How can I stop this from happening?
EDIT: Changing the ProcessDictionaryKeys parameter to true doesn't seem to make any difference.
The problem doesn't reproduce with just the code in your question as shown in demo fiddle #1 here.
Instead, you must be serializing with some global serializer settings for which JsonSerializerSettings.ContractResolver.NamingStrategy.ProcessDictionaryKeys = true such as CamelCasePropertyNamesContractResolver:
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};
var json = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
Demo fiddle #2 here.
Assuming that's correct, the reason that [JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })] does not cause the dictionary keys to be serialized verbatim is that JsonPropertyAttribute.NamingStrategyType only applies to the property name itself (here CustomAttributes) not the property names of property's items. If you wanted to apply a naming strategy to the property's items you would need something like ItemNamingStrategyType -- but JsonPropertyAttribute has no such functionality.
So, what are your options?
You could modify your global naming strategy to serialize dictionary names verbatim as shown in Keep casing when serializing dictionaries.
You could subclass Dictionary<TKey, TValue> and apply [JsonDictionary(NamingStrategyType = typeof(DefaultNamingStrategy))] to it as shown in this answer to Applying JsonDictionary attribute to dictionary:
[JsonDictionary(NamingStrategyType = typeof(DefaultNamingStrategy))]
public class VerbatimDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
}
And then later:
CustomAttributes = new VerbatimDictionary<string, string>()
{
{"Custom Attribute 1", "1"},
{"CustomAttribute 2", "2"}
}
Demo fiddle #3 here.
You could introduce a custom JsonConverter that serializes an IDictionary<TKey, TValue> with the default naming strategy. First, define the following converter:
public class VerbatimDictionaryConverter<TKey, TValue> : JsonConverter<IDictionary<TKey, TValue>>
{
[JsonDictionary(NamingStrategyType = typeof(DefaultNamingStrategy))]
class VerbatimDictionarySerializationSurrogate : IReadOnlyDictionary<TKey, TValue>
{
readonly IDictionary<TKey, TValue> dictionary;
public VerbatimDictionarySerializationSurrogate(IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
throw new ArgumentNullException(nameof(dictionary));
this.dictionary = dictionary;
}
public bool ContainsKey(TKey key) { return dictionary.ContainsKey(key); }
public bool TryGetValue(TKey key, out TValue value) { return dictionary.TryGetValue(key, out value); }
public TValue this[TKey key] { get { return dictionary[key]; } }
public IEnumerable<TKey> Keys { get { return dictionary.Keys; } }
public IEnumerable<TValue> Values { get { return dictionary.Values; } }
public int Count { get { return dictionary.Count; } }
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return dictionary.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
public override void WriteJson(JsonWriter writer, IDictionary<TKey, TValue> value, JsonSerializer serializer)
{
serializer.Serialize(writer, new VerbatimDictionarySerializationSurrogate(value));
}
public override bool CanRead { get { return false; } }
public override IDictionary<TKey, TValue> ReadJson(JsonReader reader, Type objectType, IDictionary<TKey, TValue> existingValue, bool hasExistingValue, JsonSerializer serializer) { throw new NotImplementedException(); }
}
And apply it as follows:
[JsonConverter(typeof(VerbatimDictionaryConverter<string, string>))]
public IDictionary<string, string> CustomAttributes { get; set; }
Demo fiddle #4 here.
Related
I am creating a json string by serializing an object in C# (using Newtonsoft) to pass to a third party charting library, so have no control over the structure I need to create. The structure requires an object with duplicate keys, something like;
{ "color": "dd3333",
"linewidth": 2,
"dataset": [ 3,4,5,6 ],
"dataset": [ 5,6,7,8]
}
I'm struggling to get the output I want. I was using a dictionary, but have just come across the need for duplicate keys, which scuppers that. I've had a look at creating my own object to support duplicate keys (starting with these suggestions Duplicate keys in .NET dictionaries?) but am still struggling to get them to serialize to the format I need.
Any suggestions would be very welcome!
To solve this, I created a dictionary object that accepted duplicate keys (copied from Duplicate keys in .NET dictionaries?), and added a JsonConverter to control how the object was serialized;
public class MultiMap<TKey, TValue>
{
private readonly Dictionary<TKey, IList<TValue>> storage;
public MultiMap()
{
storage = new Dictionary<TKey, IList<TValue>>();
}
public void Add(TKey key, TValue value)
{
if (!storage.ContainsKey(key)) storage.Add(key, new List<TValue>());
storage[key].Add(value);
}
public IEnumerable<TKey> Keys
{
get { return storage.Keys; }
}
public bool ContainsKey(TKey key)
{
return storage.ContainsKey(key);
}
public IList<TValue> this[TKey key]
{
get
{
if (!storage.ContainsKey(key))
throw new KeyNotFoundException(
string.Format(
"The given key {0} was not found in the collection.", key));
return storage[key];
}
}
}
The JsonConverter object was as follows (I didn't bother with the read as I didn't need it, but it could be easily implemented);
public class MultiMapJsonConverter<TKey, TValue> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(MultiMap<TKey, TValue>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
MultiMap<TKey, TValue> m = (MultiMap<TKey, TValue>)value;
foreach (TKey key in m.Keys)
{
foreach (TValue val in m[key])
{
writer.WritePropertyName(key.ToString());
JToken.FromObject(val).WriteTo(writer);
}
}
writer.WriteEndObject();
}
}
With this defined , the following code;
var mm = new MultiMap<string, object>();
mm.Add("color", "dd3333");
mm.Add("linewidth", 2);
mm.Add("dataset", new int[] { 3,4,5,6 });
mm.Add("dataset", new int[] { 5,6,7,8 });
var json = JsonConvert.SerializeObject(mm, new JsonConverter[] { new MultiMapJsonConverter<string, object>() });
gives me the json output;
{"color":"dd3333","linewidth":2,"dataset":[3,4,5,6],"dataset":[5,6,7,8]}
I have an issue while deserializing json data which can have both float or array type of data. The same issue from here
Dealing with JSON field that holds different types in C#
But everywhere the solution is to use json.net with a JsonConverter. I need to achieve the deserialization using only System.Web.Script.Serialization.JavaScriptSerializer in c#. Can anyone help, pls?
You can use a JavaScriptConverter for this purpose. However, unlike Json.NET's JsonConverter a JavaScriptConverter can only be used for types that map from and to a JSON object -- not an array or primitive type. Thus you will need to create a custom converter for any object that may contain a polymorphic property that could be an array or singleton item.
Let's imagine you have JSON that looks like the following:
{
"name": "my name",
"data": {
"foo": "Foo",
"bar": "Bar"
},
"values": [
3.14,
2.718
]
}
Where "values" might sometimes be a primitive value like so:
"values": 3.14
And, you want to map this to the following POCO:
public class RootObject
{
public string name { get; set; }
public NestedData data { get; set; }
public float[] Values { get; set; }
}
public class NestedData
{
public string foo { get; set; }
public string bar { get; set; }
}
As JavaScriptConverter.Deserialize() is passed an IDictionary<string, object> of parsed values, the steps to take are:
Detach any properties that need custom processing (keeping in mind that JavaScriptSerializer is case-insensitive but that the dictionary is not).
Generate a default deserialization for any remaining properties using JavaScriptSerializer.ConvertToType<T>() using a fresh serializer that does not contain the converter.
Manually deserialize and populate the custom properties into the partially deserialized object, and return it.
For the type shown above, the following converter, based somewhat on this answer, does the job:
class RootObjectConverter : CustomPropertiesConverter<RootObject>
{
const string ValuesName = "values";
protected override IEnumerable<string> CustomProperties
{
get { return new[] { ValuesName }; }
}
protected override void DeserializeCustomProperties(Dictionary<string, object> customDictionary, RootObject obj, JavaScriptSerializer serializer)
{
object itemCost;
if (customDictionary.TryGetValue(ValuesName, out itemCost) && itemCost != null)
obj.Values = serializer.FromSingleOrArray<float>(itemCost).ToArray();
}
protected override void SerializeCustomProperties(RootObject obj, Dictionary<string, object> dict, JavaScriptSerializer serializer)
{
obj.Values.ToSingleOrArray(dict, ValuesName);
}
}
public abstract class CustomPropertiesConverter<T> : JavaScriptConverter
{
protected abstract IEnumerable<string> CustomProperties { get; }
protected abstract void DeserializeCustomProperties(Dictionary<string, object> customDictionary, T obj, JavaScriptSerializer serializer);
protected abstract void SerializeCustomProperties(T obj, Dictionary<string, object> dict, JavaScriptSerializer serializer);
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
// Detach custom properties
var customDictionary = new Dictionary<string, object>();
foreach (var key in CustomProperties)
{
object value;
if (dictionary.TryRemoveInvariant(key, out value))
customDictionary.Add(key, value);
}
// Deserialize and populate all members other than "values"
var obj = new JavaScriptSerializer().ConvertToType<T>(dictionary);
// Populate custom properties
DeserializeCustomProperties(customDictionary, obj, serializer);
return obj;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
// Generate a default serialization. Is there an easier way to do this?
var defaultSerializer = new JavaScriptSerializer();
var dict = defaultSerializer.Deserialize<Dictionary<string, object>>(defaultSerializer.Serialize(obj));
// Remove default serializations of custom properties, if present
foreach (var key in CustomProperties)
{
dict.RemoveInvariant(key);
}
// Add custom properties
SerializeCustomProperties((T)obj, dict, serializer);
return dict;
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(T) }; }
}
}
public static class JavaScriptSerializerObjectExtensions
{
public static void ReplaceInvariant<T>(this IDictionary<string, T> dictionary, string key, T value)
{
RemoveInvariant(dictionary, key);
dictionary.Add(key, value);
}
public static bool TryRemoveInvariant<T>(this IDictionary<string, T> dictionary, string key, out T value)
{
if (dictionary == null)
throw new ArgumentNullException();
var keys = dictionary.Keys.Where(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase)).ToArray();
if (keys.Length == 0)
{
value = default(T);
return false;
}
else if (keys.Length == 1)
{
value = dictionary[keys[0]];
dictionary.Remove(keys[0]);
return true;
}
else
{
throw new ArgumentException(string.Format("Duplicate keys found: {0}", String.Join(",", keys)));
}
}
public static void RemoveInvariant<T>(this IDictionary<string, T> dictionary, string key)
{
if (dictionary == null)
throw new ArgumentNullException();
foreach (var actualKey in dictionary.Keys.Where(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase)).ToArray())
dictionary.Remove(actualKey);
}
public static void ToSingleOrArray<T>(this ICollection<T> list, IDictionary<string, object> dictionary, string key)
{
if (dictionary == null)
throw new ArgumentNullException();
if (list == null || list.Count == 0)
dictionary.RemoveInvariant(key);
else if (list.Count == 1)
dictionary.ReplaceInvariant(key, list.First());
else
dictionary.ReplaceInvariant(key, list.ToArray());
}
public static List<T> FromSingleOrArray<T>(this JavaScriptSerializer serializer, object value)
{
if (value == null)
return null;
if (value.IsJsonArray())
{
return value.AsJsonArray().Select(i => serializer.ConvertToType<T>(i)).ToList();
}
else
{
return new List<T> { serializer.ConvertToType<T>(value) };
}
}
public static bool IsJsonArray(this object obj)
{
if (obj is string || obj is IDictionary)
return false;
return obj is IEnumerable;
}
public static IEnumerable<object> AsJsonArray(this object obj)
{
return (obj as IEnumerable).Cast<object>();
}
}
Then use it like:
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new RootObjectConverter() });
var root = serializer.Deserialize<RootObject>(json);
I have an OrderedDictionary with int keys and System.Drawing.Rectangle values. JSON.NET won't serialize the OrderedDictionary...it returns an empty object. I wrote a custom converter, but I wondered if there was an easier way. Thinking that JSON.NET might use the presence of a typed enumerator as the trigger to use its built-in code for serializing and deserializing a Dictionary<TKey, TValue> I tried this:
class Program
{
static void Main(string[] args)
{
var test = new OrderedDictionary<int, Rectangle>();
test.Add(1, new Rectangle(0, 0, 50, 50));
test.Add(42, new Rectangle(1, 1, 1, 1));
string s = JsonConvert.SerializeObject(test);
var deserialized = JsonConvert.DeserializeObject<OrderedDictionary<int, Rectangle>>(s);
var someRect = deserialized[(object)1]; // someRect is null
var someOtherRect = (Rectangle)deserialized["1"]; // InvalidCastException
}
}
public class OrderedDictionary<TKey, TValue> : OrderedDictionary, IEnumerable<KeyValuePair<TKey, TValue>>
{
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
foreach (TKey key in Keys)
{
yield return new KeyValuePair<TKey, TValue>(key, (TValue)this[key]);
}
}
}
Serialization works perfectly. However, when I deserialize, the keys in the dictionary become strings and the Rectangles are JObjects that can't be cast to Rectangle. Is there something I can add to my OrderedDictionary<> class that will allow for proper deserialization with JSON.NET? Thanks.
Your problem is that, although you've added an enumerator, things like the indexer cannot be overridden. So what you're getting is the default implementation of the non-generic OrderedDictionary which doesn't give you a typed result.
So, instead of inheriting, you need a facade that fully implements the generic interface.
You'll need to verify my class (I just made the test work). I've also cheated with the Keys and Values properties (they're not often used) and some of the other ICollection methods. Just lazy :)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using Newtonsoft.Json;
using Xunit;
namespace XUnitTestProject1
{
public class UnitTest1
{
[Fact]
public void TestJsonRectange()
{
var test = new OrderedDictionary<int, Rectangle>();
test.Add(1, new Rectangle(0, 0, 50, 50));
test.Add(42, new Rectangle(1, 1, 1, 1));
string json = JsonConvert.SerializeObject(test);
var deserialized = JsonConvert.DeserializeObject<OrderedDictionary<int, Rectangle>>(json);
object someRect = deserialized[1];
Assert.NotNull(someRect);
Assert.True(someRect is Rectangle);
}
[Fact]
public void TestJsonString()
{
var test = new OrderedDictionary<string, string>();
test.Add("1", "11");
test.Add("42", "4242");
string json = JsonConvert.SerializeObject(test);
var deserialized = JsonConvert.DeserializeObject<OrderedDictionary<string, string>>(json);
object something = deserialized["1"];
Assert.NotNull(something);
Assert.True(something is string);
}
public class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly OrderedDictionary dic = new OrderedDictionary();
public TValue this[TKey key] { get { return (TValue)dic[key]; } set { dic[key] = value; } }
public void Add(KeyValuePair<TKey, TValue> item)
{
dic.Add(item.Key, item.Value);
}
public void Add(TKey key, TValue value)
{
dic.Add(key, value);
}
public void Clear() { dic.Clear(); }
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { }
public int Count { get { return dic.Count; } }
public bool IsReadOnly { get { return false; } }
public bool Contains(TKey key) { return dic.Contains(key); }
public bool ContainsKey(TKey key) { return dic.Contains(key); }
public bool Remove(TKey key) { dic.Remove(key); return true; }
public bool TryGetValue(TKey key, out TValue value) { value = default(TValue); return false; }
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) { return false; }
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
foreach (DictionaryEntry entry in dic)
yield return new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private static readonly TKey[] keys = new TKey[0];
private static readonly TValue[] values = new TValue[0];
ICollection<TKey> IDictionary<TKey, TValue>.Keys { get { return keys; } }
ICollection<TValue> IDictionary<TKey, TValue>.Values { get { return values; } }
}
}
}
Is there a better way to mimic Covariance in this example? Ideally I'd like to do:
private IDictionary<string, ICollection<string>> foos;
public IEnumerable<KeyValuePair<string, IEnumerable<string>> Foos
{
get
{
return foos;
}
}
But KeyValuePair<TKey, TValue> is not covariant.
Instead I have to do:
public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Foos
{
get
{
return foos.Select(x =>
new KeyValuePair<string, IEnumerable<string>>(x.Key, x.Value));
}
}
Is there a better/cleaner way?
Unfortunately, KeyValuePair<TKey, TValue> is a struct; and structs don't exhibit variance in .NET.
You can of course solve this by writing your own covariant Pair interface and some simple helpers to convert between sequences of KeyValuePair and your custom Pair interface. This will let you do:
var dict = new Dictionary<string, ICollection<string>>();
var view = dict.GetCovariantView(); // IEnumerable< IPair<string, ICollection<string> > >
// Notice that you can _widen_ both the key and the value types:
var dictView = view.CastPairs<object, IEnumerable<string>>(); // IEnumerable< IPair< object, IEnumerable<String> > >
// The `CastPairs` call is actually unnecessary provided you don't use `var` for the left-hand-side assignment.
// ...this is due to the implicit (and identity-preserving) variant interface conversion in C#, e.g.:
IEnumerable< IPair< Object, IEnumerable<String> > > dictView2 = view;
Console.WriteLine( Object.ReferenceEquals( view, dictView2 ) ); // --> True
Here's some example code that will let you achieve this:
// `out TKey` is for demonstration purposes. In production-quality code you probably should be using invariant key types.
public interface IPair<out TKey, out TValue>
where TKey : notnull
{
TKey Key { get; }
TValue Value { get; }
}
public class Pair<TKey, TValue> : IPair<TKey, TValue>
where TKey : notnull
{
public TKey Key { get; }
public TValue Value { get; }
public Pair(TKey key, TValue value)
{
this.Key = key;
this.Value = value;
}
public Pair(KeyValuePair<TKey, TValue> pair)
: this(pair.Key, pair.Value)
{}
}
public static class PairSequenceExtensions
{
public static IEnumerable<IPair<TKey, TValue>> GetCovariantView<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
where TKey : notnull
{
if (source is null) throw new ArgumentNullException(nameof(source));
return source.Select(kvp => new Pair<TKey, TValue>(kvp));
}
public static IEnumerable<IPair<TKey, TValue>> CastPairs<TKey, TValue>(this IEnumerable<IPair<TKey, TValue>> source)
where TKey : notnull
{
if (source is null) throw new ArgumentNullException(nameof(source));
return source;
}
}
Hardly. KVP is a struct: Not an itnerface, is of ValueType.
Interesting SO post on variance.
I think casts are more performant, so I'd prefer to code like this:
private IDictionary<string, IEnumerable<string>> foos;
public IEnumerable<KeyValuePair<string, IEnumerable<string>> Foos
{
get
{
return foos;
}
}
And cast KeyValuePair.Value to ICollection where I really need to. Frankly speaking it depends on how foos is used.
I have a JSON:
{
"data": { "A": 5, "B": 6 },
"foo": "foo",
"bar": "bar"
}
I need to deserialize data into a class:
public Dictionary<MyEnum, int> Data { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
But MyEnum values are CodeA, and CodeB instead of simply A and B respectively.
I have a custom Converter that can handle conversion. But how do I specify a JsonConverter to use with Dictionary keys?
I believe the only way is to make a JsonConverter for the whole Dictionary<MyEnum, int> type, or Dictionary<MyEnum, T>.
Dictionary keys are not regarded as values and will not be run through the JsonConverters. TypeConverters would have been a solution, but the default string to enum conversion will enter before it looks at the TypeConverters.
So... I don't think it can be done any other way.
EDIT:
Not fully tested, but I use something like this in a project of mine:
public class DictionaryWithSpecialEnumKeyConverter : JsonConverter
{
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var valueType = objectType.GetGenericArguments()[1];
var intermediateDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType);
var intermediateDictionary = (IDictionary)Activator.CreateInstance(intermediateDictionaryType);
serializer.Populate(reader, intermediateDictionary);
var finalDictionary = (IDictionary)Activator.CreateInstance(objectType);
foreach (DictionaryEntry pair in intermediateDictionary)
finalDictionary.Add(Enum.Parse(MyEnum, "Code" + pair.Key, false), pair.Value);
return finalDictionary;
}
public override bool CanConvert(Type objectType)
{
return objectType.IsA(typeof(IDictionary<,>)) &&
objectType.GetGenericArguments()[0].IsA<MyEnum>();
}
}
You will need this little helper:
public static bool IsA(this Type type, Type typeToBe)
{
if (!typeToBe.IsGenericTypeDefinition)
return typeToBe.IsAssignableFrom(type);
var toCheckTypes = new List<Type> { type };
if (typeToBe.IsInterface)
toCheckTypes.AddRange(type.GetInterfaces());
var basedOn = type;
while (basedOn.BaseType != null)
{
toCheckTypes.Add(basedOn.BaseType);
basedOn = basedOn.BaseType;
}
return toCheckTypes.Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeToBe);
}
Hope it works out for you.
Here is a generic solution for the problem of using json with Dictionary with any type of key:
[JsonObject]
public class MyKeyValuePair<TKey, TValue>
{
public TKey MyKey;
public TValue MyValue;
[JsonConstructor]
public MyKeyValuePair()
{
}
public MyKeyValuePair(TKey t1, TValue t2)
{
MyKey = t1;
MyValue = t2;
}
}
[JsonObject]
public class MyDictionaty<TKey, TValue>
{
public ICollection<MyKeyValuePair<TKey, TValue>> Collection;
[JsonConstructor]
public MyDictionaty()
{
}
public MyDictionaty(Dictionary<TKey, TValue> refund)
{
Collection = BuildMyKeyValuePairCollection(refund);
}
internal Dictionary<TKey, TValue> ToDictionary()
{
return Collection.ToDictionary(pair => pair.MyKey, pair => pair.MyValue);
}
private ICollection<MyKeyValuePair<TKey, TValue>> BuildMyKeyValuePairCollection(Dictionary<TKey, TValue> refund)
{
return refund.Select(o => new MyKeyValuePair<TKey, TValue>(o.Key, o.Value)).ToList();
}
}
[JsonObject]
public class ClassWithDictionary
{
[JsonProperty]
private readonly MyDictionary<AnyKey, AnyValue> _myDictionary;
private Dictionary<AnyKey, AnyValue> _dic;
[JsonConstructor]
public ClassWithDictionary()
{
}
public ClassWithDictionary(Dictionary<AnyKey, AnyValue> dic)
{
_dic= dic;
_myDictionary = new MyDictionaty<AnyKey, AnyValue>(dic);
}
public Dictionary<AnyKey, AnyValue> GetTheDictionary()
{
_dic = _dic??_myDictionary.ToDictionary();
return _dic;
}
}
I couldn't get any TypeConverter solution working and didn't want to have a JsonConverter that builds a string-key dictionary and then copies everything into a new dictionary, so I went with something like this:
public sealed class MyEnumKeyDictionary<TValue> : IReadOnlyDictionary<MyEnum, TValue>, IDictionary<string, TValue>
{
private readonly Dictionary<MyEnum, TValue> actual = new Dictionary<MyEnum, TValue>();
// implement IReadOnlyDictionary implicitly, passing everything from `actual`
// implement IDictionary explicitly, passing everything into/from `actual` after doing Enum.Parse/Enum.ToString
}