-Add support for sharing a static cache for contract resolvers inheriting from DefaultControlResolver

-Correct error message when casting non-JValue to a privative type
This commit is contained in:
JamesNK
2010-02-27 22:41:25 +00:00
parent d4158bb53c
commit 29b1ff5f0a
9 changed files with 204 additions and 33 deletions
@@ -1449,5 +1449,35 @@ Parameter name: arrayIndex")]
""title"": null
}", output);
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Object to String.")]
public void InvalidValueCastExceptionMessage()
{
string json = @"{
""responseData"": {},
""responseDetails"": null,
""responseStatus"": 200
}";
JObject o = JObject.Parse(json);
string name = (string)o["responseData"];
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Object to String.")]
public void InvalidPropertyValueCastExceptionMessage()
{
string json = @"{
""responseData"": {},
""responseDetails"": null,
""responseStatus"": 200
}";
JObject o = JObject.Parse(json);
string name = (string)o.Property("responseData");
}
}
}
@@ -178,5 +178,13 @@ namespace Newtonsoft.Json.Tests.Linq
JToken t = new JValue(5L);
t[0] = new JValue(3);
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Null to Int32.")]
public void CastNullValueToNonNullable()
{
JValue v = new JValue((object)null);
int i = (int) v;
}
}
}
@@ -333,6 +333,11 @@ namespace Newtonsoft.Json.Tests.Schema
public class CustomDirectoryInfoMapper : DefaultContractResolver
{
public CustomDirectoryInfoMapper()
: base(true)
{
}
protected override JsonContract CreateContract(Type objectType)
{
if (objectType == typeof(DirectoryInfo))
@@ -65,6 +65,14 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
Assert.AreEqual(person.Name, deserializedPerson.Name);
json = JsonConvert.SerializeObject(person, Formatting.Indented);
Assert.AreEqual(@"{
""Name"": ""Name!"",
""BirthDate"": ""\/Date(974764544000)\/"",
""LastModified"": ""\/Date(974764544000)\/""
}", json);
}
[Test]
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Serialization
{
private readonly char _startingWithChar;
public DynamicContractResolver(char startingWithChar)
: base(false)
{
_startingWithChar = startingWithChar;
}
@@ -689,11 +689,11 @@ keyword such as type of business.""
[Test]
public void TimeSpanTest()
{
TimeSpan ts = new TimeSpan(200, 23, 59, 1);
TimeSpan ts = new TimeSpan(00, 23, 59, 1);
string json = JsonConvert.SerializeObject(new ClassWithTimeSpan { TimeSpanField = ts }, Formatting.Indented);
Assert.AreEqual(@"{
""TimeSpanField"": ""200.23:59:01""
""TimeSpanField"": ""23:59:01""
}", json);
ClassWithTimeSpan c = JsonConvert.DeserializeObject<ClassWithTimeSpan>(json);
@@ -3032,5 +3032,29 @@ keyword such as type of business.""
d2.Add(s, 5);
Assert.AreEqual(@"{""host\\user"":5}", JsonConvert.SerializeObject(d2));
}
public class GenericListTestClass
{
public List<string> GenericList { get; set; }
public GenericListTestClass()
{
GenericList = new List<string>();
}
}
[Test]
public void DeserializeExistingGenericList()
{
GenericListTestClass c = new GenericListTestClass();
c.GenericList.Add("1");
c.GenericList.Add("2");
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
GenericListTestClass newValue = JsonConvert.DeserializeObject<GenericListTestClass>(json);
Assert.AreEqual(2, newValue.GenericList.Count);
Assert.AreEqual(typeof(List<string>), newValue.GenericList.GetType());
}
}
}