Home Yaml2JsonNode Basics
Yaml2JsonNode Basics
Cancel

Yaml2JsonNode Basics

Yaml2JsonNode is pretty simple: it converts models of YAML text that has been parsed by YamlDotNet into JsonNodes (and can also convert JsonNodes back into YamlNodes). That’s it.

Why?

OpenAPI and indeed many JSON Schema documents are written in YAML because (I’m told) it’s more human-readable than JSON. Therefore it makes sense to support YAML in the various libraries.

However, just as we let .Net handle the JSON parsing, we will let someone else handle the YAML parsing for us. Ideally, we’d want to parse directly into JsonNode, but conversion between models is also a very acceptable solution.

Using the extensions

First, you’ll need to parse the YAML. Consult the YamlDotNet docs, of course, but a simple way is:

1
2
var stream = new YamlStream();
stream.Load(new StringReader(yamlText));

Now the documents (yes, there could be multiple documents in a single parse) are stored in stream.Documents.

You can convert all of them:

1
var allTheNodes = stream.ToJsonNode();

or just the one you want:

1
var singleNode = stream.Documents[0].ToJsonNode();

If you have browsed down into their structure and you have a particular YamlNode you want converted:

1
var specificJsonNode = specificYamlNode.ToJsonNode();

There’s really not much to this. Just remember one method: .ToJsonNode(). It’ll take care of you.

If you want to go back to YAML for some reason, .ToYamlNode() is your friend.

This library will get JSON into the YamlNode model. You’ll need to understand how to get that back into a string using YamlDotNet. Link to their docs above.

Ahead of Time (AOT) compatibility

Yaml2JsonNode v2 includes updates to support Native AOT applications. However, since YamlDotNet does not provide AOT compatibility, this library also cannot provide compatibility.

The methods in this library have been decorated with the appropriate attributes to generate warnings when compiling in an AOT context.

Contents