Skip to content

Commit 1af697d

Browse files
Fix JSON pretty print (#11)
* Add (failing) pretty json print test * Make FileWrapper respect the pretty printing setting --------- Co-authored-by: Lilly Tempest <46890129+rainbowdashlabs@users.noreply.github.com>
1 parent f2cebc2 commit 1af697d

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/main/java/dev/chojo/ocular/components/FileWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public record FileWrapper<T>(Format<?, ?> format, T file) {
2424
* @throws JsonProcessingException if an error occurs during serialization
2525
*/
2626
public String asString() throws JsonProcessingException {
27+
if (format.format().enablePrettyPrint()) {
28+
return format.writer().writerWithDefaultPrettyPrinter().writeValueAsString(file);
29+
}
30+
2731
return format.writer().writeValueAsString(file);
2832
}
2933
}

src/main/java/dev/chojo/ocular/dataformats/DataFormat.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @param <B> the type of MapperBuilder used to configure the associated ObjectMapper
2323
*/
2424
public interface DataFormat<M extends ObjectMapper, B extends MapperBuilder<M, B>> extends Configurator<M, B> {
25+
2526
/**
2627
* Creates and returns a new instance of the mapper builder associated with the data format.
2728
* This method is used to initialize the builder for configuring the ObjectMapper for
@@ -40,6 +41,13 @@ public interface DataFormat<M extends ObjectMapper, B extends MapperBuilder<M, B
4041
*/
4142
String type();
4243

44+
/**
45+
* Used to determine whether the default pretty printer should be used explicitly
46+
*/
47+
default boolean enablePrettyPrint() {
48+
return false;
49+
}
50+
4351
/**
4452
* Checks whether the specified key matches the type or any of the type aliases of the current data format.
4553
*

src/main/java/dev/chojo/ocular/dataformats/JsonDataFormat.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public void assertInstalled() {
3333
// json should be always installed
3434
}
3535

36+
@Override
37+
public boolean enablePrettyPrint() {
38+
return prettyPrint;
39+
}
40+
3641
@Override
3742
public void configure(JsonMapper mapper) {
3843
if (prettyPrint) {

src/test/java/dev/chojo/ocular/ConfigurationsTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ void migrate() {
138138
Assertions.assertTrue(Files.exists(BASE.resolve(YML.path())));
139139
}
140140

141+
@Test
142+
void checkPrettyJsonFormat() throws IOException {
143+
Configurations<MyClass> conf = Configurations.builder(JSON, new JsonDataFormat(true))
144+
.setBase(BASE)
145+
.build();
146+
147+
conf.main();
148+
conf.save();
149+
150+
Assertions.assertTrue(BASE.resolve(JSON.path()).toFile().exists());
151+
String json = Files.readString(BASE.resolve(JSON.path()));
152+
153+
// The reason we use .lines() here is because of OS specific differences in line separators (LF & CRLF)
154+
Assertions.assertLinesMatch("""
155+
{
156+
"name" : "Lilly",
157+
"age" : 20
158+
}
159+
""".trim().lines(), json.lines());
160+
}
161+
162+
@Test
163+
void checkNonPrettyJsonFormat() throws IOException {
164+
Configurations<MyClass> conf = Configurations.builder(JSON, new JsonDataFormat(false))
165+
.setBase(BASE)
166+
.build();
167+
168+
conf.main();
169+
conf.save();
170+
171+
Assertions.assertTrue(BASE.resolve(JSON.path()).toFile().exists());
172+
String json = Files.readString(BASE.resolve(JSON.path()));
173+
174+
Assertions.assertEquals("{\"name\":\"Lilly\",\"age\":20}", json);
175+
}
176+
141177
// void example() {
142178
// Key<MyClass> mainConfig = Key.builder(Path.of("config.json"), MyClass::new).build();
143179
// Configurations<MyClass> conf = Configurations.builder(
@@ -154,7 +190,7 @@ void migrate() {
154190
// .build();
155191
// }
156192

157-
void exampleJacksonBukkit(){
193+
void exampleJacksonBukkit() {
158194
Key<MyClass> mainConfig = Key.builder(Path.of("config.yml"), MyClass::new).build();
159195
Configurations.builder(mainConfig, new YamlDataFormat())
160196
.withClassLoader(this.getClass().getClassLoader()) // For minecraft its important to pass the classloader

0 commit comments

Comments
 (0)