On Thu, Dec 28, 2006 at 07:11:09AM -0800, Phlip wrote:
| sauce = "--- !map
| - a:
| - !map
| sku : BL394D
| quantity : 4
| description : Basketball
| - b: two"
| o = YAML::parse(sauce)
| p o.emit
I'm a bit puzzled why this works "out of the box" like that, since
a !map should require a mapping for its container. The following
for example is an error, since it is passed a sequence,
I guess, when passed a sequence of key/value pairs, the !map constructor
is building an unordered dictionary for you...
| If I change the first !map to !omap, the code still parses correctly.
| But I need to depend on document order for the second-level nodes.
Yes, this isn't a problem for the *first* level, since an ordered
mapping requires a sequence of pairs. This is the usual form...
- foo: bar
- key: value
| Changing the second !map to !omap then produces this syntax error:
The error happens here since the !omap constructor is expecting a
sequence of pairs, and you are giving it a mapping. In the YAML
information model, the constructor happens *after* the order found in
the YAML file is discarded... so if you want to maintain order, you have
to explicitly record this intention with a sequence.
sauce = "--- !map
- sku : BL394D
- quantity : 4
- description : Basketball
- b: two"
o = YAML::parse(sauce)
The above should work... since I've added in the sequence you need.