34
34
import org .springframework .util .Assert ;
35
35
36
36
/**
37
- * Function that transforms an arbitrary split byte stream representing JSON objects into a
38
- * {@code Flux<TokenBuffer>}, where each token buffer is a well-formed JSON object.
37
+ * {@link Function} to transform a JSON stream of arbitrary size, byte array
38
+ * chunks into a {@code Flux<TokenBuffer>} where each token buffer is a
39
+ * well-formed JSON object.
39
40
*
40
41
* @author Arjen Poutsma
41
42
* @since 5.0
@@ -53,14 +54,16 @@ class Jackson2Tokenizer implements Function<DataBuffer, Flux<TokenBuffer>> {
53
54
private int arrayDepth ;
54
55
55
56
// TODO: change to ByteBufferFeeder when supported by Jackson
56
- private ByteArrayFeeder inputFeeder ;
57
+ private final ByteArrayFeeder inputFeeder ;
58
+
57
59
58
60
/**
59
61
* Create a new instance of the {@code Jackson2Tokenizer}.
60
62
* @param parser the non-blocking parser, obtained via
61
63
* {@link com.fasterxml.jackson.core.JsonFactory#createNonBlockingByteArrayParser}
62
- * @param tokenizeArrayElements if {@code true} and the "top level" JSON object is an array,
63
- * each of its elements is returned individually and immediately after it was fully received
64
+ * @param tokenizeArrayElements if {@code true} and the "top level" JSON
65
+ * object is an array, each element is returned individually, immediately
66
+ * after it is received.
64
67
*/
65
68
public Jackson2Tokenizer (JsonParser parser , boolean tokenizeArrayElements ) {
66
69
Assert .notNull (parser , "'parser' must not be null" );
@@ -71,6 +74,7 @@ public Jackson2Tokenizer(JsonParser parser, boolean tokenizeArrayElements) {
71
74
this .inputFeeder = (ByteArrayFeeder ) this .parser .getNonBlockingInputFeeder ();
72
75
}
73
76
77
+
74
78
@ Override
75
79
public Flux <TokenBuffer > apply (DataBuffer dataBuffer ) {
76
80
byte [] bytes = new byte [dataBuffer .readableByteCount ()];
@@ -86,7 +90,7 @@ public Flux<TokenBuffer> apply(DataBuffer dataBuffer) {
86
90
if (token == JsonToken .NOT_AVAILABLE ) {
87
91
break ;
88
92
}
89
- calculateDepth (token );
93
+ updateDepth (token );
90
94
91
95
if (!this .tokenizeArrayElements ) {
92
96
processTokenNormal (token , result );
@@ -106,11 +110,7 @@ public Flux<TokenBuffer> apply(DataBuffer dataBuffer) {
106
110
}
107
111
}
108
112
109
- public void endOfInput () {
110
- this .inputFeeder .endOfInput ();
111
- }
112
-
113
- private void calculateDepth (JsonToken token ) {
113
+ private void updateDepth (JsonToken token ) {
114
114
switch (token ) {
115
115
case START_OBJECT :
116
116
this .objectDepth ++;
@@ -149,7 +149,10 @@ private void processTokenArray(JsonToken token, List<TokenBuffer> result) throws
149
149
result .add (this .tokenBuffer );
150
150
this .tokenBuffer = new TokenBuffer (this .parser );
151
151
}
152
+ }
152
153
154
+ public void endOfInput () {
155
+ this .inputFeeder .endOfInput ();
153
156
}
154
157
155
158
}
0 commit comments