-Added Required.DisallowNull

This commit is contained in:
James Newton-King
2015-11-15 21:25:47 +13:00
parent 5ae9edc926
commit 000407f610
5 changed files with 77 additions and 2 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
$zipFileName = "Json80r1.zip"
$majorVersion = "8.0"
$majorWithReleaseVersion = "8.0.1"
$nugetPrelease = "beta2"
$nugetPrelease = "beta3"
$version = GetVersion $majorWithReleaseVersion
$packageId = "Newtonsoft.Json"
$signAssemblies = $false
@@ -237,6 +237,22 @@ namespace Newtonsoft.Json.Tests.Serialization
}, "Required property 'RequiredProperty' expects a value but got null. Path '', line 1, position 21.");
}
[Test]
public void CoercedEmptyStringWithRequired_DisallowNull()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.DeserializeObject<Binding_DisallowNull>("{requiredProperty:''}");
}, "Required property 'RequiredProperty' expects a non-null value. Path '', line 1, position 21.");
}
[Test]
public void DisallowNull_NoValue()
{
Binding_DisallowNull o = JsonConvert.DeserializeObject<Binding_DisallowNull>("{}");
Assert.IsNull(o.RequiredProperty);
}
[Test]
public void CoercedEmptyStringWithRequiredConstructor()
{
@@ -252,6 +268,46 @@ namespace Newtonsoft.Json.Tests.Serialization
public Binding RequiredProperty { get; set; }
}
public class Binding_DisallowNull
{
[JsonProperty(Required = Required.DisallowNull)]
public Binding RequiredProperty { get; set; }
}
[Test]
public void Serialize_Required_DisallowedNull()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.SerializeObject(new Binding_DisallowNull());
}, "Cannot write a null value for property 'RequiredProperty'. Property requires a non-null value. Path ''.");
}
[Test]
public void Serialize_Required_DisallowedNull_NullValueHandlingIgnore()
{
string json = JsonConvert.SerializeObject(new Binding_DisallowNull(), new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Assert.AreEqual("{}", json);
}
[JsonObject(ItemRequired = Required.DisallowNull)]
public class DictionaryWithNoNull
{
public string Name { get; set; }
}
[Test]
public void Serialize_ItemRequired_DisallowedNull()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.SerializeObject(new DictionaryWithNoNull());
}, "Cannot write a null value for property 'Name'. Property requires a non-null value. Path ''.");
}
public class DictionaryKeyContractResolver : DefaultContractResolver
{
protected override string ResolveDictionaryKey(string dictionaryKey)
+6 -1
View File
@@ -43,6 +43,11 @@ namespace Newtonsoft.Json
/// <summary>
/// The property must be defined in JSON and cannot be a null value.
/// </summary>
Always = 2
Always = 2,
/// <summary>
/// The property is not required but it cannot be a null value.
/// </summary>
DisallowNull = 3
}
}
@@ -2193,7 +2193,9 @@ namespace Newtonsoft.Json.Serialization
{
case PropertyPresence.None:
if (resolvedRequired == Required.AllowNull || resolvedRequired == Required.Always)
{
throw JsonSerializationException.Create(reader, "Required property '{0}' not found in JSON.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
}
if (!property.Ignored)
{
@@ -2206,7 +2208,13 @@ namespace Newtonsoft.Json.Serialization
break;
case PropertyPresence.Null:
if (resolvedRequired == Required.Always)
{
throw JsonSerializationException.Create(reader, "Required property '{0}' expects a value but got null.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
}
if (resolvedRequired == Required.DisallowNull)
{
throw JsonSerializationException.Create(reader, "Required property '{0}' expects a non-null value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
}
break;
}
}
@@ -490,7 +490,13 @@ namespace Newtonsoft.Json.Serialization
JsonObjectContract objectContract = contract as JsonObjectContract;
Required resolvedRequired = property._required ?? ((objectContract != null) ? objectContract.ItemRequired : null) ?? Required.Default;
if (resolvedRequired == Required.Always)
{
throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
}
if (resolvedRequired == Required.DisallowNull)
{
throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a non-null value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
}
}
return true;