Java 如何序列化/反序列化整数列表?这个库有可能通过Quirk实现这一点吗?
哟 我正在尝试这个新的CSV库怪癖CSV。我已经将它用于基本对象,但我正在尝试找出如何序列化/反序列化整数列表。这可能与怪癖有关吗Java 如何序列化/反序列化整数列表?这个库有可能通过Quirk实现这一点吗?,java,csv,Java,Csv,哟 我正在尝试这个新的CSV库怪癖CSV。我已经将它用于基本对象,但我正在尝试找出如何序列化/反序列化整数列表。这可能与怪癖有关吗 public static void main(String[] args) throws IOException { CSVProcessor<Client> parser = new CSVProcessor<>(Client.class); String header = &qu
public static void main(String[] args) throws IOException {
CSVProcessor<Client> parser = new CSVProcessor<>(Client.class);
String header = "ID,NAME,AGENTS\n";
String data = "1,client,2|4|66";
List<Client> list = parser.parse(new StringReader(header+data), CSVFormat.DEFAULT.withIgnoreHeaderCase());
StringWriter sw = new StringWriter();
parser.write(list,sw,CSVFormat.DEFAULT);
System.out.println(sw);
}
@CSVReadComponent(type = CSVType.NAMED)
@CSVWriteComponent(type = CSVType.NAMED)
public static class Client{
@CSVReadBinding(header = "id")
@CSVWriteBinding(header = "id")
private long id;
@CSVReadBinding(header = "name")
@CSVWriteBinding(header = "name")
private String name;
@CSVReadBinding(header = "agents")
@CSVWriteBinding(header = "agents")
private List<Integer> agentIds;
// Getters & Setters
}
,,,
publicstaticvoidmain(字符串[]args)引发IOException{
CSVProcessor parser=新的CSVProcessor(Client.class);
String header=“ID,NAME,AGENTS\n”;
String data=“1,客户端,2 | 4 | 66”;
List List=parser.parse(新的StringReader(头+数据),CSVFormat.DEFAULT.withIgnoreHeaderCase());
StringWriter sw=新的StringWriter();
write(list、sw、CSVFormat.DEFAULT);
系统输出打印LN(软件);
}
@CSVReadComponent(type=CSVType.NAMED)
@CSVWriteComponent(type=CSVType.NAMED)
公共静态类客户端{
@CSVReadBinding(header=“id”)
@CSVWriteBinding(header=“id”)
私人长id;
@CSVReadBinding(header=“name”)
@CSVWriteBinding(header=“name”)
私有字符串名称;
@CSVReadBinding(header=“agents”)
@CSVWriteBinding(header=“agents”)
私人名单代理;
//接球手和接球手
}
,,,
很高兴看到有人在使用它
我是这项技术的开发者。这个用例解决起来还不错。您可以使用这样的内联包装器:
@CSVReadBinding(header = "agents", wrapper = AgentsReadWrapper.class)
@CSVWriteBinding(header = "agents", wrapper = AgentsWriteWrapper.class)
private List<Integer> agentIds;
public static class AgentsReadWrapper implements ReadWrapper<List<Integer>> {
@Override
public List<Integer> apply(String str) {
if(Objects.isNull(str) || str.trim().isEmpty()){
return new ArrayList<>();
}
return Arrays.stream(str.split("\\|"))
.map(String::trim)
.map(Integer::valueOf)
.collect(Collectors.toList());
}
}
public static class AgentsWriteWrapper implements WriteWrapper<List<Integer>>{
@Override
public String apply(List<Integer> str) {
StringJoiner sj = new StringJoiner("|");
str.stream().map(String::valueOf).forEach(sj::add);
return sj.toString();
}
}
@CSVReadBinding(header=“agents”,wrapper=agentsreadwapper.class)
@CSVWriteBinding(header=“agents”,wrapper=AgentsWriteWrapper.class)
私人名单代理;
公共静态类AgentsReadWrapper实现ReadWrapper{
@凌驾
公共列表应用(字符串str){
if(Objects.isNull(str)| | str.trim().isEmpty()){
返回新的ArrayList();
}
返回Arrays.stream(str.split(“\\\\”))
.map(字符串::trim)
.map(整数::valueOf)
.collect(Collectors.toList());
}
}
公共静态类AgentsWriteWrapper实现WriteWrapper{
@凌驾
公共字符串应用(列表str){
细木工sj=新细木工(“|”);
str.stream().map(String::valueOf).forEach(sj::add);
返回sj.toString();
}
}
如果您有任何问题,也可以在存储库中发布问题。我会尽快回答您的问题。我不熟悉该库,但我希望它能起作用:
String data=“1,client,\'2,4,66\”代码>或如果不是,则为:String data=“1,client,[2,4,66]”
或String data=“1,客户机,\”[2,4,66]\”代码>前几天我解决了它:D然而,这基本上就是我为你做的+1。谢谢你花时间帮助m8-非常感谢。没问题