1

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!

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.