Skip to content

Commit dcb5594

Browse files
committed
#23 - Stream support when using jackson-core
1 parent d0943c8 commit dcb5594

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public interface JsonReader extends Closeable {
3434
void names(PropertyNames names);
3535

3636
/**
37-
* Read the beginning of an ARRAY or x-json-stream.
37+
* Read the beginning of an ARRAY or x-json-stream (new line delimited json content).
3838
*/
3939
default void beginStream() {
4040
beginArray();
4141
}
4242

4343
/**
44-
* Read the end of an ARRAY or x-json-stream.
44+
* Read the end of an ARRAY or x-json-stream (new line delimited json content).
4545
*/
4646
default void endStream() {
4747
endArray();
@@ -157,6 +157,29 @@ default void endStream() {
157157
*/
158158
void unmappedField(String fieldName);
159159

160+
/**
161+
* Explicitly state if the streaming content contains ARRAY '[' and ']' tokens.
162+
* <p>
163+
* The builtin avaje-jsonb parser detects this automatically. Effectively we only need
164+
* to set this when we are using the Jackson core parser.
165+
*
166+
* <pre>{@code
167+
*
168+
* try (JsonReader reader = jsonb.reader(arrayJson)) {
169+
* // content contains ARRAY '[' and ']' tokens, use streamArray(true)
170+
* Stream<MyBasic> asStream = type.stream(reader.streamArray(true));
171+
* asStream.forEach(...);
172+
* }
173+
*
174+
* }</pre>
175+
*
176+
* @param streamArray When true the content is expected to contain ARRAY '[' and ']' tokens.
177+
*/
178+
default JsonReader streamArray(boolean streamArray) {
179+
// do nothing by default, jackson specifically needs this option
180+
return this;
181+
}
182+
160183
/**
161184
* A structure, name, or value type in a JSON-encoded string.
162185
*/

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ public interface JsonType<T> extends JsonView<T> {
6969
JsonType<List<T>> list();
7070

7171
/**
72-
* Return the stream type for this JsonType.
72+
* Prefer use of {@link JsonType#stream(JsonReader)} rather than using this stream type directly.
7373
* <p>
74-
* When using this Stream type use a try-with-resources block with the Stream
74+
* Generally, we should not use this type directly but instead create a {@link JsonReader}
75+
* and use {@link JsonType#stream(JsonReader)} instead. Then we just use a try-with-resources on
76+
* the JsonReader and can additionally specify {@link JsonReader#streamArray(boolean)} option.
77+
* <p>
78+
* When using this Stream type directly, use a try-with-resources block with the Stream
7579
* to ensure that any underlying resources are closed.
7680
*
7781
* <pre>{@code
@@ -84,6 +88,9 @@ public interface JsonType<T> extends JsonView<T> {
8488
* }
8589
*
8690
* }</pre>
91+
*
92+
* @return The stream type for this base JsonType.
93+
* @see #stream(JsonReader)
8794
*/
8895
JsonType<Stream<T>> stream();
8996

@@ -142,12 +149,32 @@ public interface JsonType<T> extends JsonView<T> {
142149
* JsonType<MyBean> type = jsonb.type(MyBean.class);
143150
*
144151
* try (JsonReader reader = jsonb.reader(content)) {
145-
152+
*
146153
* Stream<MyBean> asStream = type.stream(reader);
147154
* ...
148155
* }
149156
*
150157
* }</pre>
158+
*
159+
* <h3>When using Jackson</h3>
160+
* <p>
161+
* When using Jackson-core as the underlying parser we should explicitly state that the content is either
162+
* an ARRAY (with '[' and ']' tokens or not (x-json-stream new line delimited, there are no '[' and ']' tokens).
163+
* <p>
164+
* When using the builtin avaje-jsonb parser, it automatically detects and handles both cases (with or without the
165+
* '[' and ']' tokens). With the Jackson-core parser we need to explicitly state if we are processing
166+
*
167+
* <pre>{@code
168+
*
169+
* JsonType<MyBean> type = jsonb.type(MyBean.class);
170+
*
171+
* try (JsonReader reader = jsonb.reader(content)) {
172+
* // when the content contains the ARRAY '[', ']' tokens set streamArray(true)
173+
* Stream<MyBean> asStream = type.stream(reader.streamArray(true));
174+
* ...
175+
* }
176+
*
177+
* }</pre>
151178
*/
152179
Stream<T> stream(JsonReader reader);
153180

0 commit comments

Comments
 (0)