File tree Expand file tree Collapse file tree 3 files changed +22
-5
lines changed
spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json
spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Original file line number Diff line number Diff line change 18
18
19
19
import java .math .BigDecimal ;
20
20
21
+ import org .hamcrest .Matchers ;
21
22
import org .junit .Assert ;
22
23
import org .junit .Rule ;
23
24
import org .junit .Test ;
24
25
import org .junit .rules .ExpectedException ;
25
26
26
27
import org .springframework .batch .item .ExecutionContext ;
27
28
import org .springframework .batch .item .ItemStreamException ;
29
+ import org .springframework .batch .item .ParseException ;
28
30
import org .springframework .batch .item .json .builder .JsonItemReaderBuilder ;
29
31
import org .springframework .batch .item .json .domain .Trade ;
30
32
import org .springframework .core .io .ByteArrayResource ;
@@ -102,7 +104,8 @@ public void testInvalidResourceFormat() {
102
104
103
105
@ Test
104
106
public void testInvalidResourceContent () throws Exception {
105
- this .expectedException .expect (getJsonParsingException ());
107
+ this .expectedException .expect (ParseException .class );
108
+ this .expectedException .expectCause (Matchers .instanceOf (getJsonParsingException ()));
106
109
JsonItemReader <Trade > itemReader = new JsonItemReaderBuilder <Trade >()
107
110
.jsonObjectReader (getJsonObjectReader ())
108
111
.resource (new ByteArrayResource ("[{]" .getBytes ()))
Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .batch .item .json ;
18
18
19
+ import java .io .IOException ;
19
20
import java .io .InputStream ;
20
21
import java .io .InputStreamReader ;
21
22
22
23
import com .google .gson .Gson ;
24
+ import com .google .gson .JsonIOException ;
25
+ import com .google .gson .JsonSyntaxException ;
23
26
import com .google .gson .stream .JsonReader ;
24
27
import com .google .gson .stream .JsonToken ;
25
28
29
+ import org .springframework .batch .item .ParseException ;
26
30
import org .springframework .core .io .Resource ;
27
31
import org .springframework .util .Assert ;
28
32
@@ -74,8 +78,12 @@ public void open(Resource resource) throws Exception {
74
78
75
79
@ Override
76
80
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 );
79
87
}
80
88
return null ;
81
89
}
Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .batch .item .json ;
18
18
19
+ import java .io .IOException ;
19
20
import java .io .InputStream ;
20
21
21
22
import com .fasterxml .jackson .core .JsonParser ;
22
23
import com .fasterxml .jackson .core .JsonToken ;
23
24
import com .fasterxml .jackson .databind .ObjectMapper ;
24
25
26
+ import org .springframework .batch .item .ParseException ;
25
27
import org .springframework .core .io .Resource ;
26
28
import org .springframework .util .Assert ;
27
29
@@ -72,8 +74,12 @@ public void open(Resource resource) throws Exception {
72
74
73
75
@ Override
74
76
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 );
77
83
}
78
84
return null ;
79
85
}
You can’t perform that action at this time.
0 commit comments