When an object of type Dictionary is serialized the dynamic type of the value is not added to the json string even if TypeNameHandling is set to All. The problem is documented in the unit test below.
[TestMethod]
public void SerializeDeserialize_DictionaryContextContainsGuid_DeserializesItemAsGuid() {
const string contextKey = "k1";
var someValue = Guid.NewGuid();
Dictionary inputContext = new Dictionary();
inputContext.Add(contextKey, someValue);
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() {
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.All
}
string serializedString = JsonConvert.SerializeObject(inputContext, jsonSerializerSettings);
Dictionary deserializedObject = JsonConvert.DeserializeObject(serializedString, jsonSerializerSettings);
Assert.AreEqual(someValue, deserializedObject[contextKey]);
}
In the test above the value of the KeyValuePair is of type Guid. After deserializing the dictionary the value is of type string instead of Guid.
I'm using Json.NET 4.5.11.15520