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
Expand Up @@ -49,15 +49,13 @@ public <T> T fromJson(Type type, InputStream is) {
@Override
public void toJson(Object bean, OutputStream os) {
try {
try (JsonGenerator generator = mapper.createGenerator(os)) {
try (var generator = mapper.createGenerator(os)) {
// only flush to underlying OutputStream on success
generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
generator.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
generator.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
mapper.writeValue(generator, bean);
generator.flush();
}
os.flush();
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -75,17 +73,10 @@ public String toJsonString(Object bean) {

@Override
public <T> void toJsonStream(Iterator<T> iterator, OutputStream os) {
final JsonGenerator generator;
try {
generator = mapper.createGenerator(os);
try (var generator = mapper.createGenerator(os)) {
generator.setPrettyPrinter(null);
try {
while (iterator.hasNext()) {
write(iterator, generator);
}
} finally {
generator.flush();
generator.close();
while (iterator.hasNext()) {
write(iterator, generator);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public <T> T fromJson(Type clazz, InputStream is) {

@Override
public void toJson(Object bean, OutputStream os) {
jsonb.toJson(bean, os);
jsonb.toJson(bean, new NoFlushJsonOutput(os));
}

@Override
Expand Down
12 changes: 5 additions & 7 deletions avaje-jex/src/main/java/io/avaje/jex/core/json/JsonbOutput.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.avaje.jex.core.json;

import io.avaje.jex.http.Context;
import io.avaje.json.stream.JsonOutput;

import java.io.IOException;
import java.io.OutputStream;

import io.avaje.jex.http.Context;
import io.avaje.json.stream.JsonOutput;

/**
* avaje-jsonb output that allows for writing fixed length content
* straight from the avaje-jsonb buffer, avoiding the jex side buffer.
Expand Down Expand Up @@ -43,10 +43,8 @@ public void writeLast(byte[] content, int offset, int length) throws IOException
}

@Override
public void flush() throws IOException {
if (os != null) {
os.flush();
}
public void flush() {
// shouldn't manually flush
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.avaje.jex.core.json;

import java.io.IOException;
import java.io.OutputStream;

import io.avaje.json.stream.JsonOutput;

final class NoFlushJsonOutput implements JsonOutput {

private final OutputStream outputStream;

NoFlushJsonOutput(OutputStream outputStream) {
this.outputStream = outputStream;
}

@Override
public void write(byte[] content, int offset, int length) throws IOException {
outputStream.write(content, offset, length);
}

@Override
public void flush() throws IOException {
// no flush
}

@Override
public void close() throws IOException {
outputStream.close();
}

@Override
public OutputStream unwrapOutputStream() {
return outputStream;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<maven.compiler.proc>full</maven.compiler.proc>
<avaje.config.version>4.0</avaje.config.version>
<avaje.inject.version>11.2</avaje.inject.version>
<avaje.jsonb.version>3.0</avaje.jsonb.version>
<avaje.jsonb.version>3.1-RC2</avaje.jsonb.version>
<avaje.http.version>3.0</avaje.http.version>
<avaje.metrics.version>9.4</avaje.metrics.version>
<avaje.validator.version>2.8</avaje.validator.version>
Expand Down