Skip to content

Commit 5cac108

Browse files
authored
Merge pull request #92 from avaje/feature/JsonOutput
Add JsonOutput to optimise writing to Nima ServerResponse
2 parents 66a86e2 + 2c8ef51 commit 5cac108

File tree

17 files changed

+236
-15
lines changed

17 files changed

+236
-15
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
java_version: [17]
15+
java_version: [19]
1616
os: [ubuntu-latest]
1717

1818
steps:

jsonb-jackson/src/main/java/io/avaje/jsonb/jackson/JacksonAdapter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.avaje.jsonb.spi.BytesJsonWriter;
1111
import io.avaje.jsonb.spi.JsonStreamAdapter;
1212
import io.avaje.jsonb.spi.PropertyNames;
13+
import io.avaje.jsonb.stream.JsonOutput;
1314

1415
import java.io.*;
1516

@@ -207,7 +208,6 @@ public JsonWriter writer(Writer writer) {
207208
}
208209
}
209210

210-
211211
@Override
212212
public JsonWriter writer(OutputStream outputStream) {
213213
try {
@@ -217,6 +217,16 @@ public JsonWriter writer(OutputStream outputStream) {
217217
}
218218
}
219219

220+
@Override
221+
public JsonWriter writer(JsonOutput output) {
222+
try {
223+
// just unwrap the underlying OutputStream, not supporting JsonOutput per say
224+
return new JacksonWriter(jsonFactory.createGenerator(output.unwrapOutputStream()), serializeNulls, serializeEmpty);
225+
} catch (IOException e) {
226+
throw new JsonIoException(e);
227+
}
228+
}
229+
220230
@Override
221231
public BufferedJsonWriter bufferedWriter() {
222232
SegmentedStringWriter buffer = new SegmentedStringWriter(jsonFactory._getBufferRecycler());

jsonb/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
<optional>true</optional>
2121
</dependency>
2222

23+
<dependency>
24+
<groupId>io.helidon.nima.webserver</groupId>
25+
<artifactId>helidon-nima-webserver</artifactId>
26+
<version>4.0.0-ALPHA4</version>
27+
<scope>provided</scope>
28+
<optional>true</optional>
29+
</dependency>
30+
2331
<dependency>
2432
<groupId>io.avaje</groupId>
2533
<artifactId>junit</artifactId>

jsonb/src/main/java/io/avaje/jsonb/JsonView.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.jsonb;
22

3+
import io.avaje.jsonb.stream.JsonOutput;
4+
35
import java.io.OutputStream;
46
import java.io.Writer;
57

@@ -58,4 +60,9 @@ public interface JsonView<T> {
5860
* Write to the given outputStream.
5961
*/
6062
void toJson(T value, OutputStream outputStream);
63+
64+
/**
65+
* Write to the given output.
66+
*/
67+
void toJson(T value, JsonOutput output);
6168
}

jsonb/src/main/java/io/avaje/jsonb/Jsonb.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.avaje.jsonb.spi.Bootstrap;
66
import io.avaje.jsonb.spi.JsonStreamAdapter;
77
import io.avaje.jsonb.spi.PropertyNames;
8+
import io.avaje.jsonb.stream.JsonOutput;
89

910
import java.io.InputStream;
1011
import java.io.OutputStream;
@@ -314,6 +315,11 @@ static Builder newBuilder() {
314315
*/
315316
JsonWriter writer(OutputStream outputStream);
316317

318+
/**
319+
* Return the JsonWriter used to write json to the given output.
320+
*/
321+
JsonWriter writer(JsonOutput output);
322+
317323
/**
318324
* Return the property names as PropertyNames.
319325
* <p>

jsonb/src/main/java/io/avaje/jsonb/core/DJsonType.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.avaje.jsonb.*;
44
import io.avaje.jsonb.spi.BufferedJsonWriter;
55
import io.avaje.jsonb.spi.BytesJsonWriter;
6+
import io.avaje.jsonb.stream.JsonOutput;
67

78
import java.io.*;
89
import java.lang.reflect.Type;
@@ -98,7 +99,15 @@ public final void toJson(T value, OutputStream outputStream) {
9899
close(outputStream);
99100
}
100101

101-
private void close(OutputStream outputStream) {
102+
@Override
103+
public final void toJson(T value, JsonOutput output) {
104+
try (JsonWriter writer = jsonb.writer(output)) {
105+
toJson(value, writer);
106+
}
107+
close(output);
108+
}
109+
110+
private void close(Closeable outputStream) {
102111
try {
103112
outputStream.close();
104113
} catch (IOException e) {

jsonb/src/main/java/io/avaje/jsonb/core/DJsonb.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.avaje.jsonb.*;
44
import io.avaje.jsonb.spi.*;
5+
import io.avaje.jsonb.stream.JsonOutput;
56
import io.avaje.jsonb.stream.JsonStream;
67

78
import java.io.InputStream;
@@ -94,6 +95,11 @@ public JsonWriter writer(OutputStream outputStream) {
9495
return io.writer(outputStream);
9596
}
9697

98+
@Override
99+
public JsonWriter writer(JsonOutput output) {
100+
return io.writer(output);
101+
}
102+
97103
@Override
98104
public JsonReader reader(Reader reader) {
99105
return io.reader(reader);

jsonb/src/main/java/io/avaje/jsonb/core/ViewBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.avaje.jsonb.spi.BufferedJsonWriter;
55
import io.avaje.jsonb.spi.BytesJsonWriter;
66
import io.avaje.jsonb.spi.PropertyNames;
7+
import io.avaje.jsonb.stream.JsonOutput;
78

89
import java.io.IOException;
910
import java.io.OutputStream;
@@ -243,6 +244,13 @@ public void toJson(T value, OutputStream outputStream) {
243244
toJson(value, writer);
244245
}
245246
}
247+
248+
@Override
249+
public void toJson(T value, JsonOutput output) {
250+
try (JsonWriter writer = jsonb.writer(output)) {
251+
toJson(value, writer);
252+
}
253+
}
246254
}
247255

248256
@SuppressWarnings({"rawtypes", "unchecked"})

jsonb/src/main/java/io/avaje/jsonb/spi/JsonStreamAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.avaje.jsonb.JsonReader;
44
import io.avaje.jsonb.JsonWriter;
5+
import io.avaje.jsonb.stream.JsonOutput;
56

67
import java.io.*;
78

@@ -40,6 +41,11 @@ public interface JsonStreamAdapter {
4041
*/
4142
JsonWriter writer(OutputStream outputStream);
4243

44+
/**
45+
* Return the JsonWriter given the output.
46+
*/
47+
JsonWriter writer(JsonOutput output);
48+
4349
/**
4450
* Return a JsonWriter for use for writing to json string.
4551
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.avaje.jsonb.stream;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
6+
/**
7+
* Default implementation that simply wraps OutputStream.
8+
*/
9+
final class DJsonOutput implements JsonOutput {
10+
11+
private final OutputStream outputStream;
12+
13+
DJsonOutput(OutputStream outputStream) {
14+
this.outputStream = outputStream;
15+
}
16+
17+
@Override
18+
public void write(byte[] content, int offset, int length) throws IOException {
19+
outputStream.write(content, offset, length);
20+
}
21+
22+
@Override
23+
public void flush() throws IOException {
24+
outputStream.flush();
25+
}
26+
27+
@Override
28+
public void close() throws IOException {
29+
outputStream.close();
30+
}
31+
32+
@Override
33+
public OutputStream unwrapOutputStream() {
34+
return outputStream;
35+
}
36+
}

0 commit comments

Comments
 (0)