/** * Create a new {@link JsonFileItemWriter} instance. * @param resource to write json data to * @param jsonObjectMarshaller used to marshal object into json representation */ public JsonFileItemWriter(Resource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) { Assert.notNull(resource, "resource must not be null"); Assert.notNull(jsonObjectMarshaller, "json object marshaller must not be null"); setResource(resource); setJsonObjectMarshaller(jsonObjectMarshaller); setHeaderCallback(writer -> writer.write(JSON_ARRAY_START)); setFooterCallback(writer -> writer.write(this.lineSeparator + JSON_ARRAY_STOP + this.lineSeparator)); setExecutionContextName(ClassUtils.getShortName(JsonFileItemWriter.class)); }
@Test /* * If append=true a new output file should still be created on the first run (not restart). */ public void testAppendToNotYetExistingFile() throws Exception { Resource toBeCreated = new FileSystemResource("build/FlatFileItemWriterTests.out"); File outputFile = toBeCreated.getFile(); //enable easy content reading and auto-delete the file assertFalse("output file does not exist yet", toBeCreated.exists()); this.writer.setResource(toBeCreated); this.writer.setAppendAllowed(true); this.writer.afterPropertiesSet(); this.writer.open(this.executionContext); assertTrue("output file was created", toBeCreated.exists()); this.writer.write(Collections.singletonList(this.trade1)); this.writer.close(); assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades1.json"), outputFile); outputFile.delete(); }
/** * Create a new {@link JsonFileItemWriter} instance. * @param resource to write json data to * @param jsonObjectMarshaller used to marshal object into json representation */ public JsonFileItemWriter(Resource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) { Assert.notNull(resource, "resource must not be null"); Assert.notNull(jsonObjectMarshaller, "json object marshaller must not be null"); setResource(resource); setJsonObjectMarshaller(jsonObjectMarshaller); setHeaderCallback(writer -> writer.write(JSON_ARRAY_START)); setFooterCallback(writer -> writer.write(this.lineSeparator + JSON_ARRAY_STOP + this.lineSeparator)); setExecutionContextName(ClassUtils.getShortName(JsonFileItemWriter.class)); }