beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
19 stars 1 forks source link

NimYAML reaches 1.0.0 #53

Closed flyx closed 2 years ago

flyx commented 2 years ago

Name: NimYAML

Author: flyx

Posting: NimYAML has long been in pure maintenance mode. To reflect this, I have set the version number to 1.0.0 for the most recent release (previous releases used 0.x.y versions).

The single significant feature in this release is that the DOM API has been merged into the serialization API. Previously, you had the option to load your YAML either directly into native types (objects, seqs, strings, etc) or into a YamlDocument which is a descriptive, DOM-like representation of the YAML structure which contains YamlNodes. Now, you can have a YamlNode anywhere in your target type and NimYAML will parse the subgraph at that position into a descriptive YamlNode value. For example:

import yaml

type
  Root = object
    id: string
    data: YamlNode

var loaded = loadAs[Root]("""
id: 12345
data:
  a: b
  c: [d, e]
""")

loaded.id = "123456"
echo dump(loaded, tsNone, asNone, defineOptions(outputVersion = ovNone), @[])

This gives

---
id: 123456
data:
  c: [d, e]
  a: b

The use-case for this is when you do not care about certain subgraphs from the YAML input, but do want to keep the contained data. As you can see, NimYAML might not keep the order of mapping keys (due to usage of hash maps), which is conformant to the YAML specification.

The old DOM API is still supported, but carries deprecation warnings from now on. If you want to parse the whole YAML input into a DOM, you can use loadAs[YamlNode](…) now.