-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7bdd27b
Add settings for kind of newline to use
mihnita 0f84cdf
Fix amp in javadoc
mihnita bb31498
Fixing link in javadoc
mihnita b1016f1
Doc: use JSON instead of Json
mihnita a819811
PR Feedback: Objects.requireNonNull
mihnita 35b1bd2
PR Feedback: $next-version$, don't hardcode
mihnita 750745b
s/testNewlineLF/testNewlineLf/
mihnita 314c59c
Implement PR feedback
mihnita dbeb6f8
Round two of review
mihnita db6a203
Restore copyright year, no reason to update
mihnita bf893b7
Rename OS named enum values to CR and LF
mihnita fb703a4
Add javadoc to NewlineStyle.getValue()
mihnita b72d0fc
Implement PR feedback, round 2
mihnita 580f1fc
Fix typo
mihnita 8c8cbca
No need for line break
mihnita 1db3b05
Shorter, cleaner doc
mihnita 3016279
Using a FormattingStyle for pretty print
mihnita a9cfb61
Merge master
mihnita bd8616d
Fix Junit4 and Truth after merge from master
mihnita 4c2c726
Implement review feedback
mihnita e1292eb
Double backslash in message
mihnita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (C) 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gson; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* A class used to control what the serialization looks like. | ||
* | ||
* <p>It currently defines the kind of newline to use, and the indent, but | ||
* might add more in the future.</p> | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Newline">Wikipedia Newline article</a> | ||
* | ||
* @since $next-version$ | ||
*/ | ||
public class FormattingStyle { | ||
private final String newline; | ||
private final String indent; | ||
|
||
static public final FormattingStyle DEFAULT = | ||
new FormattingStyle("\n", " "); | ||
|
||
private FormattingStyle(String newline, String indent) { | ||
Objects.requireNonNull(newline, "newline == null"); | ||
Objects.requireNonNull(indent, "indent == null"); | ||
if (!newline.matches("[\r\n]*")) { | ||
throw new IllegalArgumentException( | ||
"Only combinations of \\n and \\r are allowed in newline."); | ||
} | ||
if (!indent.matches("[ \t]*")) { | ||
throw new IllegalArgumentException( | ||
"Only combinations of spaces and tabs allowed in indent."); | ||
} | ||
this.newline = newline; | ||
this.indent = indent; | ||
} | ||
|
||
/** | ||
* Creates a {@link FormattingStyle} with the specified newline setting. | ||
* | ||
* <p>It can be used to accommodate certain OS convention, for example | ||
* hardcode {@code "\r"} for Linux and macos, {@code "\r\n"} for Windows, or | ||
* call {@link java.lang.System#lineSeparator()} to match the current OS.</p> | ||
* | ||
* <p>Only combinations of {@code \n} and {@code \r} are allowed.</p> | ||
* | ||
* @param newline the string value that will be used as newline. | ||
* @return a newly created {@link FormattingStyle} | ||
*/ | ||
public FormattingStyle withNewline(String newline) { | ||
return new FormattingStyle(newline, this.indent); | ||
} | ||
|
||
/** | ||
* Creates a {@link FormattingStyle} with the specified indent string. | ||
* | ||
* <p>Only combinations of spaces and tabs allowed in indent.</p> | ||
* | ||
* @param indent the string value that will be used as indent. | ||
* @return a newly created {@link FormattingStyle} | ||
*/ | ||
public FormattingStyle withIndent(String indent) { | ||
return new FormattingStyle(this.newline, indent); | ||
} | ||
|
||
/** | ||
* The string value that will be used as a newline. | ||
* | ||
* @return the newline value. | ||
*/ | ||
public String getNewline() { | ||
return this.newline; | ||
} | ||
|
||
/** | ||
* The string value that will be used as indent. | ||
* | ||
* @return the indent value. | ||
*/ | ||
public String getIndent() { | ||
return this.indent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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