Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.example.customer;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.util.List;

import org.junit.jupiter.api.Test;

import io.avaje.jsonb.Json;
import io.avaje.jsonb.Jsonb;

public class PrettyNestedTest {
@Json
public record DataContainer(String id, Instant createdAt, List<App> apps) {
public record App(
String id, Instant createdAt, Instant updatedAt, String name, List<String> roles) {}
}

@Test
void testNested() {
final String pretty =
"""
{
"id": "f11177d2-ec63-3995-bb4a-c628e0d782df",
"createdAt": "1970-01-01T00:00:01Z",
"apps": [
{
"id": "ea4959eb-64a1-309b-a580-d950964f3843",
"createdAt": "1970-01-01T00:00:01Z",
"updatedAt": "1970-01-01T00:00:01Z",
"name": "Name",
"roles": [
"admin"
]
},
{
"id": "ea4959eb-64a1-309b-a580-d950964f3843",
"createdAt": "1970-01-01T00:00:01Z",
"updatedAt": "1970-01-01T00:00:01Z",
"name": "Name",
"roles": [
"admin"
]
}
]
}""";

var type = Jsonb.instance().type(DataContainer.class);
String jsonPretty = type.toJsonPretty(type.fromJson(pretty));
assertThat(jsonPretty).isEqualTo(pretty);
}
}
17 changes: 13 additions & 4 deletions json-core/src/main/java/io/avaje/json/stream/core/JGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class JGenerator implements JsonGenerator {
private int position;
private boolean pretty;
private int depth;
private int arrayDepth;
private int objectDepth;
private final Deque<JsonNames> nameStack = new ArrayDeque<>();
private JsonNames currentNames;
private boolean allNames;
Expand Down Expand Up @@ -484,7 +486,7 @@ private void prefixValue() {
if (lastOp == OP_END) {
writeByte(COMMA);
}
if (pretty && (depth > 1)) {
if (pretty && depth > 1 && objectDepth <= arrayDepth) {
prettyIndent();
}
lastOp = OP_END;
Expand All @@ -496,12 +498,16 @@ public void pretty(boolean pretty) {
}

private void writeStartObject() {
if (pretty) {
depth++;
}
if (lastOp == OP_END) {
writeByte(COMMA);
}
if (pretty) {
if (arrayDepth > 0 && arrayDepth >= objectDepth) {
prettyIndent();
}
depth++;
objectDepth++;
}
writeByte(OBJECT_START);
lastOp = OP_START;
}
Expand Down Expand Up @@ -531,6 +537,7 @@ public void endObject() {
}
if (pretty) {
depth--;
objectDepth--;
prettyIndent();
}
writeByte(OBJECT_END);
Expand All @@ -543,13 +550,15 @@ public void startArray() {
lastOp = OP_START;
if (pretty) {
depth++;
arrayDepth++;
}
}

@Override
public void endArray() {
if (pretty) {
depth--;
arrayDepth--;
prettyIndent();
}
writeByte(ARRAY_END);
Expand Down
2 changes: 1 addition & 1 deletion jsonb-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<name>avaje jsonb generator</name>
<description>annotation processor generating source code json adapters for avaje-jsonb</description>
<properties>
<avaje.prisms.version>1.43</avaje.prisms.version>
<avaje.prisms.version>1.44-RC3</avaje.prisms.version>
</properties>

<licenses>
Expand Down