Skip to content

Do not close passed-in Writer (#604) #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
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
20 changes: 17 additions & 3 deletions src/main/java/org/eclipse/yasson/internal/JsonBinding.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -12,6 +12,7 @@

package org.eclipse.yasson.internal;

import java.io.FilterWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
Expand Down Expand Up @@ -145,15 +146,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 +235,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
}

}

}
Loading