-
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 drop-ktlint-3x
- Loading branch information
Showing
88 changed files
with
2,606 additions
and
687 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
* text eol=lf | ||
*.bat eol=crlf | ||
*.png binary | ||
*.jar binary |
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
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
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, " ")); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.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,88 @@ | ||
/* | ||
* Copyright 2021-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.json; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.PrettyPrinter; | ||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.json.JacksonConfig; | ||
|
||
/** | ||
* A {@link FormatterFunc} based on Jackson library | ||
*/ | ||
// https://github.com/FasterXML/jackson-dataformats-text/issues/372 | ||
public abstract class AJacksonFormatterFunc implements FormatterFunc { | ||
private JacksonConfig jacksonConfig; | ||
|
||
public AJacksonFormatterFunc(JacksonConfig jacksonConfig) { | ||
this.jacksonConfig = jacksonConfig; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
ObjectMapper objectMapper = makeObjectMapper(); | ||
|
||
return format(objectMapper, input); | ||
} | ||
|
||
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException { | ||
try { | ||
// ObjectNode is not compatible with SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS | ||
Map objectNode = objectMapper.readValue(input, Map.class); | ||
String output = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode); | ||
|
||
return output; | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException("Unable to format. input='" + input + "'", e); | ||
} | ||
} | ||
|
||
/** | ||
* @return a {@link JsonFactory}. May be overridden to handle alternative formats. | ||
* @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a> | ||
*/ | ||
protected abstract JsonFactory makeJsonFactory(); | ||
|
||
protected ObjectMapper makeObjectMapper() { | ||
JsonFactory jsonFactory = makeJsonFactory(); | ||
ObjectMapper objectMapper = new ObjectMapper(jsonFactory); | ||
|
||
objectMapper.setDefaultPrettyPrinter(makePrettyPrinter()); | ||
|
||
// Configure the ObjectMapper | ||
// https://github.com/FasterXML/jackson-databind#commonly-used-features | ||
jacksonConfig.getFeatureToToggle().forEach((rawFeature, toggle) -> { | ||
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection | ||
SerializationFeature feature = SerializationFeature.valueOf(rawFeature); | ||
|
||
objectMapper.configure(feature, toggle); | ||
}); | ||
|
||
return objectMapper; | ||
} | ||
|
||
protected PrettyPrinter makePrettyPrinter() { | ||
return new DefaultPrettyPrinter(); | ||
} | ||
} |
Oops, something went wrong.