Skip to content

Commit ced71a6

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

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
*
3838
* @param <T> type of the target object
3939
* @author Mahmoud Ben Hassine
40+
* @author Jimmy Praet
4041
* @since 4.1
4142
*/
4243
public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
@@ -102,4 +103,11 @@ public void close() throws Exception {
102103
this.jsonReader.close();
103104
}
104105

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

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @param <T> type of the target object
3636
* @author Mahmoud Ben Hassine
37+
* @author Jimmy Praet
3738
* @since 4.1
3839
*/
3940
public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
@@ -98,4 +99,13 @@ public void close() throws Exception {
9899
this.jsonParser.close();
99100
}
100101

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@
4747
*
4848
* @param <T> the type of json objects to read
4949
* @author Mahmoud Ben Hassine
50+
* @author Jimmy Praet
5051
* @since 4.1
5152
*/
5253
public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<T>
@@ -136,4 +137,9 @@ protected void doClose() throws Exception {
136137
this.jsonObjectReader.close();
137138
}
138139

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

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
*
2626
* @param <T> type of the target object
2727
* @author Mahmoud Ben Hassine
28+
* @author Jimmy Praet
2829
* @since 4.1
2930
*/
3031
public interface JsonObjectReader<T> {
@@ -54,4 +55,18 @@ default void close() throws Exception {
5455

5556
}
5657

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

0 commit comments

Comments
 (0)