Skip to content

Fix JsonFileItemWriter to produce valid JSON when append allowed#5273

Open
quaff wants to merge 1 commit intospring-projects:mainfrom
quaff:patch-54
Open

Fix JsonFileItemWriter to produce valid JSON when append allowed#5273
quaff wants to merge 1 commit intospring-projects:mainfrom
quaff:patch-54

Conversation

@quaff
Copy link
Contributor

@quaff quaff commented Feb 6, 2026

Closes GH-5272

@banseok1216
Copy link
Contributor

banseok1216 commented Feb 6, 2026

how about when

writer.setAppendAllowed(true);

writer.open(new ExecutionContext());
writer.close();

writer.open(new ExecutionContext());
writer.write(Chunk.of("ccc"));
writer.close();

resourceShouldContains("ccc");

@quaff
Copy link
Contributor Author

quaff commented Feb 6, 2026

how about when

writer.setAppendAllowed(true);

writer.open(new ExecutionContext());
writer.close();

writer.open(new ExecutionContext());
writer.write(Chunk.of("ccc"));
writer.close();

resourceShouldContains("ccc");

how about when

writer.setAppendAllowed(true);

writer.open(new ExecutionContext());
writer.close();

writer.open(new ExecutionContext());
writer.write(Chunk.of("ccc"));
writer.close();

resourceShouldContains("ccc");

Thanks for spotting, please review again.

@banseok1216
Copy link
Contributor

Thank you. Minor case I found.

@Test
void appendAllowed() throws Exception {
	File file = Files.createTempFile("test", ".json").toFile();
	WritableResource resource = new FileSystemResource(file);

	Files.writeString(file.toPath(), "[\n \"foo\"]", StandardCharsets.UTF_8);

	JsonFileItemWriter<String> writer =
			new JsonFileItemWriter<>(resource, new JacksonJsonObjectMarshaller<>());
	writer.setAppendAllowed(true);

	String before = Files.readString(file.toPath(), StandardCharsets.UTF_8);
	System.out.println("before:\n" + before + "\n");
	System.out.println("json mapper act: " + Arrays.toString(new JsonMapper().readValue(before, String[].class)) + "\n");

	writer.open(new ExecutionContext());
	writer.close();

	String after = new String(resource.getContentAsByteArray(), StandardCharsets.UTF_8);
	System.out.println("after:\n" + after + "\n");
}
before:
[
 "foo"]

json mapper act: [foo]

after:
[
 "fo
]

@quaff
Copy link
Contributor Author

quaff commented Feb 6, 2026

JsonFileItemWriter will never generate [\n \"foo\"], you should write [\n \"foo\"\n]\n instead.

@banseok1216
Copy link
Contributor

Okay then, I think the writer needs to validate that the existing file matches its expected format

Closes spring-projectsGH-5272

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
@quaff
Copy link
Contributor Author

quaff commented Feb 9, 2026

Thank you. Minor case I found.

@Test
void appendAllowed() throws Exception {
	File file = Files.createTempFile("test", ".json").toFile();
	WritableResource resource = new FileSystemResource(file);

	Files.writeString(file.toPath(), "[\n \"foo\"]", StandardCharsets.UTF_8);

	JsonFileItemWriter<String> writer =
			new JsonFileItemWriter<>(resource, new JacksonJsonObjectMarshaller<>());
	writer.setAppendAllowed(true);

	String before = Files.readString(file.toPath(), StandardCharsets.UTF_8);
	System.out.println("before:\n" + before + "\n");
	System.out.println("json mapper act: " + Arrays.toString(new JsonMapper().readValue(before, String[].class)) + "\n");

	writer.open(new ExecutionContext());
	writer.close();

	String after = new String(resource.getContentAsByteArray(), StandardCharsets.UTF_8);
	System.out.println("after:\n" + after + "\n");
}
before:
[
 "foo"]

json mapper act: [foo]

after:
[
 "fo
]

@banseok1216 I updated PR to fix this also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

appendAllowed(true) breaks JSON array when the writer is opened/written/closed multiple times

2 participants