I would like to serialize Dictionary with complex key to JSON by serializing it as a list of KeyValuePair and deserialize it back to dictionary using Json.Net's JsonConverter.
I use this Dictionary as a transfer object's property:
public class TransferObject
{
public string property1 {get;set}
...
public Dictionary<Product,int> Products {get;set;}
}
At the end I would like to do something like this:
var jsonResult = JsonConvert.Serialize(transferObject, new DictionaryConverter())
and
var transferObject = JsonConvert.Deserialize<TransferObject>(json, DictionaryConverter())
How to implement DictionaryConverter that will convert Dictionary to list of KeyValuePair? ( I have tried, but i cannot deserialize it back) Is it good idea to convert it do list of KVP? Or maybe there is a better way to serialize Dictionary with complex key to JSON?
Thank you for your time.