Skip to content

Commit

Permalink
Do not close passed-in Writer (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
greek1979 committed Feb 12, 2025
1 parent 14e68b6 commit 860bd27
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/org/eclipse/yasson/internal/JsonBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

package org.eclipse.yasson.internal;

import java.io.FilterReader;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
Expand Down Expand Up @@ -145,15 +148,15 @@ public String toJson(Object object, Type type) throws JsonbException {
@Override
public void toJson(Object object, Writer writer) throws JsonbException {
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext);
try (JsonGenerator generator = writerGenerator(writer)) {
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
marshaller.marshallWithoutClose(object, generator);
}
}

@Override
public void toJson(Object object, Type type, Writer writer) throws JsonbException {
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext, type);
try (JsonGenerator generator = writerGenerator(writer)) {
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
marshaller.marshallWithoutClose(object, generator);
}
}
Expand Down Expand Up @@ -234,4 +237,17 @@ public void close() throws Exception {
jsonbContext.getComponentInstanceCreator().close();
}

private static class CloseSuppressingWriter extends FilterWriter {

protected CloseSuppressingWriter(final Writer in) {
super(in);
}

@Override
public void close() {
// do not close
}

}

}

0 comments on commit 860bd27

Please sign in to comment.