Pregunta

today I found following strange behavior in yaml-cpp library.

Following yaml fragment:

- { a: b }

is correctly parsed as key:value element with key=a and value=b. But when I updated fragment to this:

- { a:b }

fragment is evaluated as scalar value "a:b".

Is this correct behavior? And is there a simply way how to force parser to evaluate this fragment as key:value ?

Thanks!

¿Fue útil?

Solución

This is correct behavior. From the YAML spec:

Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key: value pair.

...

To ensure JSON compatibility, if a key inside a flow mapping is JSON-like, YAML allows the following value to be specified adjacent to the “:”. This causes no ambiguity, as all JSON-like keys are surrounded by indicators.

For example, you could write:

- { "a":b }

However, as they point out, this isn't very readable; stick with putting a space after the colon.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top