diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json/JsonFileItemWriterFunctionalTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json/JsonFileItemWriterFunctionalTests.java index 3146c56cc0..536390643b 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json/JsonFileItemWriterFunctionalTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/json/JsonFileItemWriterFunctionalTests.java @@ -59,6 +59,8 @@ public abstract class JsonFileItemWriterFunctionalTests { private ExecutionContext executionContext; private Trade trade1 = new Trade("123", 5, new BigDecimal("10.5"), "foo"); private Trade trade2 = new Trade("456", 10, new BigDecimal("20.5"), "bar"); + private Trade trade3 = new Trade("789", 15, new BigDecimal("30.5"), "foobar"); + private Trade trade4 = new Trade("987", 20, new BigDecimal("40.5"), "barfoo"); protected abstract JsonObjectMarshaller getJsonObjectMarshaller(); protected abstract JsonObjectMarshaller getJsonObjectMarshallerWithPrettyPrint(); @@ -93,6 +95,20 @@ public void testJsonWriting() throws Exception { this.resource.getFile()); } + @Test + public void testJsonWritingWithMultipleWrite() throws Exception { + // when + this.writer.open(this.executionContext); + this.writer.write(this.items); + this.writer.write(Arrays.asList(trade3, trade4)); + this.writer.close(); + + // then + assertFileEquals( + new File(EXPECTED_FILE_DIRECTORY + "expected-trades-with-multiple-writes.json"), + this.resource.getFile()); + } + @Test public void testJsonWritingWithPrettyPrinting() throws Exception { // given diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/expected-trades-with-multiple-writes.json b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/expected-trades-with-multiple-writes.json new file mode 100644 index 0000000000..f010ec6e86 --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/expected-trades-with-multiple-writes.json @@ -0,0 +1,6 @@ +[ + {"isin":"123","quantity":5,"price":10.5,"customer":"foo"}, + {"isin":"456","quantity":10,"price":20.5,"customer":"bar"}, + {"isin":"789","quantity":15,"price":30.5,"customer":"foobar"}, + {"isin":"987","quantity":20,"price":40.5,"customer":"barfoo"} +] diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/trades.json b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/trades.json index fd4cca1d80..d3780a48d4 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/trades.json +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/json/trades.json @@ -10,5 +10,17 @@ "quantity": 2, "price": 1.4, "customer": "bar" + }, + { + "isin": "789", + "quantity": 3, + "price": 1.6, + "customer": "foobar" + }, + { + "isin": "100", + "quantity": 4, + "price": 1.8, + "customer": "barfoo" } ] diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonFileItemWriter.java index 39c26e7484..277a83f696 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonFileItemWriter.java @@ -95,10 +95,10 @@ public void setJsonObjectMarshaller(JsonObjectMarshaller jsonObjectMarshaller public String doWrite(List items) { StringBuilder lines = new StringBuilder(); Iterator iterator = items.iterator(); + if (!items.isEmpty() && state.getLinesWritten() > 0) { + lines.append(JSON_OBJECT_SEPARATOR).append(this.lineSeparator); + } while (iterator.hasNext()) { - if (iterator.hasNext() && state.getLinesWritten() > 0) { - lines.append(JSON_OBJECT_SEPARATOR).append(this.lineSeparator); - } T item = iterator.next(); lines.append(' ').append(this.jsonObjectMarshaller.marshal(item)); if (iterator.hasNext()) {