Is there any interface in Jackson
, or a better way to create the same configuration for multiple object mappers?
For example, I have this both methods for the same class:
public static File toCsv(List<Entity> entityList) {
final File tempFile = new File(CSV_FILE_NAME);
tempFile.deleteOnExit();
CsvMapper mapper = new CsvMapper();
mapper.findAndRegisterModules()
.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true)
.addMixIn(Entity.class, EntityMixIn.class)
.addMixIn(EntityB.class, EntityBMixIn.class);
CsvSchema schema = mapper.schemaFor(Entity.class).withHeader();
try {
mapper.writer(schema).writeValue(tempFile,entityList);
return tempFile;
} catch (IOException ex) {
throw new Exception();
}
}
}
public static File toXml(List<Entity> entityList) {
final File tempFile = new File(XML_FILE_NAME);
tempFile.deleteOnExit();
XmlMapper mapper = new XmlMapper();
mapper.findAndRegisterModules()
.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true)
.addMixIn(Entity.class, EntityMixIn.class)
.addMixIn(EntityB.class, EntityBMixIn.class);
try {
mapper.writeValue(tempFile, entityList);
return tempFile;
} catch (IOException ex) {
throw new Exception();
}
}
}
And both have exactly the same configuration, if in the future i need a new format, i could probably use the same configuration.