Skip to content

Commit

Permalink
Return null when reaching at the end of input in the beginning of Jso…
Browse files Browse the repository at this point in the history
…nValueParser#captureJsonValues
  • Loading branch information
dmikurube committed Aug 8, 2023
1 parent 8fc77a9 commit 49c98de
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,10 @@ static CapturingDirectMemberNameList of(final List<String> memberNames) {
JsonValue[] captureFromParser(
final JsonParser jacksonParser,
final InternalJsonValueReader valueReader) throws IOException {
final JsonValue[] values = new JsonValue[this.size];
for (int i = 0; i < values.length; i++) {
values[i] = null;
}

try {
final JsonToken firstToken = jacksonParser.nextToken();
if (firstToken == null) {
throw new JsonParseException("Failed to parse JSON");
return null;
}
if (firstToken != JsonToken.START_OBJECT) {
throw new JsonParseException("Failed to parse JSON: Expected JSON Object, but " + firstToken.toString());
Expand All @@ -70,6 +65,11 @@ JsonValue[] captureFromParser(
throw new JsonParseException("Failed to parse JSON", ex);
}

final JsonValue[] values = new JsonValue[this.size];
for (int i = 0; i < values.length; i++) {
values[i] = null;
}

while (true) {
final JsonToken token = jacksonParser.nextToken();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ JsonValue[] captureFromParser(final JsonParser parser, final InternalJsonValueRe
this.size,
valueReader);

final boolean isFirstAvailable = capturer.next();
if (!isFirstAvailable) {
return null;
}

while (capturer.next()) {
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ private CapturingPointerToRoot() {
JsonValue[] captureFromParser(
final JsonParser jacksonParser,
final InternalJsonValueReader valueReader) throws IOException {
final JsonValue value = valueReader.read(jacksonParser);
if (value == null) {
return null;
}

final JsonValue[] values = new JsonValue[1];
values[0] = valueReader.read(jacksonParser);
values[0] = value;
return values;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/embulk/util/json/CapturingPointers.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static Builder builder() {
* Captures JSON values with the capturing pointers from the parser.
*
* @param parser the parser to capture values from
* @return the array of captured JSON values
* @return the array of captured JSON values, or {@code null} if the parser reaches at the end of input in the beginning
*/
abstract JsonValue[] captureFromParser(
final JsonParser parser,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/embulk/util/json/JsonValueParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static Builder builder(final JsonFactory jsonFactory) {
/**
* Reads a {@link org.embulk.spi.json.JsonValue} from the parser.
*
* @return the JSON value
* @return the JSON value, or {@code null} if the parser reaches at the end of input in the beginning
* @throws IOException if failing to read JSON
* @throws JsonParseException if failing to parse JSON
*/
Expand All @@ -222,7 +222,7 @@ public JsonValue readJsonValue() throws IOException {
/**
* Captures {@link org.embulk.spi.json.JsonValue}s from the parser with the specified capturing pointers.
*
* @return the captured JSON values
* @return an array of the captured JSON values, or {@code null} if the parser reaches at the end of input in the beginning
* @throws IOException if failing to read JSON
* @throws JsonParseException if failing to parse JSON
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void testRead1() throws Exception {

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

assertNull(capturingPointers.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

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

assertNull(capturingPointers.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

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

assertEquals(4, actual2.length);
assertEquals(JsonLong.of(84), actual2[0]);
assertEquals(JsonObject.of("foo", JsonLong.of(84), "bar", JsonBoolean.FALSE), actual2[1]);
assertEquals(JsonBoolean.FALSE, actual2[2]);
assertNull(actual2[3]);
assertNull(capturingPointers.captureFromParser(parser2, reader));
assertNull(parser2.nextToken());

assertEquals(4, actual3.length);
assertEquals(JsonLong.of(123), actual3[0]);
assertEquals(JsonObject.of("foo", JsonLong.of(123), "bar", JsonBoolean.FALSE), actual3[1]);
assertEquals(JsonBoolean.FALSE, actual3[2]);
assertNull(actual3[3]);
assertNull(capturingPointers.captureFromParser(parser3, reader));
assertNull(parser3.nextToken());
}

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

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

assertNull(capturingPointers1.captureFromParser(parser, reader));
assertNull(capturingPointers2.captureFromParser(parser, reader));
assertNull(parser.nextToken());
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/embulk/util/json/TestJsonValueParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public void testCaptureJsonPointers() throws Exception {
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
values[1]);
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[2]);

// Confirming that JsonValueParser reaches at the end as expected.

assertNull(parser.captureJsonValues(pointers));
}

@Test
Expand All @@ -166,6 +170,10 @@ public void testCaptureDirectMemberNames() throws Exception {
assertEquals(2, values.length);
assertEquals(JsonLong.of(12L), values[0]);
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[1]);

// Confirming that JsonValueParser reaches at the end as expected.

assertNull(parser.captureJsonValues(pointers));
}

@Test
Expand All @@ -187,6 +195,10 @@ public void testCaptureMixed() throws Exception {
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
values[1]);
assertEquals(JsonObject.of("hoge", JsonString.of("fuga")), values[2]);

// Confirming that JsonValueParser reaches at the end as expected.

assertNull(parser.captureJsonValues(pointers));
}

@Test
Expand All @@ -203,5 +215,9 @@ public void testCaptureRoot() throws Exception {
"baz", JsonNull.NULL,
"qux", JsonObject.of("hoge", JsonString.of("fuga"))),
values[0]);

// Confirming that JsonValueParser reaches at the end as expected.

assertNull(parser.captureJsonValues(pointers));
}
}

0 comments on commit 49c98de

Please sign in to comment.