Skip to content

Add YAMLGenerator.Feature.ALLOW_LONG_KEYS to allow writing keys longer than 128 characters (default) #244

Closed
@msmsimondean

Description

@msmsimondean

At the moment, a field name with a length of 128 characters or longer results in Jackson's YAML generator producing YAML like this:

---
? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
: anything

Using 1 less character in the field name results in the usual YAML syntax.

---
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: anything

Apparently the two YAML styles are called simple keys and complex keys. SnakeYAML provides an option in its DumperOptions to change the max field name length at which SnakeYAML will switch from a) using the simple key style to b) using the complex key style. Here it is:

https://github.com/asomov/snakeyaml/blob/88c727ce266533c1db50ac24fbd3a3f33f7fe5a0/src/main/java/org/yaml/snakeyaml/DumperOptions.java#L449-L463

    public int getMaxSimpleKeyLength() {
        return maxSimpleKeyLength;
    }

    /**
     * Define max key length to use simple key (without '?')
     * More info https://yaml.org/spec/1.1/#id934537
     * @param maxSimpleKeyLength - the limit after which the key gets explicit key indicator '?'
     */
    public void setMaxSimpleKeyLength(int maxSimpleKeyLength) {
        if(maxSimpleKeyLength > 1024) {
            throw new YAMLException("The simple key must not span more than 1024 stream characters. See https://yaml.org/spec/1.1/#id934537");
        }
        this.maxSimpleKeyLength = maxSimpleKeyLength;
    }

Please could Jackson's YAML lib provide a YAML feature for using a higher value for maxSimpleKeyLength. E.g. a LONG_SIMPLE_KEYS feature that sets maxSimpleKeyLength to something like 1024.

See https://bitbucket.org/asomov/snakeyaml/issues/431/increase-simple-key-length-to-1024-stream for details of what the DumperOptions setting was added to SnakeYAML. That issue mentions that a code change to Jackson's YAML lib would be needed to take advantage of the new setting when using Jackson. Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions