Skip to content

Commit ef26aa2

Browse files
committed
add new pretty formatting style
1 parent 1f9f496 commit ef26aa2

11 files changed

Lines changed: 87 additions & 20 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ subprojects {
4848
apply(plugin = "java")
4949

5050
group = "dev.mudkip"
51-
version = "0.2.0"
51+
version = "0.3.0"
5252
java.toolchain.languageVersion = JavaLanguageVersion.of(25)
5353

5454
repositories {

json/src/main/java/alpine/json/ArrayElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean equals(Object object) {
3939

4040
@Override
4141
public String toString() {
42-
return Json.write(this, Json.Formatting.PRETTY);
42+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
4343
}
4444

4545
@Override

json/src/main/java/alpine/json/BooleanElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public boolean equals(Object object) {
2525

2626
@Override
2727
public String toString() {
28-
return Json.write(this, Json.Formatting.PRETTY);
28+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
2929
}
3030

3131
public boolean value() {

json/src/main/java/alpine/json/Json.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,26 @@ public record Formatting(String indentation, String newLine, String comma, Strin
6363
*/
6464
public static final Formatting COMPACT = new Formatting(
6565
"",
66-
System.lineSeparator(),
66+
"\n",
6767
String.valueOf(COMMA),
6868
String.valueOf(COLON));
6969

7070
/**
71-
* Represents a beautified format ideal for human readability.
71+
* Represents a single-line beautified format with spaces between values.
72+
*/
73+
public static final Formatting INLINE_PRETTY = new Formatting(
74+
"",
75+
"\n",
76+
COMPACT.comma + SPACE,
77+
COMPACT.colon + SPACE);
78+
79+
/**
80+
* Represents a multi-line beautified format ideal for human readability.
7281
*/
7382
public static final Formatting PRETTY = new Formatting(
7483
String.valueOf(SPACE).repeat(4),
75-
System.lineSeparator(),
76-
COMPACT.comma + SPACE,
84+
"\n",
85+
String.valueOf(COMMA),
7786
COMPACT.colon + SPACE);
7887
}
7988
}

json/src/main/java/alpine/json/JsonWriter.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ String write(Element value, Json.Formatting formatting) {
1616
default -> 32;
1717
});
1818

19-
this.write(builder, value, formatting);
19+
this.write(builder, value, formatting, 0);
2020
return builder.toString();
2121
}
2222

23-
private void write(StringBuilder builder, Element value, Json.Formatting formatting) {
23+
private void write(StringBuilder builder, Element value, Json.Formatting formatting, int depth) {
2424
switch (value) {
2525
case NullElement _ -> builder.append(NULL);
2626
case BooleanElement element -> builder.append(element.value() ? TRUE : FALSE);
2727
case NumberElement element -> this.writeNumber(builder, element.value());
2828
case StringElement element -> this.writeString(builder, element.value());
29-
case ArrayElement element -> this.writeArray(builder, element, formatting);
30-
case ObjectElement element -> this.writeObject(builder, element, formatting);
29+
case ArrayElement element -> this.writeArray(builder, element, formatting, depth);
30+
case ObjectElement element -> this.writeObject(builder, element, formatting, depth);
3131
}
3232
}
3333

@@ -89,7 +89,16 @@ private void writeString(StringBuilder builder, String string) {
8989
builder.append(QUOTE);
9090
}
9191

92-
private void writeArray(StringBuilder builder, ArrayElement element, Json.Formatting formatting) {
92+
private void writeIndentation(StringBuilder builder, Json.Formatting formatting, int depth) {
93+
if (formatting.indentation().isEmpty()) {
94+
return;
95+
}
96+
97+
builder.append(formatting.newLine());
98+
builder.append(formatting.indentation().repeat(depth));
99+
}
100+
101+
private void writeArray(StringBuilder builder, ArrayElement element, Json.Formatting formatting, int depth) {
93102
builder.append(BEGIN_ARRAY);
94103
var firstElement = true;
95104

@@ -98,14 +107,19 @@ private void writeArray(StringBuilder builder, ArrayElement element, Json.Format
98107
builder.append(formatting.comma());
99108
}
100109

101-
this.write(builder, value, formatting);
110+
this.writeIndentation(builder, formatting, depth + 1);
111+
this.write(builder, value, formatting, depth + 1);
102112
firstElement = false;
103113
}
104114

115+
if (!firstElement) {
116+
this.writeIndentation(builder, formatting, depth);
117+
}
118+
105119
builder.append(END_ARRAY);
106120
}
107121

108-
private void writeObject(StringBuilder builder, ObjectElement element, Json.Formatting formatting) {
122+
private void writeObject(StringBuilder builder, ObjectElement element, Json.Formatting formatting, int depth) {
109123
builder.append(BEGIN_OBJECT);
110124
var firstElement = new boolean[] { true };
111125

@@ -116,11 +130,16 @@ private void writeObject(StringBuilder builder, ObjectElement element, Json.Form
116130
builder.append(formatting.comma());
117131
}
118132

133+
this.writeIndentation(builder, formatting, depth + 1);
119134
this.writeString(builder, key);
120135
builder.append(formatting.colon());
121-
this.write(builder, value, formatting);
136+
this.write(builder, value, formatting, depth + 1);
122137
});
123138

139+
if (!firstElement[0]) {
140+
writeIndent(builder, formatting, depth);
141+
}
142+
124143
builder.append(END_OBJECT);
125144
}
126145
}

json/src/main/java/alpine/json/NullElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ private NullElement() {
1616

1717
@Override
1818
public String toString() {
19-
return Json.write(this, Json.Formatting.PRETTY);
19+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
2020
}
2121
}

json/src/main/java/alpine/json/NumberElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public boolean equals(Object object) {
2525

2626
@Override
2727
public String toString() {
28-
return Json.write(this, Json.Formatting.PRETTY);
28+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
2929
}
3030

3131
public double value() {

json/src/main/java/alpine/json/ObjectElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public boolean equals(Object object) {
3838

3939
@Override
4040
public String toString() {
41-
return Json.write(this, Json.Formatting.PRETTY);
41+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
4242
}
4343

4444
public Stream<Map.Entry<String, Element>> stream() {

json/src/main/java/alpine/json/StringElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public boolean equals(Object object) {
3131

3232
@Override
3333
public String toString() {
34-
return Json.write(this, Json.Formatting.PRETTY);
34+
return Json.write(this, Json.Formatting.INLINE_PRETTY);
3535
}
3636

3737
public String value() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package alpine.json;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static alpine.json.Element.*;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
final class FormattingTest {
9+
private static final ObjectElement OBJECT = object()
10+
.set("a", "b")
11+
.set("c", "d")
12+
.set("nested", object().set("x", 1));
13+
14+
@Test
15+
void testCompact() {
16+
assertEquals(
17+
"{\"a\":\"b\",\"c\":\"d\",\"nested\":{\"x\":1}}",
18+
Json.write(OBJECT, Json.Formatting.COMPACT));
19+
}
20+
21+
@Test
22+
void testInlinePretty() {
23+
assertEquals(
24+
"{\"a\": \"b\", \"c\": \"d\", \"nested\": {\"x\": 1}}",
25+
Json.write(OBJECT, Json.Formatting.INLINE_PRETTY));
26+
}
27+
28+
@Test
29+
void testPretty() {
30+
assertEquals("""
31+
{
32+
"a": "b",
33+
"c": "d",
34+
"nested": {
35+
"x": 1
36+
}
37+
}""", Json.write(OBJECT, Json.Formatting.PRETTY));
38+
}
39+
}

0 commit comments

Comments
 (0)