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

When field names are reserved words, they should be written out with quotes #68

Closed
baconmania opened this issue Feb 6, 2018 · 4 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@baconmania
Copy link

baconmania commented Feb 6, 2018

https://github.com/FasterXML/jackson-dataformat-yaml/blob/master/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java specifies that YAML keys will always be written with STYLE_NAME, e.g. without quotes.

    // for field names, leave out quotes
    private final static Character STYLE_NAME = null;
...
    private final void _writeFieldName(String name)
        throws IOException
    {
        _writeScalar(name, "string", STYLE_NAME);
    }

But this breaks parsers when you write a map with a key that is a reserved word in YAML, e.g. no. If _writeFieldName() encounters a field that is one of { y, true, yes, on, n, false, no, off }, it should use STYLE_QUOTED.

Actual definition of such boolean values can be found here:

https://yaml.org/type/bool.html

@cowtowncoder
Copy link
Member

Thank you for reporting this, contributing patch.

Bit odd that SnakeYAML does not do escaping. But as with everything, looks like YAML has gone off the deep end with the rather long list of reserved names.... so maybe it's impractical.

@baconmania
Copy link
Author

baconmania commented Feb 7, 2018

Thanks for the quick response!

And yeah, YAML's actual spec is chaos:

The content of a mapping node is an unordered set of key: value node pairs, with the restriction that
each of the keys is unique. YAML places no further restrictions on the nodes. In particular, keys may be
arbitrary nodes, the same node may be used as the value of several key: value pairs, and a mapping
could even contain itself as a key or a value (directly or indirectly).

Given that, the minimal-change way to support reserved-words keys without actually implementing the full YAML spec in Jackson's/SnakeYAML's writer is adding a feature to just quote all keys.

But hey, I'll ask you one favor: would you able to backport this to jackson-dataformat-yaml 2.7.x?

@cowtowncoder
Copy link
Member

@baconmania Wrt 2.7, unfortunately no. If I followed semantic versioning strictly, this could even go in 2.9 being new feature. But since that branch is open and likely the last 2.x version I don't have problem doing that. However backporting is different story -- 2.7 in particular is closed and only "micro-patches" are to be release (like 2.7.9.1) for critical bugs.

Thank you for feature PR itself; I'll add just one note there.

@cowtowncoder cowtowncoder added the yaml Issue related to YAML format backend label Apr 11, 2018
@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Feb 27, 2019
@cowtowncoder cowtowncoder added this to the 2.9.9 milestone Feb 28, 2019
@cowtowncoder cowtowncoder changed the title When field names are reserved words, they should be written out with quotes. When field names are reserved words, they should be written out with quotes Feb 28, 2019
cowtowncoder added a commit that referenced this issue Feb 28, 2019
@cowtowncoder
Copy link
Member

@baconmania I actually decided to try to add only-if-needed quoting, and not yet adding new feature to force quoting. Happy to add such feature if it's needed of course.

Will finally be in 2.9.9 and 2.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

2 participants