aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to emit an empty Scalar

SchreinerK opened this issue · comments

emitter.Emit(new MappingStart());
emitter.Emit(new Scalar("Nothing"));
emitter.Emit(new Scalar()); ??? emitter.Emit(new Scalar(null); ??? bug or missing feature?
emitter.Emit(new Scalar("EmptyString"));
emitter.Emit(new Scalar(""));
emitter.Emit(new MappingEnd());

as result:

Nothing:
EmptyString: ''

The parser works fine:

<StreamStart>
<DocumentStart>
<MappingStart>
<Scalar> "Nothing" (Plain)
<Scalar> "" (Plain)    <-- here is the empty Scalar
<Scalar> "EmptyString" (Plain)
<Scalar> "" (SingleQuoted)
...

It may also be that I am simply blind ;-)

What is the yaml you’re trying to get back?

The same as above

Nothing:
EmptyString: ''

The serializer can do that, I just don't know how to achieve that with the emitter.

When you new up the scalar try setting the scalar style to plain and use an empty string.

I think I tried all possible parameters, but only an empyt string Nothing: '' is written.

The answer is:

emitter.Emit(new Scalar(AnchorName.Empty, new TagName("tag:yaml.org,2002:null"),  "", ScalarStyle.Plain, true, false));

I happened to find this in the code: new TagName("tag:yaml.org,2002:null") and just tried it out to see what it does.

But IMHO the following line would be more legible, only as a suggestion:

emitter.Emit(new Scalar(null));

Since there was an answer, I'm going to close this issue.