Since it was necessary to create a csv format that can filter output fields by multi-line header and field name, describe the program that was considered to be the library used.
2.CsvMapper/CsvSchema
You can easily convert an object (json) to csv with Jackson's library (jackson-dataformat-csv). I used this this time.
// getter/setter is required but omitted
class TestObj {
private String field1 = "aaa";
private String field2 = null;
private String field3 = "ccc";
}
TestObj obj = new TestObj();
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(TestObj.class).withHeader();
try {
System.out.println(mapper.writer(schema).writeValueAsString(obj));
} catch(JsonProcessingException e) {
//Error handling (provisional)
e.printStackTrace();
}
[output]
field1,field2,field3
aaa,,ccc
You can also do the following
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(TestObj.class).withHeader().withNullValue("<empty>");
[output]
field1,field2,field3
aaa,<empty>,ccc
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(TestObj.class).withHeader().withLineSeparator("|");
[output]
"field1","field2","field3"|"aaa",,"ccc"|
List<TestObj> objs = new ArrayList<TestObj>() {
{
add(new TestObj());
add(new TestObj());
add(new TestObj());
}
};
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(TestObj.class).withHeader();
[output]
field1,field2,field3
aaa,,ccc
aaa,,ccc
aaa,,ccc
It should be noted that it does not support the following changes to nested objects.
//Can be converted to csv
class TestObj {
private String field1 = "aaa";
private String field2 = "bbb";
private String field3 = "ccc";
}
//Cannot be converted to csv
class TestObj2 {
TestObj obj = new TestObj();
private String field4 = "aaa";
}
Create something that outputs the field name and type name of the object as a header (2 lines).
Unfortunately, jackson doesn't support multiple headers or filters, so I created it myself.
public static void main(String[] args) {
TestObj obj = new TestObj();
List<Field> fields = Arrays.asList(obj.getClass().getDeclaredFields());
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(TestObj.class);
//Field to filter (originally external input)
List<String> filterParam = new ArrayList<String>() {
{
add("field1");
add("field3");
}
};
//Create multiple headers & filters
String header = createHeader(obj, filterParam, fields);
List<String> bodies = new ArrayList<>();
try {
bodies = Arrays.asList(mapper.writer(schema).writeValueAsString(obj).split(","));
} catch (JsonProcessingException e) {
//Error handling (provisional)
e.printStackTrace();
}
String body = filter(bodies, filterParam, fields);
//Originally connected and returned
System.out.println(header);
System.out.println(body);
}
private static String createHeader(TestObj obj, List<String> param, List<Field> fields) {
String fieldHeader = fields.stream().filter(o -> param.contains(o.getName())).map(o -> o.getName
)
.collect(Collectors.joining(","));
String typeHeader = fields.stream().filter(o -> param.contains(o.getName())).map(o -> {
String typeName = o.getType().getName();
return typeName.substring(typeName.lastIndexOf(".") + 1, typeName.length());
}).collect(Collectors.joining(","));
return fieldHeader + "\n" + typeHeader;
}
private static String filter(List<String> bodies, List<String> param, List<Field> fields) {
if (param == null || param.isEmpty()) {
return bodies.stream().collect(Collectors.joining(","));
}
List<String> res = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
String fieldName = fields.get(i).getName();
if (param.contains(fieldName)) {
res.add(bodies.get(i));
}
}
return res.stream().collect(Collectors.joining(","));
}
[Output result]
field1,field3
String,String
aaa,ccc
CsvSchema http://fasterxml.github.io/jackson-dataformat-csv/javadoc/2.8/com/fasterxml/jackson/dataformat/csv/CsvSchema.html#withoutColumns()
Several ways to concatenate strings separated by commas in Java https://qiita.com/shisama/items/b27d16b3aeb1baf055b1
String concatenation in Java 8 https://qiita.com/lonerydeveloper/items/9f7c977c039ad4d24d30
Recommended Posts