Using Newtonsoft's Json.NET serializer, is it possible to require a property to contain a non-null value and throw an exception on serialization if this not the case? Something like:
public class Foo
{
    [JsonProperty("bar", SerializationRequired = SerializationRequired.DisallowNull)]
    public string Bar { get; set; }
}
I know it is possible to do this upon deserialization (using the Required property of JsonProperty), but I cannot find anything on this for serialization.
This is now possible by setting the JsonPropertyAttribute to Required.Always.
This requires Newtonsoft 12.0.1+, which didn't exist at the time this question was asked.
The example below throws a JsonSerializationException ( "Required property 'Value' expects a value but got null. Path '', line 1, position 16." ):
void Main()
{
    string json = @"{'Value': null }";
    Demo res = JsonConvert.DeserializeObject<Demo>(json);
}
class Demo
{
    [JsonProperty(Required = Required.Always)]
    public string Value { get; set;}
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With