Skip to content

Commit ff0fb97

Browse files
jpraetfmbenhassine
authored andcommitted
Implement jumpToItem in JsonItemReader
Issue #4557
1 parent dbd2089 commit ff0fb97

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
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: 17 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,19 @@ default void close() throws Exception {
5455

5556
}
5657

58+
/**
59+
* Move to the given item index. Implementations should override this method if there
60+
* is a 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 implementations to throw checked exceptions for
64+
* interpretation by the framework
65+
* @since 5.2
66+
*/
67+
default void jumpToItem(int itemIndex) throws Exception {
68+
for (int i = 0; i < itemIndex; i++) {
69+
read();
70+
}
71+
}
72+
5773
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 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.
@@ -130,4 +130,25 @@ void testInvalidResourceContent() {
130130
assertTrue(getJsonParsingException().isInstance(expectedException.getCause()));
131131
}
132132

133+
@Test
134+
void testJumpToItem() throws Exception {
135+
// given
136+
JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>().jsonObjectReader(getJsonObjectReader())
137+
.resource(new ClassPathResource("org/springframework/batch/item/json/trades.json"))
138+
.name("tradeJsonItemReader")
139+
.build();
140+
itemReader.open(new ExecutionContext());
141+
142+
// when
143+
itemReader.jumpToItem(3);
144+
145+
// then
146+
Trade trade = itemReader.read();
147+
assertNotNull(trade);
148+
assertEquals("100", trade.getIsin());
149+
assertEquals("barfoo", trade.getCustomer());
150+
assertEquals(new BigDecimal("1.8"), trade.getPrice());
151+
assertEquals(4, trade.getQuantity());
152+
}
153+
133154
}

0 commit comments

Comments
 (0)