-Changed Value<T> on JObject to support getting nullable values
-Fixed JsonSerializer to correctly skip missing members
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<bool?>("foo");
|
||||
Assert.AreEqual(true, value);
|
||||
|
||||
json = @"{""foo"":null}";
|
||||
o = (JObject)JavaScriptConvert.DeserializeObject(json);
|
||||
value = o.Value<bool?>("foo");
|
||||
Assert.AreEqual(null, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user