java - 使用 SuperCSV ICsvBeanReader 解析枚举

标签 java csv enums supercsv

我解析 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/

相关文章:

csv - mdb-export不创建CSV文件

c# - 为什么 typeof(System.Enum).IsEnum = false?

java - 当 putifabsent 时间到期时做点什么 [infinispan]

java - Mockito:如何部分使用 @InjectMocks-Annotation 进行 MockMvc 测试?

java - 清除 Runnable 对象的 ArrayList 时会发生什么?

python - 使用python保存.xlsx文件时保持相同的文件格式

java - 无法将日期和时间转换为 LocalDateTime [Java]

python csv unicode 示例

Java:新枚举是旧枚举的子集

java - JAXB 中不区分大小写的 XMLEnumValue