I have a Java class and i want to serialize it into CSV using jackson. In addition i want to exclude from the csv a single field base on a external property.
I have tried using all features provides by jackson like Feature.IGNORE_UNKNOWN=true
or @JsonIgnoreProperties(ignoreUnknown = true)
on my data class, csvMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
but none of them works. I still receive the exception column not found in schema when not all columns are declared in the schema. Code works fine in the other case.
Here i upload my implementation
public class MyClass{
@JsonProperty("A")
private string a;
@JsonProperty("B")
private string b;
@JsonProperty("C")
private string c;
}
CsvMapper mapper = new CsvMapper();
mapper.configure(Feature.IGNORE_UNKNOWN, true);
CsvSchema csvSchema;
if (disable = true) {
schema = CsvSchema.builder()
.addColumn("A")
.addColumn("C")
.build()
} else {
schema = CsvSchema.builder()
.addColumn("A")
.addColumn("B")
.addColumn("C")
.build()
}
ObjectWriter ow = mapper.writer(schema);
String csv = ow.writeValueAsString(list);