rtluckie/seria

Preserve node anchors when converting from json -> yaml and restore from json -> yaml.

Opened this issue · 0 comments

Consider the following sample.yaml:

again: &again
  x: several
  y: repeated
  z: values
a:
  again: *again
  k: v
b:
  again: *again
  k: v

This is the output of cat sample.yaml | seria -j | seria -y:

again:
  x: several
  y: repeated
  z: values
a:
  again:
    x: several
    y: repeated
    z: values
  k: v
b:
  again:
    x: several
    y: repeated
    z: values
  k: v

... which is entirely sensible. Some might even say correct. However, wouldn't it be neat if when we got back to yaml the node anchors were back in place?

I'm wondering if you think it would be to hacky to take a switch in the cli to preserve metadata, that stored a known metadata object allowing us to get back. For instance, cat sample.yaml | seria -j --preserve-anchors might look like:

{
  "again": {
    "x": "several",
    "y": "repeated",
    "z": "values"
  },
  "a": {
    "again": {
      "x": "several",
      "y": "repeated",
      "z": "values"
    },
    "k": "v"
  },
  "b": {
    "again": {
      "x": "several",
      "y": "repeated",
      "z": "values"
    },
    "k": "v"
  },
  "_meta": {
    "anchor": {
      "/again": [
        "/a",
        "/b"
      ]
    }
  }
}

Then, back to yaml, cat sample.yaml | seria -j --preserve-anchors | seria -y would output with our original node anchors.

I think that would also allow the preservation of comments.

I have no use for this. But I noticed it trying something unimportant. It is more of a thought experiment.