Skip to content

Commit d25658c

Browse files
committed
Make JsonSimpleStep round-trippable
1 parent a22f07a commit d25658c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.json;
1717

18-
import java.io.IOException;
1918
import java.io.Serializable;
2019
import java.lang.reflect.Constructor;
2120
import java.lang.reflect.InvocationTargetException;
@@ -26,17 +25,35 @@
2625
import com.diffplug.spotless.FormatterStep;
2726
import com.diffplug.spotless.JarState;
2827
import com.diffplug.spotless.Provisioner;
28+
import com.diffplug.spotless.RoundedStep;
2929

3030
/**
3131
* Simple JSON formatter which reformats the file according to the org.json library's default pretty-printing, but has no ability to customise more than the indentation size.
3232
*/
33-
public final class JsonSimpleStep {
33+
public final class JsonSimpleStep implements RoundedStep {
34+
private static final long serialVersionUID = 1L;
3435
private static final String MAVEN_COORDINATE = "org.json:json:";
3536
private static final String DEFAULT_VERSION = "20210307";
37+
public static final String NAME = "jsonSimple";
38+
39+
private final JarState.Promised jarState;
40+
private final int indentSpaces;
41+
42+
private JsonSimpleStep(JarState.Promised jarState, int indentSpaces) {
43+
this.indentSpaces = indentSpaces;
44+
this.jarState = jarState;
45+
}
3646

3747
public static FormatterStep create(int indent, Provisioner provisioner) {
3848
Objects.requireNonNull(provisioner, "provisioner cannot be null");
39-
return FormatterStep.createLazy("json", () -> new State(indent, provisioner), State::toFormatter);
49+
return FormatterStep.create(NAME,
50+
new JsonSimpleStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + DEFAULT_VERSION, provisioner)), indent),
51+
JsonSimpleStep::equalityState,
52+
State::toFormatter);
53+
}
54+
55+
private State equalityState() {
56+
return new State(jarState.get(), indentSpaces);
4057
}
4158

4259
private static final class State implements Serializable {
@@ -45,9 +62,9 @@ private static final class State implements Serializable {
4562
private final int indentSpaces;
4663
private final JarState jarState;
4764

48-
private State(int indent, Provisioner provisioner) throws IOException {
65+
State(JarState jarState, int indent) {
66+
this.jarState = jarState;
4967
this.indentSpaces = indent;
50-
this.jarState = JarState.from(MAVEN_COORDINATE + DEFAULT_VERSION, provisioner);
5168
}
5269

5370
FormatterFunc toFormatter() {
@@ -94,8 +111,4 @@ private String format(Constructor<?> constructor, Method toString, String input)
94111
}
95112
}
96113
}
97-
98-
private JsonSimpleStep() {
99-
// cannot be directly instantiated
100-
}
101114
}

testlib/src/main/java/com/diffplug/spotless/StepHarnessBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected StepHarnessBase(Formatter formatter) {
5151
supportsRoundTrip = true;
5252
} else if (Set.of("black", "buf", "clang", "ktlint", "ktfmt", "scalafmt", "palantir-java-format", "google-java-format",
5353
"removeUnusedImports", "cleanthat", "No line break between type annotation and type",
54-
"gson", "jacksonJson", "apply-json-patch",
54+
"gson", "jacksonJson", "apply-json-patch", "jsonSimple",
5555
"importOrder", "Remove unnecessary semicolons").contains(onlyStepName)) {
5656
supportsRoundTrip = true;
5757
}

0 commit comments

Comments
 (0)