Jak prawidłowo serializować krotkę jako słownik kluczy
Mam następującą aplikację, która pokazuje, że kluczowa część słownika nie jest wysyłana doJsonConverter
, ale nazywa się toToString()
na. To jest dla mnie problem, ponieważ nie mogę deserializować mojegoJson
strunowy .
Jakieś pomysły?
class Program
{
static void Main(string[] args)
{
var coll = new Dictionary<Tuple<string,string>, string>();
coll.Add(Tuple.Create("key1", "KEY1"), "Value1");
coll.Add(Tuple.Create("key2", "KEY2"), "Value2");
string json = JsonConvert.SerializeObject(coll);
Dictionary<Tuple<string, string>, string> coll2;
Console.WriteLine(json);
//coll2 = JsonConvert.DeserializeObject<Dictionary<Tuple<string, string>, string>>(json);
// It throws an exception here
//foreach (var k in coll2)
//{
// Console.WriteLine("<{0}|{1}>",k.Key, k.Value);
//}
var t = Tuple.Create("key1", "key2");
Console.WriteLine(t.ToString());
string json2 = JsonConvert.SerializeObject(t);
Console.WriteLine(json2);
}
}
Wyjście:
{"(key1, KEY1)": "Value1", "(key2, KEY2)": "Value2"} (key1, key2)
{"Item1": "key1", "Item2": "key2"}
Naciśnij dowolny klawisz, aby kontynuować . . .