-Added Required.DisallowNull
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user