-
Notifications
You must be signed in to change notification settings - Fork 472
Convert Gson integration to compile-only source set #1510
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
Merged
nedtwigg
merged 6 commits into
diffplug:main
from
abelk2:feat/gson-custom-compile-sourceset
Jan 24, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10f04de
move gson formatter to custom compile sourceset
abelk2 0a86bad
apply format
abelk2 eea5d5c
fix tests
abelk2 f3481be
apply format
abelk2 85d727e
update CHANGES.md
abelk2 92b3cf8
keep old signature of GsonStep.create() and restore GsonConfig classname
abelk2 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 hidden or 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 hidden or 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
92 changes: 92 additions & 0 deletions
92
lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java
This file contains hidden or 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,92 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* 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.diffplug.spotless.glue.gson; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.Collections; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.ThrowingEx; | ||
import com.diffplug.spotless.json.gson.GsonConfig; | ||
|
||
public class GsonFormatterFunc implements FormatterFunc { | ||
|
||
private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to format JSON"; | ||
|
||
private final Gson gson; | ||
private final GsonConfig gsonConfig; | ||
private final String generatedIndent; | ||
|
||
public GsonFormatterFunc(GsonConfig gsonConfig) { | ||
GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls(); | ||
if (!gsonConfig.isEscapeHtml()) { | ||
gsonBuilder = gsonBuilder.disableHtmlEscaping(); | ||
} | ||
this.gson = gsonBuilder.create(); | ||
this.gsonConfig = gsonConfig; | ||
this.generatedIndent = generateIndent(gsonConfig.getIndentSpaces()); | ||
} | ||
|
||
@Override | ||
public String apply(String inputString) { | ||
String result; | ||
if (inputString.isEmpty()) { | ||
result = ""; | ||
} else { | ||
JsonElement jsonElement = gson.fromJson(inputString, JsonElement.class); | ||
if (jsonElement == null) { | ||
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE); | ||
} | ||
if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) { | ||
jsonElement = sortByKeys(jsonElement.getAsJsonObject()); | ||
} | ||
try (StringWriter stringWriter = new StringWriter()) { | ||
JsonWriter jsonWriter = new JsonWriter(stringWriter); | ||
jsonWriter.setIndent(this.generatedIndent); | ||
gson.toJson(jsonElement, jsonWriter); | ||
result = stringWriter + "\n"; | ||
} catch (IOException ioException) { | ||
throw ThrowingEx.asRuntime(ioException); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private JsonElement sortByKeys(JsonObject jsonObject) { | ||
JsonObject result = new JsonObject(); | ||
jsonObject.keySet().stream().sorted() | ||
.forEach(key -> { | ||
JsonElement element = jsonObject.get(key); | ||
if (element.isJsonObject()) { | ||
element = sortByKeys(element.getAsJsonObject()); | ||
} | ||
result.add(key, element); | ||
}); | ||
return result; | ||
} | ||
|
||
private String generateIndent(int indentSpaces) { | ||
return String.join("", Collections.nCopies(indentSpaces, " ")); | ||
} | ||
|
||
} |
54 changes: 0 additions & 54 deletions
54
lib/src/main/java/com/diffplug/spotless/json/gson/GsonBuilderWrapper.java
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
lib/src/main/java/com/diffplug/spotless/json/gson/GsonConfig.java
This file contains hidden or 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,66 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* 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.diffplug.spotless.json.gson; | ||
|
||
import java.io.Serializable; | ||
|
||
public class GsonConfig implements Serializable { | ||
private static final long serialVersionUID = 6039715618937332633L; | ||
|
||
private boolean sortByKeys; | ||
private boolean escapeHtml; | ||
private int indentSpaces; | ||
private String version; | ||
|
||
public GsonConfig(boolean sortByKeys, boolean escapeHtml, int indentSpaces, String version) { | ||
this.sortByKeys = sortByKeys; | ||
this.escapeHtml = escapeHtml; | ||
this.indentSpaces = indentSpaces; | ||
this.version = version; | ||
} | ||
|
||
public boolean isSortByKeys() { | ||
return sortByKeys; | ||
} | ||
|
||
public void setSortByKeys(boolean sortByKeys) { | ||
this.sortByKeys = sortByKeys; | ||
} | ||
|
||
public boolean isEscapeHtml() { | ||
return escapeHtml; | ||
} | ||
|
||
public void setEscapeHtml(boolean escapeHtml) { | ||
this.escapeHtml = escapeHtml; | ||
} | ||
|
||
public int getIndentSpaces() { | ||
return indentSpaces; | ||
} | ||
|
||
public void setIndentSpaces(int indentSpaces) { | ||
this.indentSpaces = indentSpaces; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(String version) { | ||
this.version = version; | ||
} | ||
} |
This file contains hidden or 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
41 changes: 0 additions & 41 deletions
41
lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapper.java
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.