-Added support for deserializing IDictionary properties

This commit is contained in:
JamesNK
2011-08-03 01:53:49 +00:00
parent 44ab7f71df
commit d8b32dfdc4
2 changed files with 51 additions and 0 deletions
@@ -4748,5 +4748,52 @@ keyword such as type of business.""
}", json);
}
#endif
public class ClassWithException
{
public IList<Exception> Exceptions { get; set; }
public ClassWithException()
{
Exceptions = new List<Exception>();
}
}
#if !(SILVERLIGHT || WINDOWS_PHONE)
[Test]
public void SerializeException1()
{
ClassWithException classWithException = new ClassWithException();
try
{
throw new Exception("Test Exception");
}
catch (Exception ex)
{
classWithException.Exceptions.Add(ex);
}
string sex = JsonConvert.SerializeObject(classWithException);
ClassWithException dex = JsonConvert.DeserializeObject<ClassWithException>(sex);
Assert.AreEqual(dex.Exceptions[0].ToString(), dex.Exceptions[0].ToString());
sex = JsonConvert.SerializeObject(classWithException, Formatting.Indented);
dex = JsonConvert.DeserializeObject<ClassWithException>(sex); // this fails!
Assert.AreEqual(dex.Exceptions[0].ToString(), dex.Exceptions[0].ToString());
}
#endif
public void DeserializeIDictionary()
{
IDictionary dictionary = JsonConvert.DeserializeObject<IDictionary>("{'name':'value!'}");
Assert.AreEqual(1, dictionary.Count);
Assert.AreEqual("value!", dictionary["name"]);
}
public void DeserializeIList()
{
IList list = JsonConvert.DeserializeObject<IList>("['1', 'two', 'III']");
Assert.AreEqual(3, list.Count);
}
}
}
@@ -75,6 +75,10 @@ namespace Newtonsoft.Json.Serialization
{
CreatedType = ReflectionUtils.MakeGenericType(typeof(Dictionary<,>), keyType, valueType);
}
else if (UnderlyingType == typeof(IDictionary))
{
CreatedType = typeof (Dictionary<object, object>);
}
}
internal IWrappedDictionary CreateWrapper(object dictionary)