Skip to content

FeatureRequest: Add PRESERVE_COMMENTS feature for SnakeYAML > 2.0Β #491

@bmarwell

Description

@bmarwell

Hi,

Description

currently I am using Jackson with dataformat-yaml to sort my yaml files by key:

 final YAMLFactory yamlFactory = new YAMLFactory()
        .disable(YAMLGenerator.Feature.SPLIT_LINES)
        .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
        ;
    this.objectMapper = new ObjectMapper(yamlFactory)
        .registerModule(new Jdk8Module())
        .setDefaultPrettyPrinter(new DefaultPrettyPrinter())
        .configure(SerializationFeature.INDENT_OUTPUT, true)
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, orderMapEntriesByKeys)
    ;

Now, comments will get lost regardless of which method I use to read and write the document.

SnakeYAML features

However, with SnakeYAML alone you could do something like this:

  1. Create a new Reader/Writer which preserves comments: https://stackoverflow.com/a/76975195/1549977
  2. Create a custom Representer which sorts keys: https://stackoverflow.com/a/63095199/1549977

Wishlist

Ideally, a comment above a key in the same indentation level stays with the key. If it is the same indentation level as the previous element, keep it below that one.

Can this be done in Jackson with dataformat-yaml?

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions