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

FeatureRequest: Add PRESERVE_COMMENTS feature for SnakeYAML > 2.0 #491

Open
bmarwell opened this issue Aug 20, 2024 · 3 comments
Open

FeatureRequest: Add PRESERVE_COMMENTS feature for SnakeYAML > 2.0 #491

bmarwell opened this issue Aug 20, 2024 · 3 comments
Labels
yaml Issue related to YAML format backend

Comments

@bmarwell
Copy link

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?

@cowtowncoder cowtowncoder added the yaml Issue related to YAML format backend label Aug 20, 2024
@cowtowncoder
Copy link
Member

No, this cannot be done currently.

But this has come up multiple times, and I think we have some existing issues.
One big problem is that it is very difficult to preserve comment content in JsonNode (or similar) since they can exist anywhere within content model. So while adding ability read/write comments at low level (parser/generator) is quite possible, we have nothing to do that at higher level(s).

So this is the problem to resolve.

Also affects XML, fwtw, and likely other formats (TOML at least? even some binary formats; albeit nothing with schema).

@bmarwell
Copy link
Author

Yes, I suspected this much. I will wonder how to deal with that in tools that consume and auto-sort YAML, maybe do replacements after saving files. Thanks for your quick confirmation! 👍🏻

@yawkat
Copy link
Member

yawkat commented Aug 26, 2024

#36 (yaml), #254 (toml)

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

3 participants