using System.Text.Json.Serialization;
public string Test1 {get; set;}
public string? Test2 {get; set;}
public bool Test3 {get; set;}
public bool? Test4 {get; set;}
public static void Main()
var body = new Test(){Test1 = "hi"};
var jsonString = JsonSerializer.Serialize(body, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault });
Console.WriteLine("With WhenWritingDefault");
Console.WriteLine(jsonString);
jsonString = JsonSerializer.Serialize(body, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
Console.WriteLine("\n\nWith WhenWritingNull");
Console.WriteLine(jsonString);