Skip to content

Commit df78103

Browse files
authored
fix nested pretty (#459)
1 parent a2dd4c9 commit df78103

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.example.customer;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.time.Instant;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.avaje.jsonb.Json;
11+
import io.avaje.jsonb.Jsonb;
12+
13+
public class PrettyNestedTest {
14+
@Json
15+
public record DataContainer(String id, Instant createdAt, List<App> apps) {
16+
public record App(
17+
String id, Instant createdAt, Instant updatedAt, String name, List<String> roles) {}
18+
}
19+
20+
@Test
21+
void testNested() {
22+
final String pretty =
23+
"""
24+
{
25+
"id": "f11177d2-ec63-3995-bb4a-c628e0d782df",
26+
"createdAt": "1970-01-01T00:00:01Z",
27+
"apps": [
28+
{
29+
"id": "ea4959eb-64a1-309b-a580-d950964f3843",
30+
"createdAt": "1970-01-01T00:00:01Z",
31+
"updatedAt": "1970-01-01T00:00:01Z",
32+
"name": "Name",
33+
"roles": [
34+
"admin"
35+
]
36+
},
37+
{
38+
"id": "ea4959eb-64a1-309b-a580-d950964f3843",
39+
"createdAt": "1970-01-01T00:00:01Z",
40+
"updatedAt": "1970-01-01T00:00:01Z",
41+
"name": "Name",
42+
"roles": [
43+
"admin"
44+
]
45+
}
46+
]
47+
}""";
48+
49+
var type = Jsonb.instance().type(DataContainer.class);
50+
String jsonPretty = type.toJsonPretty(type.fromJson(pretty));
51+
assertThat(jsonPretty).isEqualTo(pretty);
52+
}
53+
}

json-core/src/main/java/io/avaje/json/stream/core/JGenerator.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class JGenerator implements JsonGenerator {
6464
private int position;
6565
private boolean pretty;
6666
private int depth;
67+
private int arrayDepth;
68+
private int objectDepth;
6769
private final Deque<JsonNames> nameStack = new ArrayDeque<>();
6870
private JsonNames currentNames;
6971
private boolean allNames;
@@ -484,7 +486,7 @@ private void prefixValue() {
484486
if (lastOp == OP_END) {
485487
writeByte(COMMA);
486488
}
487-
if (pretty && (depth > 1)) {
489+
if (pretty && depth > 1 && objectDepth <= arrayDepth) {
488490
prettyIndent();
489491
}
490492
lastOp = OP_END;
@@ -496,12 +498,16 @@ public void pretty(boolean pretty) {
496498
}
497499

498500
private void writeStartObject() {
499-
if (pretty) {
500-
depth++;
501-
}
502501
if (lastOp == OP_END) {
503502
writeByte(COMMA);
504503
}
504+
if (pretty) {
505+
if (arrayDepth > 0 && arrayDepth >= objectDepth) {
506+
prettyIndent();
507+
}
508+
depth++;
509+
objectDepth++;
510+
}
505511
writeByte(OBJECT_START);
506512
lastOp = OP_START;
507513
}
@@ -531,6 +537,7 @@ public void endObject() {
531537
}
532538
if (pretty) {
533539
depth--;
540+
objectDepth--;
534541
prettyIndent();
535542
}
536543
writeByte(OBJECT_END);
@@ -543,13 +550,15 @@ public void startArray() {
543550
lastOp = OP_START;
544551
if (pretty) {
545552
depth++;
553+
arrayDepth++;
546554
}
547555
}
548556

549557
@Override
550558
public void endArray() {
551559
if (pretty) {
552560
depth--;
561+
arrayDepth--;
553562
prettyIndent();
554563
}
555564
writeByte(ARRAY_END);

jsonb-generator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<name>avaje jsonb generator</name>
1212
<description>annotation processor generating source code json adapters for avaje-jsonb</description>
1313
<properties>
14-
<avaje.prisms.version>1.43</avaje.prisms.version>
14+
<avaje.prisms.version>1.44-RC3</avaje.prisms.version>
1515
</properties>
1616

1717
<licenses>

0 commit comments

Comments
 (0)