using System.Text.Json.Serialization;
public static void Main()
var test = new TestClass();
var jsonWithoutOptions = JsonSerializer.Serialize(test);
var jsonWithOptions = JsonSerializer.Serialize(test, new JsonSerializerOptions
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Console.WriteLine(jsonWithoutOptions);
Console.WriteLine(jsonWithOptions);
var testRecord = new TestRecord(null);
var recordWithoutOptions = JsonSerializer.Serialize(testRecord);
var recordWithOptions = JsonSerializer.Serialize(testRecord, new JsonSerializerOptions
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Console.WriteLine(recordWithoutOptions);
Console.WriteLine(recordWithOptions);
public string Test {get; set;}
public record TestRecord(string Test);