Skip to content

Why is yaml.stringify({time: "12:00"}) not adding quotes to the resulting value? #264

Closed Answered by eemeli
aisamu asked this question in Q&A
Discussion options

You must be logged in to vote

It's because yaml defaults to the "core" schema of YAML 1.2, which does not include sexagesimal numbers. If you're working with another YAML library that uses the YAML 1.1 schema, it might make good sense to use the version: '1.1' option:

YAML.parse('time: 12:00')
> { time: '12:00' }
YAML.parse('time: 12:00', { version: '1.1' })
> { time: 720 }
YAML.parse('time: "12:00"', { version: '1.1' })
> { time: '12:00' }

YAML.stringify({ time: '12:00' })
> 'time: 12:00\n'
YAML.stringify({ time: '12:00' }, { version: '1.1' })
> 'time: "12:00"\n'

The logic here is that unless otherwise specified, if it looks like a string could be written as a plain scalar, we do that. But if re-parsing that output …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aisamu
Comment options

Answer selected by aisamu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants