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

YamlScalarNode with "NULL" value should be written with surrounding quotes #591

Closed
AArnott opened this issue Mar 3, 2021 · 3 comments
Closed

Comments

@AArnott
Copy link

AArnott commented Mar 3, 2021

When manually writing out a YamlMappingNode with YamlScalarNode("NULL") as a key or value, I get this:

NULL: value
e: NULL

This is apparently a bad write job, given that NULL outside of quotes actually means null to YAML, producing a null mapping node instead of NULL (the string) as a key in the map.

When I paste this into http://yaml-online-parser.appspot.com/, the json representation is (appropriately):

{
  "null": "value", 
  "e": null
}

Note the change in case, because the YAML was read as null instead of as the intended string NULL. JSON has to surround a javascript null with quotes when it appears as a property name, but consider when the null is on the value side.

The emitted YAML should have been:

"NULL": value
e: "NULL"

Note the quotes around the yaml-keyword, which then forces it to be read as a string literal by other YAML parsers:

{
  "NULL": "value", 
  "e": "NULL"
}

Repro steps

using System;
using System.IO;
using YamlDotNet.RepresentationModel;

var mappingNode = new YamlMappingNode(
    new YamlScalarNode("NULL"), new YamlScalarNode("value"),
    new YamlScalarNode("e"), new YamlScalarNode("NULL")
    );
var doc = new YamlDocument(mappingNode);
var stream = new YamlStream(doc);
var sb = new StringWriter();
stream.Save(sb);

Console.WriteLine(sb);

Actual output

&428684954 NULL: value
e: NULL
...

Expected output

&428684954 "NULL": value
e: "NULL"
...

Deserialization fails

This bug becomes a more practical problem because the deserializer fails on null keys:

using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.RepresentationModel;

var mappingNode = new YamlMappingNode(
    new YamlScalarNode("NULL"), new YamlScalarNode("V")
    );
var doc = new YamlDocument(mappingNode);
var stream = new YamlStream(doc);
var sb = new StringWriter();
stream.Save(sb);

Console.WriteLine(sb);

var deserializer = new YamlDotNet.Serialization.Deserializer();
var data = deserializer.Deserialize<Dictionary<string, string>>(new StringReader(sb.ToString()));

class Detail
{
    public string H { get; set; }
}

The above throws an exception on deserialization unless I change "NULL" to "NULL2".
If I read YAML that properly quotes the NULL, deserialization succeeds.

@AArnott
Copy link
Author

AArnott commented Mar 3, 2021

Maybe related to #304

@fireflycons
Copy link

I second this, especially where a key name is NULL. I would also like to emit numeric keys quoted.
What I don't see is a way to tap into the serializer with an object that can control how keys are rendered, e.g. some kind of IKeySerializer

@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

3 participants