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

Add option to allow use of platform-linefeed (YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS) #84

Closed
thauk-copperleaf opened this issue May 1, 2018 · 3 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@thauk-copperleaf
Copy link

The behaviour I expect would be for the serializer to produce output with line endings consistent with that platform it's running on, unless explicitly overriden.

I'm just wondering if this is on purpose or not?

Deeper dive:

YAMLGenerator uses DumperOptions from snakeyaml and overrides some settings, but does not override the line ending setting:

protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures,
org.yaml.snakeyaml.DumperOptions.Version version)
{
DumperOptions opt = new DumperOptions();
// would we want canonical?
if (Feature.CANONICAL_OUTPUT.enabledIn(_formatFeatures)) {
opt.setCanonical(true);
} else {
opt.setCanonical(false);
// if not, MUST specify flow styles
opt.setDefaultFlowStyle(FlowStyle.BLOCK);
}
// split-lines for text blocks?
opt.setSplitLines(Feature.SPLIT_LINES.enabledIn(_formatFeatures));
// array indentation?
if (Feature.INDENT_ARRAYS.enabledIn(_formatFeatures)) {
// But, wrt [dataformats-text#34]: need to set both to diff values to work around bug
// (otherwise indentation level is "invisible". Note that this should NOT be necessary
// but is needed up to at least SnakeYAML 1.18.
// Also looks like all kinds of values do work, except for both being 2... weird.
opt.setIndicatorIndent(1);
opt.setIndent(2);
}
return opt;
}

And since DumperOptions has the following default:
private LineBreak lineBreak = LineBreak.UNIX;

That results in output having Unix line endings, regardless of platform.

Alternatively, can you tell me how to produce YAML with platform-specific line endings?

@cowtowncoder
Copy link
Member

Generator uses SnakeYAML defaults so this is not conscious definition. I actually do not know what YAML specs says: I assume it probably allows multiple styles of linefeeds just like JSON and XML.
Whether platform defaults should be used or canonical defaults is an open question; since both are (I presume) legit I don't have strong opinion.

I would be open to having an option to change behavior: being an API change, it needs to go in Jackson 3.0 however.

One challenge there is this: it is easy to add on/off kinds of things (YAMLGenerator.Feature).
However, with 3.0 it will be much easier to add backend-specific configuration via Builder-style construction. But to keep dependencies to SnakeYAML hidden, we'd probably need a new enumeration for choices as well.
That seems cleaner than adding logic to detect platform, although maybe that'd be fine as an alternative. But that would then require definition of some kind of universal default, for feature for "enable platform-specific linefeed" (if disabled, what is the linefeed then -> default).

@thauk-copperleaf
Copy link
Author

@cowtowncoder

Thanks for the quick reply!

I actually do not know what YAML specs says: I assume it probably allows multiple styles of linefeeds just like JSON and XML.

Correct. The YAML spec allows four different "generic line breaks", including CRLF for Windows and LF for Unix.

Whether platform defaults should be used or canonical defaults is an open question; since both are (I presume) legit I don't have strong opinion.

The behaviour I expect from the serializer is that it outputs textual data with line endings consistent with that platform it's running on, by default, but should allow overriding to a specific line ending. I think this is the least-surprising choice (between that and defaulting to LF), and indeed, is the design choice that's already present because of FasterXML/jackson-databind#585 so at least making that change would be internally consistent.

One challenge there is this: it is easy to add on/off kinds of things (YAMLGenerator.Feature)

Would you accept a PR to add a new enumeration value to YAMLGenerator.Format called USE_PLATFORM_LINE_BREAKS and adds the following to YAMLGenerator#buildDumperOptions?

        if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatFeatures)) {
            opt.setLineBreak(opt.LineBreak.getPlatformLineBreak());
        }

To not break existing clients, the default value of USE_PLATFORM_LINE_BREAKS would be set to false

@cowtowncoder cowtowncoder changed the title Serializing to YAML on Windows results in files with Unix-style line endings (LF) Add option to allow use of platform-linefeed (YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS) May 15, 2018
@cowtowncoder cowtowncoder added this to the 2.9.6 milestone May 15, 2018
cowtowncoder added a commit that referenced this issue May 15, 2018
@cowtowncoder cowtowncoder added 2.10 yaml Issue related to YAML format backend and removed 2.9 labels Oct 5, 2019
@cowtowncoder cowtowncoder removed the 2.11 label Sep 18, 2020
@cowtowncoder
Copy link
Member

Forgot to close, was included in 2.9.6.

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