forked from google/gson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#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>
- Loading branch information
1 parent
2243482
commit c26532f
Showing
10 changed files
with
371 additions
and
43 deletions.
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.