-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fixes #776: Add settings for the kind of newline to use #2231
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for this well written pull request!
Personally your reasoning regarding #776 makes sense to me. However, I am not a direct member of this project, so I won't be making the final decision.
My review comments are mostly intended as suggestions; I hope they are helpful. Also as warning (in case you have not encountered that yet): GitHub has the bad habbit to collapse unresolved review comments as part of its "... hidden conversations".
I am not sure if the current NewlineStyle
constants are a good idea. The advantage is that it makes it easy to select a style for an OS, but on the other hand this binds Gson to something external: Which OS uses which newline. This could be a bit problematic if an OS changes its newline (quite unlikely though) or if a new OS becomes popular or one of these becomes obsolete (also rather unlikely). Maybe just using LF
, CRLF
, CR
and CURRENT_OS
would be better, assuming that users which want to use this feature know which newline Linux or Windows uses. What do you think?
(Also could you please edit the description of this pull request to say "Fixes #..." / "Resolves #..." instead of "Addresses #..." so that GitHub understands that this PR closes the issue.)
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Implemented everything, but I still need to check that GitHub didn't hide anything from me :-) And I did not resolve any conversation because I don't know what the convention is for this project. I've contributed to various projects with different conventions. Thanks for the review, and please don't hesitate to push back on the one or two items that I didn't do, |
I think there are some good points here, but unfortunately I've seen a lot of developers that A few alternative ideas:
Of course, each one has pros and cons :-( I kind of like the last one though... I think that indeed an OS changing the convention is a very-very unlikely event. |
Done with he implementation of the second round of review. Unfortunately there are still a couple of items that I couldn't get myself to implement, as I still have some doubts. For example CRLF vs WINDOWS. Thanks, |
I've slept a bit on this over night ;-) What about this: I add the standard "glue" for an enum with a string value and I define all the values in the ECMAScript spec:
With comments specifying the OS using each (if any). And the string values eliminates the need of a switch in |
UGH :-( From https://en.wikipedia.org/wiki/Newline#Unicode And the fact that C transparently maps "\n" to whatever the OS uses as newline when doing IO. So "\n" on Windows actually means CR+LF, unless you open the file in binary mode. Which is something I know, as I've spent a lot of time on Windows. But a lot of people don't. From readying the wiki article it looks like thinks are messy, with JSON using different rules than ECMAScript, "\n" not always == CR in some programming languages, and what not. I also did an (informal) survey with some colleagues. |
I think it is all done form my side. I've changed the enum to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the great amount of work and thoughts you have put into this! I have only a few minor comments but I think this looks very good otherwise.
Regarding the NewlineStyle
constants, I think your current solution with LF
, CR
, ... is good, but I am not sure either what the "best" solution would be.
Though as mentioned before, I am not a direct member of this project. So @eamonnmcmanus, what you think about this pull request?
Done, all feedback implemented (2022/11/03) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Only a few minor things which came up now, but feel free to wait until Éamonn has given feedback on this pull request as a whole as well.
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Thanks! |
Gentle ping? Do you think that adding Éamonn as reviewer might help? Thank you, |
Hi, just back from vacation. :-) My initial reactions:
public class FormattingStyle {
private final String newline;
private FormattingStyle(String newline) {
this.newline = newline;
}
public static final FormattingStyle DEFAULT = new FormattingStyle("\n");
public FormattingStyle withNewline(String newline) {
return new FormattingStyle(newline);
}
} which would evolve in the expected way if we add other parameters like indent. I don't think it is really necessary to have constants for the kinds of newline style. We can certainly document the two prevailing styles, and perhaps require that the I'll also note, playing Devil's Advocate a bit, that it's pretty easy to do |
That's true, and it is something I did for years (and it annoyed me for years :-). So, there are workarounds, but at the core this makes the people who need Windows EOL feel like second class citizens. When the JSON spec (and ECMAScript spec) does not specify a certain EOL as preferred, they accept all the valid combinations. In fact there is an effort to update the spec to also allow for Anyway... Maybe move this discussion into an issue, so that you can review the direction before I implement something debatable? I would probably go with a more "fluent" Thank you, |
Copied the last set of comments (about Hoping we can outline there what the preferred way would be, and I can follow up with a new PR or update this one. Your call. Thanks, |
Yes, for a nontrivial API change like this it's probably better to agree on the API in an issue first before committing to an implementation with tests and everything. |
Updated to |
Just to make sure there is no misunderstanding; I hope no one was waiting for my review. For me the initial implementation was fine, however I did not think about a more general implementation covering more than just the new line style, as done now with the @eamonnmcmanus, are these changes similar to what you had in mind? (though hopefully this also has time until next year) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to have lost track of this. It looks great! Just some small comments.
import java.util.Objects; | ||
|
||
/** | ||
* An enumeration that defines the kind of newline to use for serialization. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment needs to be rewritten slightly? It isn't an enumeration, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
return this.newline; | ||
} | ||
|
||
public String getIndent() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a javadoc comment here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
try { | ||
// TBD if we want to accept \u2028 and \u2029. For now we don't. | ||
FormattingStyle.DEFAULT.withNewline("\u2028"); | ||
fail("Gson should not accept anything but \r and \r for newline"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these messages should say
...anything but \\r and \\n...
So double backslash, and the second one should be n rather than r.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Don't worry, I know what is like :-) I've added comments to all public methods, generated the javadoc and checked what they look like, checked the links, etc. Thank you, |
Thanks for doing this! |
Thank you very much for taking it! |
…#2231) * Add settings for kind of newline to use * Fix amp in javadoc * Fixing link in javadoc * Doc: use JSON instead of Json Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * PR Feedback: Objects.requireNonNull Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * PR Feedback: $next-version$, don't hardcode Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * s/testNewlineLF/testNewlineLf/ Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * Implement PR feedback * Round two of review * Restore copyright year, no reason to update * Rename OS named enum values to CR and LF * Add javadoc to NewlineStyle.getValue() * Implement PR feedback, round 2 * Fix typo Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * No need for line break Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * Shorter, cleaner doc Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * Using a FormattingStyle for pretty print * Fix Junit4 and Truth after merge from master * Implement review feedback * Double backslash in message --------- Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Addresses #776
I don't think that
setFormattingStyles(String style)
(as proposed) should affect the newline, that would be orthogonal.That idea has no explanation, but I'm thinking that it would be something similar to the formatting styles in programming languages (where to break and how to indent, for example K&R vs Linux kernel vs BSD KNF, etc.)
If that is the idea then the kind of newline to use is not related to the style.
The "dangling new line at the end of the JSON" would indeed be part of the style.