It seems impossible to (de-)serialize a generic dictionary using Json.NET out of the box.
class KeyClass
{
public KeyClass(string p1, string p2) { prop1 = p1; prop2 = p2;}
public string prop1 { get; set; }
public string prop2 { get; set; }
}
class ValueClass
{
public ValueClass(string pa, string pb) { propA = pa; propB = pb;}
public string propA { get; set; }
public string propB { get; set; }
}
...
Dictionary<KeyClass, ValueClass> d = new Dictionary<KeyClass, ValueClass>();
d.Add(new KeyClass("k1", "k2"), new ValueClass("v1", "v2"));
d.Add(new KeyClass("k3", "k4"), new ValueClass("v3", "v4"));
string json = JsonConvert.SerializeObject(d);
returns
{"ConsoleApplication1.KeyClass":{"propA":"v1","propB":"v2"},
"ConsoleApplication1.KeyClass":{"propA":"v3","propB":"v4"}}
where I expected something like:
{{"prop1":"k1","prop2":"k2"}:{"propA":"v1","propB":"v2"},
{"prop1":"k3","prop2":"k4"}:{"propA":"v3","propB":"v4"}}
So ToString()
is called on the key (KeyClass). (an override that generates json doesn't help: escapes the json and just doesn't feel right).
Is it possible to write a CustomCreationConverter for this case?
Is this a hard limitation of json.NET?
If so, do other libraries support this?