Recursive AI

Omit null values in C# YAML serialization

OneYaml is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Leave out nulls, not false or 0.

Terminal
dotnet add package OneYaml

Targets .NET 8 and .NET 10.

C#
using OneYaml;
using static OneYaml.YamlIgnoreCondition;
var options = new YamlSerializerOptions
{ DefaultIgnoreCondition = WhenWritingNull };
var config = new Config(false, 0, null);
Console.Write(YamlSerializer.Serialize(
    config, options));
record Config(
    bool Enabled, int Retries, string? Note);

Prints the YAML without note:

Text
enabled: false
retries: 0

If you forget to pass options to Serialize, the default behavior writes note: null instead of omitting it.

Unzip the example, then dotnet run.

Variations

Omit null on Note only. Leave Owner explicit and keep the empty string in Title:

C#
using OneYaml;
 
Console.Write(YamlSerializer.Serialize(new Config(null, null, "")));
 
record Config(
    [property: YamlIgnore(Condition = YamlIgnoreCondition.WhenWritingNull)]
    string? Note,
    string? Owner,
    string Title);

Prints the YAML without note:

YAML
owner: null
title: ''

The short answer

Set YamlSerializerOptions.DefaultIgnoreCondition to YamlIgnoreCondition.WhenWritingNull and pass the options to YamlSerializer.Serialize. The resulting YAML has no key for a null member. The snippet at the top sets Enabled to false and Retries to 0; both keys are still written.

How do I omit null values without losing false or zero?

Use WhenWritingNull, not WhenWritingDefault, when false and 0 carry meaning. A retry count of zero might mean “do not retry,” while an omitted retry count might tell the program that reads the file to use its own setting. The two conditions are listed in the serializer options. WhenWritingDefault tests the default for the declared member type: false for bool, 0 for int, and null for int?.

This is a setting for writing C# object members to YAML. It does not go through an existing YAML file and remove its null keys. If you write several files with the same settings, reuse the options object; it becomes read-only after its first use.

Is a missing YAML key the same as null or an empty string?

No. In the YAML Core schema, note: null has a present key and a null value. With note: '', the key is present and its value is a string of length zero. If there’s no note key, the mapping has no entry for it at all. WhenWritingNull omits the first kind of value and leaves empty strings alone.

Check what reads the file before choosing between omission and explicit null. An application can interpret a missing setting as “inherit the default” and an explicit null as “clear this value.” If only some settings should disappear, use the member attribute in the variation. On a positional record, property: puts it on the generated property. Without the global ignore setting, unmarked null members such as Owner still appear in the output.

All oneyaml C# recipes