gpt4 book ai didi

java - 使用 SuperCSV ICsvBeanReader 解析枚举

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:48 25 4
gpt4 key购买 nike

我解析 CSV 文件并使用 supercsv 创建域对象.我的域对象有一个枚举字段,例如:

public class TypeWithEnum {

private Type type;

public TypeWithEnum(Type type) {
this.type = type;
}

public Type getType() {
return type;
}

public void setType(Type type) {
this.type = type;
}
}

我的枚举看起来像这样:

public enum Type {

CANCEL, REFUND
}

尝试从此 CSV 文件创建 bean:

final String[] header = new String[]{ "type"  };
ICsvBeanReader inFile = new CsvBeanReader(new FileReader(
getFilePath(this.getClass(), "learning/enums.csv")), CsvPreference.STANDARD_PREFERENCE);

final CellProcessor[] processors =
new CellProcessor[]{ TODO WHAT TO PUT HERE? };
TypeWithEnum myEnum = inFile.read(
TypeWithEnum.class, header, processors);

这失败了填充对象上下文时出错:null 违规处理器:null 在 org.supercsv.io.CsvBeanReader.fillObject(未知来源) 在 org.supercsv.io.CsvBeanReader.read(未知来源)

关于解析枚举的任何提示?我应该为此编写自己的处理器吗?

我已经尝试编写自己的处理器,像这样:

class MyCellProcessor extends CellProcessorAdaptor {
public Object execute(Object value, CSVContext context) {
Type type = Type.valueOf(value.toString());
return next.execute(type, context);
}
}

但它死于相同的异常。

我的 enums.csv 文件的内容很简单:

取消
退款

最佳答案

您遇到的异常是因为 CsvBeanReader 无法实例化您的 TypeWithEnum 类,因为它没有默认(无参数)构造函数。打印堆栈跟踪可能是个好主意,这样您就可以看到出错的全部详细信息。

super CSV 依赖于您应该提供有效的 Java bean 的事实,即具有默认构造函数和每个字段的公共(public) getters/setters 的类。

因此,您可以通过将以下内容添加到 TypeWithEnum 来修复异常:

public TypeWithEnum(){
}

至于解析枚举的提示,两个最简单的选项是:

<强>1。使用 HashMapper 处理器

@Test
public void hashMapperTest() throws Exception {

// two lines of input
String input = "CANCEL\nREFUND";

// you could also put the header in the CSV file
// and use inFile.getCSVHeader(true)
final String[] header = new String[] { "type" };

// map from enum name to enum
final Map<Object, Object> typeMap = new HashMap<Object, Object>();
for( Type t : Type.values() ) {
typeMap.put(t.name(), t);
}

// HashMapper will convert from the enum name to the enum
final CellProcessor[] processors =
new CellProcessor[] { new HashMapper(typeMap) };

ICsvBeanReader inFile =
new CsvBeanReader(new StringReader(input),
CsvPreference.STANDARD_PREFERENCE);

TypeWithEnum myEnum;
while((myEnum = inFile.read(TypeWithEnum.class, header, processors)) !=null){
System.out.println(myEnum.getType());
}

}

<强>2。创建自定义 CellProcessor

创建你的处理器

package org.supercsv;

import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.exception.SuperCSVException;
import org.supercsv.util.CSVContext;

public class TypeProcessor extends CellProcessorAdaptor {

public TypeProcessor() {
super();
}

public TypeProcessor(CellProcessor next) {
super(next);
}

public Object execute(Object value, CSVContext context) {

if (!(value instanceof String)){
throw new SuperCSVException("input should be a String!");
}

// parse the String to a Type
Type type = Type.valueOf((String) value);

// execute the next processor in the chain
return next.execute(type, context);
}

}

使用它!

@Test
public void customProcessorTest() throws Exception {

// two lines of input
String input = "CANCEL\nREFUND";

final String[] header = new String[] { "type" };

// HashMapper will convert from the enum name to the enum
final CellProcessor[] processors =
new CellProcessor[] { new TypeProcessor() };

ICsvBeanReader inFile =
new CsvBeanReader(new StringReader(input),
CsvPreference.STANDARD_PREFERENCE);
TypeWithEnum myEnum;
while((myEnum = inFile.read(TypeWithEnum.class, header, processors)) !=null){
System.out.println(myEnum.getType());
}

}

我正在开发即将发布的 Super CSV。我一定会更新网站,明确说明您必须有一个有效的 Java bean - 可能还有可用处理器的描述,以供那些不喜欢阅读 Javadoc 的人使用。

关于java - 使用 SuperCSV ICsvBeanReader 解析枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9033756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com