-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/source-set
- Loading branch information
Showing
38 changed files
with
2,258 additions
and
132 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
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
54 changes: 54 additions & 0 deletions
54
lib/src/main/java/com/diffplug/spotless/json/gson/GsonBuilderWrapper.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,54 @@ | ||
/* | ||
* Copyright 2022 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.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
import com.diffplug.spotless.JarState; | ||
|
||
class GsonBuilderWrapper extends GsonWrapperBase { | ||
|
||
private final Constructor<?> constructor; | ||
private final Method serializeNullsMethod; | ||
private final Method disableHtmlEscapingMethod; | ||
private final Method createMethod; | ||
|
||
GsonBuilderWrapper(JarState jarState) { | ||
Class<?> clazz = loadClass(jarState.getClassLoader(), "com.google.gson.GsonBuilder"); | ||
this.constructor = getConstructor(clazz); | ||
this.serializeNullsMethod = getMethod(clazz, "serializeNulls"); | ||
this.disableHtmlEscapingMethod = getMethod(clazz, "disableHtmlEscaping"); | ||
this.createMethod = getMethod(clazz, "create"); | ||
} | ||
|
||
Object createGsonBuilder() { | ||
return newInstance(constructor); | ||
} | ||
|
||
Object serializeNulls(Object gsonBuilder) { | ||
return invoke(serializeNullsMethod, gsonBuilder); | ||
} | ||
|
||
Object disableHtmlEscaping(Object gsonBuilder) { | ||
return invoke(disableHtmlEscapingMethod, gsonBuilder); | ||
} | ||
|
||
Object create(Object gsonBuilder) { | ||
return invoke(createMethod, gsonBuilder); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.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,106 @@ | ||
/* | ||
* Copyright 2022 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.IOException; | ||
import java.io.Serializable; | ||
import java.io.StringWriter; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class GsonStep { | ||
private static final String MAVEN_COORDINATES = "com.google.code.gson:gson"; | ||
|
||
public static FormatterStep create(int indentSpaces, boolean sortByKeys, boolean escapeHtml, String version, Provisioner provisioner) { | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("gson", () -> new State(indentSpaces, sortByKeys, escapeHtml, version, provisioner), State::toFormatter); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = -1493479043249379485L; | ||
|
||
private final int indentSpaces; | ||
private final boolean sortByKeys; | ||
private final boolean escapeHtml; | ||
private final JarState jarState; | ||
|
||
private State(int indentSpaces, boolean sortByKeys, boolean escapeHtml, String version, Provisioner provisioner) throws IOException { | ||
this.indentSpaces = indentSpaces; | ||
this.sortByKeys = sortByKeys; | ||
this.escapeHtml = escapeHtml; | ||
this.jarState = JarState.from(MAVEN_COORDINATES + ":" + version, provisioner); | ||
} | ||
|
||
FormatterFunc toFormatter() { | ||
JsonWriterWrapper jsonWriterWrapper = new JsonWriterWrapper(jarState); | ||
JsonElementWrapper jsonElementWrapper = new JsonElementWrapper(jarState); | ||
JsonObjectWrapper jsonObjectWrapper = new JsonObjectWrapper(jarState, jsonElementWrapper); | ||
GsonBuilderWrapper gsonBuilderWrapper = new GsonBuilderWrapper(jarState); | ||
GsonWrapper gsonWrapper = new GsonWrapper(jarState, jsonElementWrapper, jsonWriterWrapper); | ||
|
||
Object gsonBuilder = gsonBuilderWrapper.serializeNulls(gsonBuilderWrapper.createGsonBuilder()); | ||
if (!escapeHtml) { | ||
gsonBuilder = gsonBuilderWrapper.disableHtmlEscaping(gsonBuilder); | ||
} | ||
Object gson = gsonBuilderWrapper.create(gsonBuilder); | ||
|
||
return inputString -> { | ||
String result; | ||
if (inputString.isEmpty()) { | ||
result = ""; | ||
} else { | ||
Object jsonElement = gsonWrapper.fromJson(gson, inputString, jsonElementWrapper.getWrappedClass()); | ||
if (jsonElement == null) { | ||
throw new AssertionError(GsonWrapperBase.FAILED_TO_PARSE_ERROR_MESSAGE); | ||
} | ||
if (sortByKeys && jsonElementWrapper.isJsonObject(jsonElement)) { | ||
jsonElement = sortByKeys(jsonObjectWrapper, jsonElementWrapper, jsonElement); | ||
} | ||
try (StringWriter stringWriter = new StringWriter()) { | ||
Object jsonWriter = jsonWriterWrapper.createJsonWriter(stringWriter); | ||
jsonWriterWrapper.setIndent(jsonWriter, generateIndent(indentSpaces)); | ||
gsonWrapper.toJson(gson, jsonElement, jsonWriter); | ||
result = stringWriter + "\n"; | ||
} | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
private Object sortByKeys(JsonObjectWrapper jsonObjectWrapper, JsonElementWrapper jsonElementWrapper, Object jsonObject) { | ||
Object result = jsonObjectWrapper.createJsonObject(); | ||
jsonObjectWrapper.keySet(jsonObject).stream().sorted() | ||
.forEach(key -> { | ||
Object element = jsonObjectWrapper.get(jsonObject, key); | ||
if (jsonElementWrapper.isJsonObject(element)) { | ||
element = sortByKeys(jsonObjectWrapper, jsonElementWrapper, element); | ||
} | ||
jsonObjectWrapper.add(result, key, element); | ||
}); | ||
return result; | ||
} | ||
|
||
private String generateIndent(int indentSpaces) { | ||
return String.join("", Collections.nCopies(indentSpaces, " ")); | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
lib/src/main/java/com/diffplug/spotless/json/gson/GsonWrapper.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,41 @@ | ||
/* | ||
* Copyright 2022 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.lang.reflect.Method; | ||
|
||
import com.diffplug.spotless.JarState; | ||
|
||
class GsonWrapper extends GsonWrapperBase { | ||
|
||
private final Method fromJsonMethod; | ||
private final Method toJsonMethod; | ||
|
||
GsonWrapper(JarState jarState, JsonElementWrapper jsonElementWrapper, JsonWriterWrapper jsonWriterWrapper) { | ||
Class<?> clazz = loadClass(jarState.getClassLoader(), "com.google.gson.Gson"); | ||
this.fromJsonMethod = getMethod(clazz, "fromJson", String.class, Class.class); | ||
this.toJsonMethod = getMethod(clazz, "toJson", jsonElementWrapper.getWrappedClass(), jsonWriterWrapper.getWrappedClass()); | ||
} | ||
|
||
Object fromJson(Object gson, String json, Class<?> type) { | ||
return invoke(fromJsonMethod, gson, json, type); | ||
} | ||
|
||
void toJson(Object gson, Object jsonElement, Object jsonWriter) { | ||
invoke(toJsonMethod, gson, jsonElement, jsonWriter); | ||
} | ||
|
||
} |
Oops, something went wrong.