Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyphens in key names (dumper) #37

Open
jgm opened this issue Oct 6, 2019 · 5 comments
Open

Hyphens in key names (dumper) #37

jgm opened this issue Oct 6, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@jgm
Copy link
Contributor

jgm commented Oct 6, 2019

I've been trying out dumping YAML from the structure we use to represent pandoc command-line options (with HsYAML-aeson).

I'm getting this:

abbreviations: null
ascii: false
"base-header-level": 1
"cite-method": Citeproc
etc.

Keys with hyphens are quoted. This isn't necessary in YAML, so the quotes are overkill and undesirable. Can the code that determines when a key name needs quoting be tweaked to make it less aggressive? (I could take a look if you point me to the right place in the code.)

I'm assuming this is an issue with HsYAML itself rather than HsYAML-aeson.

[EDIT: fixed YAML nomenclature]

@jgm jgm changed the title Hyphens in tag names (dumper) Hyphens in key names (dumper) Oct 7, 2019
@vijayphoenix
Copy link
Collaborator

The quoting is done just to avoid ambiguity because some scalars can be misinterpreted to be something else.
For example a Haskell string like:

str = "- This is a string (not a sequence)" :: [Text]

when dumped without quotes, it will give us the following YAML output.

- This is a string (not a sequence)

This output will be misinterpreted to be sequence by a YAML parser.
So, if we quote it then there is no ambiguity.

To avoid such quoting, you can make a new custom schemaEncoder and change the code at line 490
to

| T.any (not. isPlainChar) t       = Right (untagged, Plain, t)

Now the dump YAML files will not have DoubleQuotes.

@hvr
Copy link
Collaborator

hvr commented Oct 7, 2019

@vijayphoenix would it be possible to detect more accurately when quoting is necessary? i.e. when the presence of hyphens in a string doesn't require quoting as there's no grammar ambiguity?

@hvr hvr added the enhancement New feature or request label Oct 7, 2019
@jgm
Copy link
Contributor Author

jgm commented Oct 7, 2019

Thanks, that makes sense. I know that a hyphen at the beginning of the key name requires quoting, but an internal one should be okay, I believe, and these are quite common. They even occur in examples in the YAML spec documentation. Ideally HsYAML would make that distinction and avoid the unnecessary quoting. (You guys are the experts on the YAML spec, though! There may be something I'm missing.)

@vijayphoenix
Copy link
Collaborator

@vijayphoenix would it be possible to detect more accurately when quoting is necessary? i.e. when the presence of hyphens in a string doesn't require quoting as there's no grammar ambiguity?

Yeah, we could definitely improve in this area.
But it will be great if we could figure out an elegant solution to handle quoting due to the presence of some other special characters like -?:,[]{}#&*!,>%@

@vijayphoenix vijayphoenix self-assigned this Oct 7, 2019
@perlpunk
Copy link

perlpunk commented Oct 7, 2019

like -?:,[]{}#&*!,>%@

and

`|'"

plus whitespace of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants