using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
protected Foo(int number, string text)
if (number > 10 && text == null)
throw new ArgumentException("missing text");
[JsonProperty(Required = Required.Always)]
public int Number { get; set; }
public string Text { get; set; }
public static void Test()
Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Foo>("""{ "Number": 5 }"""));
Assert.That(() => JsonConvert.DeserializeObject<Foo>("""{ "Number": 20 }"""), Throws.Exception);
Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Foo>("""{ "Number": 20, "Text": "Bar" }"""));
Assert.That(() => JsonConvert.DeserializeObject<Foo>("""{ "Number": 20, "Text" : null }"""), Throws.Exception);
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");