Skip to content

Commit 53262e6

Browse files
committed
BATCH-2691: remove leaky abstraction of json parsing exceptions by
wrapping them in a ParseException
1 parent 5b6e7b9 commit 53262e6

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json/JsonItemReaderFunctionalTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
import java.math.BigDecimal;
2020

21+
import org.hamcrest.Matchers;
2122
import org.junit.Assert;
2223
import org.junit.Rule;
2324
import org.junit.Test;
2425
import org.junit.rules.ExpectedException;
2526

2627
import org.springframework.batch.item.ExecutionContext;
2728
import org.springframework.batch.item.ItemStreamException;
29+
import org.springframework.batch.item.ParseException;
2830
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
2931
import org.springframework.batch.item.json.domain.Trade;
3032
import org.springframework.core.io.ByteArrayResource;
@@ -102,7 +104,8 @@ public void testInvalidResourceFormat() {
102104

103105
@Test
104106
public void testInvalidResourceContent() throws Exception {
105-
this.expectedException.expect(getJsonParsingException());
107+
this.expectedException.expect(ParseException.class);
108+
this.expectedException.expectCause(Matchers.instanceOf(getJsonParsingException()));
106109
JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>()
107110
.jsonObjectReader(getJsonObjectReader())
108111
.resource(new ByteArrayResource("[{]".getBytes()))

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/GsonJsonObjectReader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package org.springframework.batch.item.json;
1818

19+
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.io.InputStreamReader;
2122

2223
import com.google.gson.Gson;
24+
import com.google.gson.JsonIOException;
25+
import com.google.gson.JsonSyntaxException;
2326
import com.google.gson.stream.JsonReader;
2427
import com.google.gson.stream.JsonToken;
2528

29+
import org.springframework.batch.item.ParseException;
2630
import org.springframework.core.io.Resource;
2731
import org.springframework.util.Assert;
2832

@@ -74,8 +78,12 @@ public void open(Resource resource) throws Exception {
7478

7579
@Override
7680
public T read() throws Exception {
77-
if (this.jsonReader.hasNext()) {
78-
return this.mapper.fromJson(this.jsonReader, this.itemType);
81+
try {
82+
if (this.jsonReader.hasNext()) {
83+
return this.mapper.fromJson(this.jsonReader, this.itemType);
84+
}
85+
} catch (IOException |JsonIOException | JsonSyntaxException e) {
86+
throw new ParseException("Unable to read next JSON object", e);
7987
}
8088
return null;
8189
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JacksonJsonObjectReader.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package org.springframework.batch.item.json;
1818

19+
import java.io.IOException;
1920
import java.io.InputStream;
2021

2122
import com.fasterxml.jackson.core.JsonParser;
2223
import com.fasterxml.jackson.core.JsonToken;
2324
import com.fasterxml.jackson.databind.ObjectMapper;
2425

26+
import org.springframework.batch.item.ParseException;
2527
import org.springframework.core.io.Resource;
2628
import org.springframework.util.Assert;
2729

@@ -72,8 +74,12 @@ public void open(Resource resource) throws Exception {
7274

7375
@Override
7476
public T read() throws Exception {
75-
if (this.jsonParser.nextToken() == JsonToken.START_OBJECT) {
76-
return this.mapper.readValue(this.jsonParser, this.itemType);
77+
try {
78+
if (this.jsonParser.nextToken() == JsonToken.START_OBJECT) {
79+
return this.mapper.readValue(this.jsonParser, this.itemType);
80+
}
81+
} catch (IOException e) {
82+
throw new ParseException("Unable to read next JSON object", e);
7783
}
7884
return null;
7985
}

0 commit comments

Comments
 (0)