Skip to content

Commit

Permalink
Implement jumpToItem in JsonItemReader
Browse files Browse the repository at this point in the history
Issue #4557
  • Loading branch information
jpraet authored and fmbenhassine committed Apr 18, 2024
1 parent dbd2089 commit ff0fb97
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
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,19 @@ default void close() throws Exception {

}

/**
* Move to the given item index. Implementations 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 implementations to throw checked exceptions for
* interpretation by the framework
* @since 5.2
*/
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());
}

}

0 comments on commit ff0fb97

Please sign in to comment.