I have 2 methods
public static string SerializeObject<T>(T value)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject);
return jsonString;
}
and
public static T DeserializeObject<T>(string jsonString)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString());
}
When I deserialize a json string with type
ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>
I have an exception:
Could not convert string '[1, 1]' to dictionary key type 'System.Collections.Generic.KeyValuePair`2[System.Int64,System.Int64]'. Create a TypeConverter to convert from the string to the key type object. Path '[1, 1]', line 2, position 12.
So Can someone show me the right code for it to work?
Here is my code:
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
var se = SerializeObject(test);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se);