Skip to content

Implement jumpToItem in JsonItemReader #4557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@
*
* @param <T> type of the target object
* @author Mahmoud Ben Hassine
* @author Jimmy Praet
* @since 4.1
*/
public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
Expand Down Expand Up @@ -102,4 +103,11 @@ public void close() throws Exception {
this.jsonReader.close();
}

@Override
public void jumpToItem(int itemIndex) throws Exception {
for (int i = 0; i < itemIndex; i++) {
this.jsonReader.skipValue();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@
*
* @param <T> type of the target object
* @author Mahmoud Ben Hassine
* @author Jimmy Praet
* @since 4.1
*/
public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
Expand Down Expand Up @@ -98,4 +99,13 @@ public void close() throws Exception {
this.jsonParser.close();
}

@Override
public void jumpToItem(int itemIndex) throws Exception {
for (int i = 0; i < itemIndex; i++) {
if (this.jsonParser.nextToken() == JsonToken.START_OBJECT) {
this.jsonParser.skipChildren();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,7 @@
*
* @param <T> the type of json objects to read
* @author Mahmoud Ben Hassine
* @author Jimmy Praet
* @since 4.1
*/
public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<T>
Expand Down Expand Up @@ -136,4 +137,9 @@ protected void doClose() throws Exception {
this.jsonObjectReader.close();
}

@Override
protected void jumpToItem(int itemIndex) throws Exception {
this.jsonObjectReader.jumpToItem(itemIndex);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
*
* @param <T> type of the target object
* @author Mahmoud Ben Hassine
* @author Jimmy Praet
* @since 4.1
*/
public interface JsonObjectReader<T> {
Expand Down Expand Up @@ -54,4 +55,18 @@ default void close() throws Exception {

}

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,4 +130,25 @@ void testInvalidResourceContent() {
assertTrue(getJsonParsingException().isInstance(expectedException.getCause()));
}

@Test
void testJumpToItem() throws Exception {
// given
JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>().jsonObjectReader(getJsonObjectReader())
.resource(new ClassPathResource("org/springframework/batch/item/json/trades.json"))
.name("tradeJsonItemReader")
.build();
itemReader.open(new ExecutionContext());

// when
itemReader.jumpToItem(3);

// then
Trade trade = itemReader.read();
assertNotNull(trade);
assertEquals("100", trade.getIsin());
assertEquals("barfoo", trade.getCustomer());
assertEquals(new BigDecimal("1.8"), trade.getPrice());
assertEquals(4, trade.getQuantity());
}

}