Serializing Dictionary< String, Dictionary< String, String > > to the point where it can be stored, and later de serialized. I've done quite a bit of googling and can't find a class, or function that is compatible with .NET 4.5 Completely.
Asked
Active
Viewed 3,829 times
1
-
i.e. Use IXmlSerializable and create your custom logic. – Myrtle Oct 31 '12 at 08:25
-
See this answer: http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member – Bojan Spasevski Oct 31 '12 at 08:29
4 Answers
2
The out-of-the-box JavaScriptSerializer
should be able to cope with your dictionary. It should give quite compact output, too.
var source = new Dictionary<string, Dictionary<string, string>>
{
{ "one", new Dictionary<string, string> { { "a", "1a"}, { "b", "1b" } } },
{ "two", new Dictionary<string, string> { { "a", "2a"}, { "b", "2b" } } }
};
var serializer = new JavaScriptSerializer();
// This gives {"one":{"a":"1a","b":"1b"},"two":{"a":"2a","b":"2b"}}
string serialized = serializer.Serialize(source);
// This gives a clone of the original dictionary.
var deserialized = serializer.Deserialize
<Dictionary<string, Dictionary<string, string>>>(serialized);

Rawling
- 49,248
- 7
- 89
- 127
-
But JavascriptSerializer doesn't exist in Metro for some reason :\ – Manak Kapoor Oct 31 '12 at 08:42
-
You can't add a reference to `System.Web.Extensions`? In that case, according to [this](http://stackoverflow.com/questions/9573119/how-to-parse-json-without-json-net-library), you could try the [`System.Json`](http://msdn.microsoft.com/en-us/library/system.json.aspx) namespace, but I'm afraid I'm unfamiliar with it and don't have access to 4.5 so I can't offer any further help. – Rawling Oct 31 '12 at 08:47
-
Thats fine, Thanks for all your help. And nope can't add reference to System.Web.Extensions :\ – Manak Kapoor Oct 31 '12 at 08:48
1
Well i found the solution is to use JSON.NET
public String Serialize(Dictionary<int, Dictionary<String, String>> all)
{
String abc = JsonConvert.SerializeObject(all, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
return abc;
}
public Dictionary<int, Dictionary<String, String>> DeSerialize(String text)
{
Dictionary<int, Dictionary<String, String>> abc;
abc = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<String, String>>>(text, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
});
return abc;
}

Manak Kapoor
- 962
- 3
- 12
- 21