diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 2938d528..6b7654a5 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -1063,7 +1063,7 @@ keyword such as type of business."" this.phoneNumber = phoneNumber; } } - + [Test] public void SerializeMemberGenericList() { @@ -1471,5 +1471,26 @@ keyword such as type of business."" Assert.AreEqual(null, userNullablleDeserialized.NullRoleId); Assert.AreEqual(true, userNullablleDeserialized.Active); } + + public class MyClass + { + public int PreProperty { get; set; } + //public DateTime DateProperty { get; set; } + public int PostProperty { get; set; } + } + + [Test] + public void MissingMemberIgnoreComplexValue() + { + JsonSerializer serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore }; + serializer.Converters.Add(new JavaScriptDateTimeConverter()); + + string response = @"{""PreProperty"":1,""DateProperty"":new Date(1225962698973),""PostProperty"":2}"; + + MyClass myClass = (MyClass)serializer.Deserialize(new StringReader(response), typeof(MyClass)); + + Assert.AreEqual(1, myClass.PreProperty); + Assert.AreEqual(2, myClass.PostProperty); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index c87bf5c0..e0d3739b 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -330,5 +330,19 @@ Parameter name: arrayIndex")] Assert.AreEqual(JavaScriptConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v); } + + [Test] + public void GenericValueCast() + { + string json = @"{""foo"":true}"; + JObject o = (JObject)JavaScriptConvert.DeserializeObject(json); + bool? value = o.Value("foo"); + Assert.AreEqual(true, value); + + json = @"{""foo"":null}"; + o = (JObject)JavaScriptConvert.DeserializeObject(json); + value = o.Value("foo"); + Assert.AreEqual(null, value); + } } } diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 986d89f7..3c748c44 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -518,6 +518,8 @@ namespace Newtonsoft.Json { if (_missingMemberHandling == MissingMemberHandling.Error) throw new JsonSerializationException("Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, targetType.GetType().Name)); + + reader.Skip(); } } diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs index 11cd62ec..b0b9ff0b 100644 --- a/Src/Newtonsoft.Json/Linq/Extensions.cs +++ b/Src/Newtonsoft.Json/Linq/Extensions.cs @@ -267,7 +267,17 @@ namespace Newtonsoft.Json.Linq if (value == null) throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T))); - return (U)System.Convert.ChangeType(value.Value, typeof(U), CultureInfo.InvariantCulture); + Type targetType = typeof(U); + + if (ReflectionUtils.IsNullableType(targetType)) + { + if (value.Value == null) + return default(U); + + targetType = Nullable.GetUnderlyingType(targetType); + } + + return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture); } }