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

Most safe/generic/features-wide way to format yaml files #372

Closed
blacelle opened this issue Jan 11, 2023 · 7 comments
Closed

Most safe/generic/features-wide way to format yaml files #372

blacelle opened this issue Jan 11, 2023 · 7 comments

Comments

@blacelle
Copy link

While working on diffplug/spotless#1478, to integrate Jackson as a YAML formatter, I encounter various issues/limitations.

The naive approach was to load a YML as an Object with ObjectMapper.readValue(...), and output right-away with ObjectMapper.writeValueAsString after adding some feature (e.g. INDENT_OUTPUT).

I encounter issues relating to:

  1. multiple documents
  2. anchors/references
  3. comments
  4. certainly many more features

Does jackson team has some recommendation for generic .yaml formatting? If Jackson is not a good option, would you have any recommanded alternative ? (e.g. stick we SnakeYml).

Thanks

@blacelle

This comment was marked as resolved.

@cowtowncoder
Copy link
Member

@blacelle I think you are right in that Jackson's support for formatting of every kind of YAML is lacking in areas you mention. I hope over time we can improve some, but fundamental I think you are right in that using SnakeYAML (or, newer snakeyaml-engine from same author) is probably better way to go.

I think some things, like handling of multiple documents, are easier than others (comments are actually challenging since they are sort of "fluff", non-data content that can exist anywhere, and Jackson token stream has no way to express them).
Anchors/references' handling could perhaps also be improved but... all of these would need to be tackled separately. Filing separate issues might be the way to go (showing in each case what is missing).

@blacelle
Copy link
Author

Multiple documents per file is described in #66. It seems to cover our case.

@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 18, 2023

Yes, and I think #132, fixed in 2.10.2, made it work (for SequenceWriter which is the way to deal with conceptual "multiple root values/docs" use case; slightly different semantics depending on format; Docs for YAML).
And then its counterpart, .... MappingIterator (via ObjectWriter.readValues()) for reading multi-YAML-doc streams which I think has worked longer.

@blacelle
Copy link
Author

About comments, they are discussed here #36.

As some point, I thought that by deserializing a String into an ObjectNode, it would keep comments (as ObjectNode would kind of retain the most granular information reachable by Jackson). True of false, once YAML comments would be manageable with Jackson (a way or another), I suppose such a trick (i.e. referring to some technical class like ObjectNode could make the trick).

For now, comments with Jackson seems not reachable (while SnakeYAML may have recently make this reachable (#36 (comment)))

@blacelle
Copy link
Author

About anchors, #120 is relevant but not very active. It suggests (like previous comment about ObjectNode) that deserializing into a JsonNode might retain anchors at some point in the future (this is just an option/a possible future design).

@cowtowncoder
Copy link
Member

Yeah, comments are by far the most difficult ones due to multiple reasons.
Anchors/refs, type ids (tags?) are also... challenging, as JsonNode is designed to be sort of extensible, but only for custom implementations, not by adding metadata.
In particular, same node types are immutable (boolean and null node) and it'd be necessary to change that somehow.

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

No branches or pull requests

2 participants