Stops requiring JsonIgnore properties with ItemRequired (#1302)
* Made JsonIgnore properties not required when using ItemRequired. * Update JsonSerializerInternalReader.cs * Update JsonSerializerInternalReader.cs * Update JsonSerializerInternalReader.cs * Remove end of file newline
This commit is contained in:
committed by
James Newton-King
parent
92653ae3f2
commit
b6363c70e6
@@ -1761,5 +1761,42 @@ namespace Newtonsoft.Json.Tests
|
|||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ShouldNotRequireIgnoredPropertiesWithItemsRequired()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
""exp"": 1483228800,
|
||||||
|
""active"": true
|
||||||
|
}";
|
||||||
|
ItemsRequiredObjectWithIgnoredProperty value = JsonConvert.DeserializeObject<ItemsRequiredObjectWithIgnoredProperty>(json);
|
||||||
|
Assert.IsNotNull(value);
|
||||||
|
Assert.AreEqual(value.Expiration, new DateTime(2017, 1, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||||
|
Assert.AreEqual(value.Active, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonObject(ItemRequired = Required.Always)]
|
||||||
|
public sealed class ItemsRequiredObjectWithIgnoredProperty
|
||||||
|
{
|
||||||
|
private static readonly DateTime s_unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
[JsonProperty("exp")]
|
||||||
|
private int _expiration
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return (int)((Expiration - s_unixEpoch).TotalSeconds);
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
Expiration = s_unixEpoch.AddSeconds(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Active { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public DateTime Expiration { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2523,7 +2523,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
|
Required resolvedRequired = property.Ignored ? Required.Default : property._required ?? contract.ItemRequired ?? Required.Default;
|
||||||
|
|
||||||
switch (presence)
|
switch (presence)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user