forked from diffplug/spotless
-
-
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.
Currenly, if consumers of Spotless want to format their JSON files, they need to do this by adding `prettier`, which increases the dependencies a project needs. To simplify things for consumers, as documented in diffplug#850, we can provide a JVM-based JSON formatter, using `org.json`'s JSON formatting. To follow how we've done this with other formatters, we can retrieve `org.json`'s JAR at runtime, and retrieve the classes via Reflection, instead of adding this as a `lib-extra` project, with an explicit dependency. To validate this fully, we want a few straightforward JSON files to validate before/after, but we can also use [a sample Cucumber JSON report] as a much more complex example of what happens, which we've removed any `data` objects from its source, so the files are smaller. We've added an `equality` test, but it doesn't really test too much right now as there's nothing stored in the state. [a sample Cucumber JSON report]: https://github.com/damianszczepanik/cucumber-reporting/raw/master/src/test/resources/json/sample.json
- Loading branch information
1 parent
59173b0
commit f787f9e
Showing
21 changed files
with
1,449 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
lib/src/main/java/com/diffplug/spotless/json/JsonFormatterStep.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 2021 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; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
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 final class JsonFormatterStep { | ||
private static final String MAVEN_COORDINATE = "org.json:json:"; | ||
private static final String DEFAULT_VERSION = "20210307"; | ||
private static final int INDENT = 4; | ||
|
||
public static FormatterStep create(Provisioner provisioner) { | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("json", () -> new State(provisioner), State::toFormatter); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final JarState jarState; | ||
|
||
private State(Provisioner provisioner) throws IOException { | ||
this.jarState = JarState.from(MAVEN_COORDINATE + DEFAULT_VERSION, provisioner); | ||
} | ||
|
||
FormatterFunc toFormatter() { | ||
return s -> { | ||
ClassLoader classLoader = jarState.getClassLoader(); | ||
String prettyPrinted = null; | ||
if (s.isEmpty()) { | ||
prettyPrinted = s; | ||
} | ||
if (s.startsWith("{")) { | ||
Class<?> jsonObject = classLoader.loadClass("org.json.JSONObject"); | ||
Class<?>[] type = new Class[]{String.class}; | ||
Constructor<?> constructor = jsonObject.getConstructor(type); | ||
Method toString = jsonObject.getMethod("toString", int.class); | ||
try { | ||
Object parsed = constructor.newInstance(s); | ||
prettyPrinted = toString.invoke(parsed, INDENT) + "\n"; | ||
} catch (Exception e) { | ||
// ignore if we cannot convert to JSON string | ||
} | ||
} | ||
if (s.startsWith("[")) { | ||
Class<?> jsonArray = classLoader.loadClass("org.json.JSONArray"); | ||
Class<?>[] type = new Class[]{String.class}; | ||
Constructor<?> constructor = jsonArray.getConstructor(type); | ||
Method toString = jsonArray.getMethod("toString", int.class); | ||
try { | ||
Object parsed = constructor.newInstance(s); | ||
prettyPrinted = toString.invoke(parsed, INDENT) + "\n"; | ||
} catch (Exception e) { | ||
// ignore if we cannot convert to JSON string | ||
} | ||
} | ||
|
||
if (prettyPrinted == null) { | ||
throw new AssertionError("Invalid JSON file provided"); | ||
} | ||
|
||
return prettyPrinted; | ||
}; | ||
} | ||
} | ||
|
||
private JsonFormatterStep() { | ||
// cannot be directly instantiated | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
lib/src/main/java/com/diffplug/spotless/json/package-info.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,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@ReturnValuesAreNonnullByDefault | ||
package com.diffplug.spotless.extra.json.java; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault; |
Oops, something went wrong.