behrang / YamlSwift

Load YAML and JSON documents using Swift
MIT License
404 stars 97 forks source link

Support ordered keys #39

Closed seivan closed 8 years ago

seivan commented 8 years ago

It would be great if we could keep the order of the keys as the same as the source file parsed from. I know JSON doesn't maintain order, but it could be useful in scenarios where YAML is used for configurations and we need access to defined variables within the YAML tree.

behrang commented 8 years ago

Per Yaml spec, order of mappings should not be preserved. In Yaml, !!map is the default tag for mappings and it is explicitly specified that order of keys need not be maintained. On the other hand, there is a `!!omap`` tag which specifies that order of keys should be maintained and it is a sequence.

You can maintain order of keys by using a sequence of compact mappings like this:

- key1: value1
- key2: value2
seivan commented 8 years ago

Yeah but it would be good if you could specify OrderedDictionaries to keep a one to one mapping of the textual representation. Right now, can’t ensure that a key has been read in to the system as order is not preserved. JSON has the same problem, so it makes sense since it’s a superset.

But I am wondering if it’s a purely implementation detail? Why does it matter for the user that the keys are not in order?

On 2016Oct04, at 10:00, Behrang Noruzi Niya notifications@github.com wrote:

Per Yaml spec, order of mappings should not be preserved. In Yaml, !!map is the default tag for mappings and it is explicitly specified that order of keys need not be maintained. On the other hand, there is a !!omap` tag which specifies that order of keys should be maintained and it is a sequence.

You can maintain order of keys by using a sequence of compact mappings like this:

behrang commented 8 years ago

I don't get the problem. I don't understand your point in:

can’t ensure that a key has been read in to the system as order is not preserved

It is specified that the user should not rely on order of keys.

seivan commented 8 years ago

Yeah but from my point of view, that seems like an implementation detail. But you're right, since the specification say so, it's probably better not move forward with this.