Skip to content

Commit 8eba18b

Browse files
authored
Merge pull request #31 from embulk/return-null-from-captureJsonValues
Return null when reaching at the end of input in the beginning of JsonValueParser#captureJsonValues
2 parents 8fc77a9 + 49c98de commit 8eba18b

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

src/main/java/org/embulk/util/json/CapturingDirectMemberNameList.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,10 @@ static CapturingDirectMemberNameList of(final List<String> memberNames) {
4747
JsonValue[] captureFromParser(
4848
final JsonParser jacksonParser,
4949
final InternalJsonValueReader valueReader) throws IOException {
50-
final JsonValue[] values = new JsonValue[this.size];
51-
for (int i = 0; i < values.length; i++) {
52-
values[i] = null;
53-
}
54-
5550
try {
5651
final JsonToken firstToken = jacksonParser.nextToken();
5752
if (firstToken == null) {
58-
throw new JsonParseException("Failed to parse JSON");
53+
return null;
5954
}
6055
if (firstToken != JsonToken.START_OBJECT) {
6156
throw new JsonParseException("Failed to parse JSON: Expected JSON Object, but " + firstToken.toString());
@@ -70,6 +65,11 @@ JsonValue[] captureFromParser(
7065
throw new JsonParseException("Failed to parse JSON", ex);
7166
}
7267

68+
final JsonValue[] values = new JsonValue[this.size];
69+
for (int i = 0; i < values.length; i++) {
70+
values[i] = null;
71+
}
72+
7373
while (true) {
7474
final JsonToken token = jacksonParser.nextToken();
7575

src/main/java/org/embulk/util/json/CapturingJsonPointerList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ JsonValue[] captureFromParser(final JsonParser parser, final InternalJsonValueRe
8080
this.size,
8181
valueReader);
8282

83+
final boolean isFirstAvailable = capturer.next();
84+
if (!isFirstAvailable) {
85+
return null;
86+
}
87+
8388
while (capturer.next()) {
8489
;
8590
}

src/main/java/org/embulk/util/json/CapturingPointerToRoot.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ private CapturingPointerToRoot() {
2828
JsonValue[] captureFromParser(
2929
final JsonParser jacksonParser,
3030
final InternalJsonValueReader valueReader) throws IOException {
31+
final JsonValue value = valueReader.read(jacksonParser);
32+
if (value == null) {
33+
return null;
34+
}
35+
3136
final JsonValue[] values = new JsonValue[1];
32-
values[0] = valueReader.read(jacksonParser);
37+
values[0] = value;
3338
return values;
3439
}
3540

src/main/java/org/embulk/util/json/CapturingPointers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static Builder builder() {
185185
* Captures JSON values with the capturing pointers from the parser.
186186
*
187187
* @param parser the parser to capture values from
188-
* @return the array of captured JSON values
188+
* @return the array of captured JSON values, or {@code null} if the parser reaches at the end of input in the beginning
189189
*/
190190
abstract JsonValue[] captureFromParser(
191191
final JsonParser parser,

src/main/java/org/embulk/util/json/JsonValueParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static Builder builder(final JsonFactory jsonFactory) {
211211
/**
212212
* Reads a {@link org.embulk.spi.json.JsonValue} from the parser.
213213
*
214-
* @return the JSON value
214+
* @return the JSON value, or {@code null} if the parser reaches at the end of input in the beginning
215215
* @throws IOException if failing to read JSON
216216
* @throws JsonParseException if failing to parse JSON
217217
*/
@@ -222,7 +222,7 @@ public JsonValue readJsonValue() throws IOException {
222222
/**
223223
* Captures {@link org.embulk.spi.json.JsonValue}s from the parser with the specified capturing pointers.
224224
*
225-
* @return the captured JSON values
225+
* @return an array of the captured JSON values, or {@code null} if the parser reaches at the end of input in the beginning
226226
* @throws IOException if failing to read JSON
227227
* @throws JsonParseException if failing to parse JSON
228228
*/

src/test/java/org/embulk/util/json/TestCapturingJsonPointerList.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void testRead1() throws Exception {
6565

6666
// Confirming that JsonParser reaches at the end as expected.
6767

68+
assertNull(capturingPointers1.captureFromParser(parser, reader));
6869
assertNull(parser.nextToken());
6970
}
7071

@@ -96,6 +97,7 @@ public void testRead2() throws Exception {
9697

9798
// Confirming that JsonParser reaches at the end as expected.
9899

100+
assertNull(capturingPointers1.captureFromParser(parser, reader));
99101
assertNull(parser.nextToken());
100102
}
101103

@@ -129,6 +131,7 @@ public void testRead3() throws Exception {
129131

130132
// Confirming that JsonParser reaches at the end as expected.
131133

134+
assertNull(capturingPointers1.captureFromParser(parser, reader));
132135
assertNull(parser.nextToken());
133136
}
134137

@@ -160,6 +163,7 @@ public void testRead4() throws Exception {
160163

161164
// Confirming that JsonParser reaches at the end as expected.
162165

166+
assertNull(capturingPointers1.captureFromParser(parser, reader));
163167
assertNull(parser.nextToken());
164168
}
165169

@@ -185,6 +189,7 @@ public void testRead5() throws Exception {
185189

186190
// Confirming that JsonParser reaches at the end as expected.
187191

192+
assertNull(capturingPointers1.captureFromParser(parser, reader));
188193
assertNull(parser.nextToken());
189194
}
190195

@@ -208,6 +213,7 @@ public void testRead6() throws Exception {
208213

209214
// Confirming that JsonParser reaches at the end as expected.
210215

216+
assertNull(capturingPointers1.captureFromParser(parser, reader));
211217
assertNull(parser.nextToken());
212218
}
213219

@@ -233,6 +239,7 @@ public void testRead7() throws Exception {
233239

234240
// Confirming that JsonParser reaches at the end as expected.
235241

242+
assertNull(capturingPointers1.captureFromParser(parser, reader));
236243
assertNull(parser.nextToken());
237244
}
238245

@@ -256,6 +263,7 @@ public void testRead8() throws Exception {
256263

257264
// Confirming that JsonParser reaches at the end as expected.
258265

266+
assertNull(capturingPointers1.captureFromParser(parser, reader));
259267
assertNull(parser.nextToken());
260268
}
261269

@@ -298,6 +306,7 @@ public void testReadArray() throws Exception {
298306

299307
assertEquals(JsonToken.END_ARRAY, parser.nextToken());
300308

309+
assertNull(capturingPointers.captureFromParser(parser, reader));
301310
assertNull(parser.nextToken());
302311
}
303312

@@ -336,6 +345,7 @@ public void testReadSequence() throws Exception {
336345
assertEquals(JsonBoolean.FALSE, actual3[2]);
337346
assertNull(actual3[3]);
338347

348+
assertNull(capturingPointers.captureFromParser(parser, reader));
339349
assertNull(parser.nextToken());
340350
}
341351

@@ -375,6 +385,7 @@ public void testReadScalars() throws Exception {
375385

376386
assertEquals(JsonToken.END_ARRAY, parser.nextToken());
377387

388+
assertNull(capturingPointers.captureFromParser(parser, reader));
378389
assertNull(parser.nextToken());
379390
}
380391

@@ -401,20 +412,23 @@ public void testReadMultiParsers() throws Exception {
401412
assertEquals(JsonObject.of("foo", JsonLong.of(12), "bar", JsonBoolean.TRUE), actual1[1]);
402413
assertEquals(JsonBoolean.TRUE, actual1[2]);
403414
assertNull(actual1[3]);
415+
assertNull(capturingPointers.captureFromParser(parser1, reader));
404416
assertNull(parser1.nextToken());
405417

406418
assertEquals(4, actual2.length);
407419
assertEquals(JsonLong.of(84), actual2[0]);
408420
assertEquals(JsonObject.of("foo", JsonLong.of(84), "bar", JsonBoolean.FALSE), actual2[1]);
409421
assertEquals(JsonBoolean.FALSE, actual2[2]);
410422
assertNull(actual2[3]);
423+
assertNull(capturingPointers.captureFromParser(parser2, reader));
411424
assertNull(parser2.nextToken());
412425

413426
assertEquals(4, actual3.length);
414427
assertEquals(JsonLong.of(123), actual3[0]);
415428
assertEquals(JsonObject.of("foo", JsonLong.of(123), "bar", JsonBoolean.FALSE), actual3[1]);
416429
assertEquals(JsonBoolean.FALSE, actual3[2]);
417430
assertNull(actual3[3]);
431+
assertNull(capturingPointers.captureFromParser(parser3, reader));
418432
assertNull(parser3.nextToken());
419433
}
420434

@@ -477,6 +491,8 @@ public void testReadContinued() throws Exception {
477491

478492
// Confirming that JsonParser reaches at the end as expected.
479493

494+
assertNull(capturingPointers1.captureFromParser(parser, reader));
495+
assertNull(capturingPointers2.captureFromParser(parser, reader));
480496
assertNull(parser.nextToken());
481497
}
482498

src/test/java/org/embulk/util/json/TestJsonValueParser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public void testCaptureJsonPointers() throws Exception {
153153
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
154154
values[1]);
155155
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[2]);
156+
157+
// Confirming that JsonValueParser reaches at the end as expected.
158+
159+
assertNull(parser.captureJsonValues(pointers));
156160
}
157161

158162
@Test
@@ -166,6 +170,10 @@ public void testCaptureDirectMemberNames() throws Exception {
166170
assertEquals(2, values.length);
167171
assertEquals(JsonLong.of(12L), values[0]);
168172
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[1]);
173+
174+
// Confirming that JsonValueParser reaches at the end as expected.
175+
176+
assertNull(parser.captureJsonValues(pointers));
169177
}
170178

171179
@Test
@@ -187,6 +195,10 @@ public void testCaptureMixed() throws Exception {
187195
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
188196
values[1]);
189197
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[2]);
198+
199+
// Confirming that JsonValueParser reaches at the end as expected.
200+
201+
assertNull(parser.captureJsonValues(pointers));
190202
}
191203

192204
@Test
@@ -203,5 +215,9 @@ public void testCaptureRoot() throws Exception {
203215
"baz", JsonNull.NULL,
204216
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
205217
values[0]);
218+
219+
// Confirming that JsonValueParser reaches at the end as expected.
220+
221+
assertNull(parser.captureJsonValues(pointers));
206222
}
207223
}

0 commit comments

Comments
 (0)