3

I would like to represent tree structures in YAML. Each node of the tree is composed of a (key,value) pair. The key must be a string.

A structure like this would be perfect:

node1: child_A: child_AA: child_AB: 1.2 child_B: node2: 

...except that I sometimes have nodes that have values but are not leaves. For example, let's add a child to child_AB:

node1: child_A: child_AA: child_AB: 1.2 child_ABA: 3.14 child_B: node2: 

This is not valid YAML anymore!

Is there a nice way to correct the last YAML document to make it work while staying true to this (key,value) tree structure?

This one is correct :

node1: child_A: child_AA: child_AB: : 1.2 # using empty string, could also use 'value' or similar child_ABA: 3.14 child_B: node2: 

But I feel it's a shame that something like the previous one is not valid. Any suggestion? YAML extensions? Alternatives languages?

1
  • Why not use XML? It allows to this exact structure. Commented Feb 3 at 13:18

2 Answers 2

5

This is where it becomes useful to separate the value from the child nodes. While this doesn't give you a perfect representation of your desired structure, it is valid YAML. Sometimes you just need to work within the constraints of a given technology, or choose a technology that better models your data structure.

node1: child1: value: 1.2 children: - value: 3.14 children: - value: "..." children: - and on it goes 

While this allows you to use YAML, your structure feels more natural as XML:

<node1> <child_A> <child_AA /> <child_AB foo="1.2" child_ABA="3.14" /> </child_A> <child_B> ... </child_B> </node1> <node2> ... </node2> 

The kind of structural data in your post is exactly the kind of structure that XML excels at, but YAML and JSON can struggle with (hence your question).

The reason people shy away from XML is the processing necessary to turn this into a usable data structure for a particular use case. Depending on the technologies available, you can serialize and deserialize to XML so you can work with strongly-typed objects rather than some generic XML document structure.

You will need to make trade-offs for this, unfortunately. What you want to do can be done with YAML, but it is a little awkward. The alternative is to use XML, which makes this kind of structure more intuitive, but this brings its own challenges when parsing and making sense of the XML structure.

2

As an alternative you can separate tree structure from values like this:

node1: value: 1 children: [node2, node3] node2: children: [node4] node3: value: 2 node4: value: 3 

or you can use parents instead of children.

A big advantage is that this representation also works for non-trees out of the box. You can even create loops if needed. And it is flat, which imo is easier to read, especially when the tree grows.

On the other hand you will need some additional processing and validation. But its not hard at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.