psych icon indicating copy to clipboard operation
psych copied to clipboard

psych quoting strings if they begin with dash.

Open longspell opened this issue 4 years ago • 1 comments

Strings are quoted if they begin with a dash. This is causing issues for an external parser I'm forced to use and inquiring because of the difference in behaviour with python's yaml module.

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> puts YAML.dump({'key' => 'value' })
---
key: value
=> nil
irb(main):003:0> puts YAML.dump({'key' => '-value' })
---
key: "-value"
=> nil
irb(main):004:0> puts YAML.dump({'-key' => '-value' })
---
"-key": "-value"
=> nil

Compared to python

>>> import yaml
>>> print(yaml.dump({'key': 'value'}))
key: value

>>> print(yaml.dump({'key': '-value'}))
key: -value

>>> print(yaml.dump({'-key': '-value'}))
-key: -value

longspell avatar Mar 03 '21 16:03 longspell

Quotes are legal and necessary if Psych wishes to be backwards compatible with much older YAML parsers. In newer YAML specs (since 1.1), quotes would be optional in a lot more cases due to ambiguous interpretations being addressed with stricter grammar rules. Regardless, it should never be wrong to encapsulate with quotes - all YAML parsers should accept this. Remember, YAML is a superset of JSON, which always requires quotes.

jpfourny avatar Jan 12 '22 18:01 jpfourny