我正在尝试将 csv(tsv) 文件反序列化为不可变的 POJO。
Jackson 将值分配给错误的字段(它忽略了带注释的列标题名称)类字段的编写顺序与文件中的顺序相同。
我真的必须为此创建自定义 CSV 架构吗?无论如何,“withHeader()”有什么意义,忽略第一行?
我尝试编辑 csv(tsv) 文件的标题,将名称更改为未注释的内容,但没有发生错误。
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Person(@JsonProperty("name") String name,
@JsonProperty("address") String address,
@JsonProperty("phone") String phone) {
this.name = name;
...
}
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Person.class).withColumnSeparator('\t').withHeader();
MappingIterator<Person> it = mapper.readerFor(Person.class).with(schema).readValues(file);
Run Code Online (Sandbox Code Playgroud) 给定一个POJO:
class Person {
@Expose
String name;
@Expose
String phone;
@Expose
String fax;
}
Run Code Online (Sandbox Code Playgroud)
我希望“电话”始终都进行序列化,但是仅当“传真”不为null时才进行序列化。因此,假设没有电话或传真的“约翰”人:
当前:
{ "name": "John", "phone": null, "fax": null }
Run Code Online (Sandbox Code Playgroud)
我需要的:
{ "name": "John", "phone": null }
Run Code Online (Sandbox Code Playgroud)
是否有类似的东西:
@Expose (serialize_if_null = false)
Run Code Online (Sandbox Code Playgroud)
瞬态不起作用,因为如果它有值,我仍然希望将其序列化。
使用ExclusionStrategy,我可以定义字段,但是似乎找不到找到值的方法。
谢谢
为什么此代码返回false?
Path path = Paths.get("C:\\aaa\\bbb\\ccc");
Files.exists(path); // false!?
Run Code Online (Sandbox Code Playgroud)
即使当我从文件(存在)转换为它时:
File file = new File("C:\\aaa\\bbb\\ccc");
file.exists(); // true!!!
Path path = file.toPath();
Files.exists(path); // still false!?
Run Code Online (Sandbox Code Playgroud)