-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Gson integration to compile-only source set (#1510)
- Loading branch information
Showing
14 changed files
with
197 additions
and
384 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
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
92 changes: 92 additions & 0 deletions
92
lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java
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,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 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 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.