[serde-gson,serde-jackson] Unify JSON writer tests

This commit is contained in:
2025-08-03 21:57:13 +02:00
parent a666ac6c33
commit 32831c7c22
5 changed files with 158 additions and 131 deletions

View File

@@ -1,23 +1,22 @@
package de.siphaolor.tweed5.data.gson;
import com.google.gson.stream.JsonWriter;
import de.siphalor.tweed5.dataapi.api.TweedDataVisitor;
import de.siphalor.tweed5.dataapi.api.TweedDataWriteException;
import de.siphalor.tweed5.dataapi.api.TweedDataWriter;
import de.siphalor.tweed5.dataapi.api.decoration.TweedDataCommentDecoration;
import de.siphalor.tweed5.dataapi.api.decoration.TweedDataDecoration;
import org.jspecify.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class GsonWriter implements TweedDataWriter {
private final JsonWriter writer;
private final Deque<Context> contextStack = new ArrayDeque<>();
private @Nullable String deferredFieldComment;
private final List<String> deferredFieldComments = new ArrayList<>();
public GsonWriter(JsonWriter writer) {
this.writer = writer;
@@ -148,10 +147,18 @@ public class GsonWriter implements TweedDataWriter {
@Override
public void visitMapEntryKey(String key) {
try {
if (deferredFieldComment != null) {
if (!deferredFieldComments.isEmpty()) {
writer.name(key + "__comment");
writer.value(deferredFieldComment);
deferredFieldComment = null;
if (deferredFieldComments.size() == 1) {
writer.value(deferredFieldComments.get(0));
} else {
writer.beginArray();
for (String comment : deferredFieldComments) {
writer.value(comment);
}
writer.endArray();
}
deferredFieldComments.clear();
}
writer.name(key);
contextStack.push(Context.VALUE);
@@ -174,14 +181,25 @@ public class GsonWriter implements TweedDataWriter {
@Override
public void visitDecoration(TweedDataDecoration decoration) {
if (decoration instanceof TweedDataCommentDecoration) {
if (deferredFieldComment == null) {
deferredFieldComment = ((TweedDataCommentDecoration) decoration).comment();
} else {
deferredFieldComment += "\n" + ((TweedDataCommentDecoration) decoration).comment();
if (peekContext() == Context.MAP) {
appendDeferredComment(((TweedDataCommentDecoration) decoration).comment());
}
}
}
private void appendDeferredComment(String comment) {
int index = 0;
while (true) {
int next = comment.indexOf('\n', index);
if (next == -1) {
deferredFieldComments.add(comment.substring(index));
break;
}
deferredFieldComments.add(comment.substring(index, next));
index = next + 1;
}
}
@Override
public void close() throws Exception {
writer.close();

View File

@@ -1,54 +1,16 @@
package de.siphaolor.tweed5.data.gson;
import com.google.gson.GsonBuilder;
import de.siphalor.tweed5.dataapi.api.decoration.TweedDataCommentDecoration;
import de.siphalor.tweed5.dataapi.api.TweedDataWriter;
import de.siphalor.tweed5.testutils.serde.json.JsonWriterTest;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import java.io.StringWriter;
import static org.assertj.core.api.Assertions.assertThat;
class GsonWriterTest {
class GsonWriterTest implements JsonWriterTest {
@Override
@SneakyThrows
@Test
void complex() {
var stringWriter = new StringWriter();
var writer = new GsonWriter(new GsonBuilder().setPrettyPrinting().create().newJsonWriter(stringWriter));
writer.visitMapStart();
writer.visitMapEntryKey("first");
writer.visitListStart();
writer.visitInt(123);
writer.visitListStart();
writer.visitBoolean(false);
writer.visitListEnd();
writer.visitListEnd();
writer.visitDecoration((TweedDataCommentDecoration) () -> "Hello");
writer.visitDecoration((TweedDataCommentDecoration) () -> "World");
writer.visitMapEntryKey("second");
writer.visitMapStart();
writer.visitMapEntryKey("nested");
writer.visitDouble(12.34);
writer.visitMapEnd();
writer.visitMapEnd();
assertThat(stringWriter.toString()).isEqualTo("""
{
"first": [
123,
[
false
]
],
"second__comment": "Hello\\nWorld",
"second": {
"nested": 12.34
}
}""");
public TweedDataWriter createPrettyJsonWriter(StringWriter stringWriter) {
return new GsonWriter(new GsonBuilder().setPrettyPrinting().create().newJsonWriter(stringWriter));
}
}