[serde-*] Make data readers and writers AutoCloseable

This commit is contained in:
2025-08-03 20:36:28 +02:00
parent 1fc418970f
commit 7ce3aaac06
16 changed files with 117 additions and 63 deletions

View File

@@ -266,6 +266,11 @@ public class GsonReader implements TweedDataReader {
};
}
@Override
public void close() throws Exception {
reader.close();
}
private enum Context {
VALUE,
LIST,

View File

@@ -3,6 +3,7 @@ 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;
@@ -11,7 +12,7 @@ import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
public class GsonWriter implements TweedDataVisitor {
public class GsonWriter implements TweedDataWriter {
private final JsonWriter writer;
private final Deque<Context> contextStack = new ArrayDeque<>();
@@ -181,6 +182,11 @@ public class GsonWriter implements TweedDataVisitor {
}
}
@Override
public void close() throws Exception {
writer.close();
}
private void afterValueWritten() {
if (peekContext() == Context.VALUE) {
contextStack.pop();