Skip to content

Fix JsonFileItemWriter for multiple write's #636

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
Expand Up @@ -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<Trade> getJsonObjectMarshaller();
protected abstract JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}
]
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public void setJsonObjectMarshaller(JsonObjectMarshaller<T> jsonObjectMarshaller
public String doWrite(List<? extends T> items) {
StringBuilder lines = new StringBuilder();
Iterator<? extends T> 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()) {
Expand Down