Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null value not properly serialized #611

Closed
Thf772 opened this issue May 17, 2021 · 4 comments
Closed

Null value not properly serialized #611

Thf772 opened this issue May 17, 2021 · 4 comments

Comments

@Thf772
Copy link

Thf772 commented May 17, 2021

Description

When serializing, null values (that is, null references, not strings with the value "null") are written as an empty string instead of an actual YAML null. The possibility to serialize null values to actual null should be present either as a default or an option to the SerializerBuilder class.

As a side not, this issue is related to issues #591, #387 and #304 (ambiguous serialization of null-like values between YAML and CLR).

To reproduce

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YamlDotNet.Serialization;

class Program {
    static async Task Main(string[] args) {
        Dictionary<string, object> data = new() {
            ["null_test"] = null,
        };

        await using MemoryStream outStream = new();
        await using StreamWriter output = new(outStream, new UTF8Encoding());

        ISerializer serializer = new SerializerBuilder().DisableAliases().WithIndentedSequences().Build();
        serializer.Serialize(output, data);

        await output.FlushAsync();
        Console.WriteLine(new UTF8Encoding().GetString(outStream.ToArray()));
    }
}

Expected output:

null_test: null

or

null_test: ~

Actual output:

null_test: 
@yamldotnet
Copy link

null_test: null

or

null_test: ~

This YAML snippet appears to be invalid.
The following errors were identifier by the reference parser:

  • Line 2, char 1: Unexpected '`'

@pensono
Copy link
Contributor

pensono commented Jun 22, 2021

The empty string is a valid, but not canonical, spelling of the value null.

https://yaml.org/type/null.html

@Thf772
Copy link
Author

Thf772 commented Aug 18, 2021

The empty string is a valid, but not canonical, spelling of the value null.

https://yaml.org/type/null.html

I didn't know empty strings were correct for null values, thanks for clarifying.

Unfortunately, this means that deserializing an object serialized with YamlDotNet will yield a different object, since YamlDotNet itself interprets these null empty strings as non-null empty strings. So there is still a need to offer canonical serialization of nulls...

@EdwardCooke
Copy link
Collaborator

This will be fixed when the next release goes out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants