committed by
Mahmoud Ben Hassine
parent
dbd2089c47
commit
ff0fb97272
@@ -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.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @param <T> type of the target object
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Jimmy Praet
|
||||
* @since 4.1
|
||||
*/
|
||||
public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
|
||||
@@ -102,4 +103,11 @@ public class GsonJsonObjectReader<T> implements JsonObjectReader<T> {
|
||||
this.jsonReader.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jumpToItem(int itemIndex) throws Exception {
|
||||
for (int i = 0; i < itemIndex; i++) {
|
||||
this.jsonReader.skipValue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @param <T> type of the target object
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Jimmy Praet
|
||||
* @since 4.1
|
||||
*/
|
||||
public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
|
||||
@@ -98,4 +99,13 @@ public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -47,6 +47,7 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @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>
|
||||
@@ -136,4 +137,9 @@ public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<
|
||||
this.jsonObjectReader.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void jumpToItem(int itemIndex) throws Exception {
|
||||
this.jsonObjectReader.jumpToItem(itemIndex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -25,6 +25,7 @@ import org.springframework.lang.Nullable;
|
||||
*
|
||||
* @param <T> type of the target object
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Jimmy Praet
|
||||
* @since 4.1
|
||||
*/
|
||||
public interface JsonObjectReader<T> {
|
||||
@@ -54,4 +55,19 @@ public interface JsonObjectReader<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -130,4 +130,25 @@ abstract class JsonItemReaderFunctionalTests {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user