I'm actually filling up a Dictionary<string, object>
, with objects of different kinds (it's a sort of property bag).
I'd like to be able to serialize and deserialize this dictionary. My problem is, when I serialize it with json.net it does not store type information for the values in the dictionary.
Here's an example:
Dictionary<string, object> items = new Dictionary<string, object>();
items.Add("a number", 1.23f);
items.Add("a point", new Point(5,6));
JsonSerializerSettings settings = new JsonSerializerSettings() { Formatting = Formatting.Indented, TypeNameHandling=TypeNameHandling.All };
var serialized = JsonConvert.SerializeObject(items, settings);
I end up with this in the serialized
variable:
{
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib",
"a number": 1.23,
"a point": "5,6"
}
So it actually stores the type for the dictionary itself(which is kinda useless for me here), but does not store type information for the objects themselves. I'd like it to save the type information of the objects themselves. Something like this maybe:
{
{ "a number": 1.23, "$type" : "System.Float, mscorlib..." },
{ "a point": "5,6", "$type" : "System.Drawing.Point..." }
}
And, of course, I'd like it to use this type information when deserializing, so that e.g. the second value in the dictionary actually gets deserialized as a Point instead of as a string.
Is there a way to do this?