Skip to content

Commit e59d0ea

Browse files
committed
Implement jumpToItem in JsonItemReader
1 parent 4b8e504 commit e59d0ea

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,11 @@ public void close() throws Exception {
102102
this.jsonReader.close();
103103
}
104104

105+
@Override
106+
public void jumpToItem(int itemIndex) throws Exception {
107+
for (int i = 0; i < itemIndex; i++) {
108+
this.jsonReader.skipValue();
109+
}
110+
}
111+
105112
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,13 @@ public void close() throws Exception {
9898
this.jsonParser.close();
9999
}
100100

101+
@Override
102+
public void jumpToItem(int itemIndex) throws Exception {
103+
for (int i = 0; i < itemIndex; i++) {
104+
if (this.jsonParser.nextToken() == JsonToken.START_OBJECT) {
105+
this.jsonParser.skipChildren();
106+
}
107+
}
108+
}
109+
101110
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,9 @@ protected void doClose() throws Exception {
136136
this.jsonObjectReader.close();
137137
}
138138

139+
@Override
140+
protected void jumpToItem(int itemIndex) throws Exception {
141+
this.jsonObjectReader.jumpToItem(itemIndex);
142+
}
143+
139144
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ default void close() throws Exception {
5454

5555
}
5656

57+
/**
58+
* Move to the given item index. Subclasses should override this method if there is a
59+
* more efficient way of moving to given index than re-reading the input using
60+
* {@link #read()}.
61+
* @param itemIndex index of item (0 based) to jump to.
62+
* @throws Exception Allows subclasses to throw checked exceptions for interpretation
63+
* by the framework
64+
*/
65+
default void jumpToItem(int itemIndex) throws Exception {
66+
for (int i = 0; i < itemIndex; i++) {
67+
read();
68+
}
69+
}
70+
5771
}

0 commit comments

Comments
 (0)