-Fixed populating ignored properties with DefaultValueHandling.IgnoreAndPopulate
This commit is contained in:
@@ -46,6 +46,42 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class DefaultValueHandlingTests : TestFixtureBase
|
public class DefaultValueHandlingTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
public class MyClass
|
||||||
|
{
|
||||||
|
[JsonIgnore]
|
||||||
|
public MyEnum Status { get; set; }
|
||||||
|
|
||||||
|
private string _data;
|
||||||
|
public string Data
|
||||||
|
{
|
||||||
|
get { return _data; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_data = value;
|
||||||
|
if (_data != null && _data.StartsWith("Other"))
|
||||||
|
{
|
||||||
|
this.Status = MyEnum.Other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum MyEnum
|
||||||
|
{
|
||||||
|
Default = 0,
|
||||||
|
Other
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void PopulateWithJsonIgnoreAttribute()
|
||||||
|
{
|
||||||
|
string json = "{\"Data\":\"Other with some more text\"}";
|
||||||
|
|
||||||
|
MyClass result = JsonConvert.DeserializeObject<MyClass>(json, new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate });
|
||||||
|
|
||||||
|
Assert.AreEqual(MyEnum.Other, result.Status);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Include()
|
public void Include()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2020,7 +2020,7 @@ To fix this error either change the environment to be fully trusted, change the
|
|||||||
if (property.PropertyContract == null)
|
if (property.PropertyContract == null)
|
||||||
property.PropertyContract = GetContractSafe(property.PropertyType);
|
property.PropertyContract = GetContractSafe(property.PropertyType);
|
||||||
|
|
||||||
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable)
|
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable && !property.Ignored)
|
||||||
property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType));
|
property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType));
|
||||||
break;
|
break;
|
||||||
case PropertyPresence.Null:
|
case PropertyPresence.Null:
|
||||||
|
|||||||
Reference in New Issue
Block a user