首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SuperCSV ICsvBeanReader解析枚举

使用SuperCSV ICsvBeanReader解析枚举
EN

Stack Overflow用户
提问于 2012-01-27 21:13:25
回答 3查看 3.1K关注 0票数 5

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

代码语言:javascript
复制
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;
}
}

我的枚举看起来像这样:

代码语言:javascript
复制
public enum Type {

    CANCEL, REFUND
}

正在尝试从此CSV文件创建bean:

代码语言:javascript
复制
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违规处理器:在org.supercsv.io.CsvBeanReader.read(未知源)的org.supercsv.io.CsvBeanReader.fillObject(Unknown源处为null

关于解析枚举有什么建议吗?我应该为此编写自己的处理器吗?

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

代码语言:javascript
复制
class MyCellProcessor extends CellProcessorAdaptor {
    public Object execute(Object value, CSVContext context) {
        Type type = Type.valueOf(value.toString());
        return next.execute(type, context);
    }
}

但它会以同样的异常消亡。

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

取消

退款

EN

回答 3

Stack Overflow用户

发布于 2012-02-21 21:03:04

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

Super CSV依赖于这样一个事实,即您应该提供一个有效的Java bean,即一个具有默认构造函数和每个字段的公共getter/setter的类。

因此,您可以通过将以下代码添加到TypeWithEnum来修复该异常

代码语言:javascript
复制
public TypeWithEnum(){
}

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

1.使用HashMapper处理器

代码语言:javascript
复制
@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

创建您的处理器

代码语言:javascript
复制
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);
    }

}

使用它!

代码语言:javascript
复制
@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的人来说,也许还需要一个可用的处理器的描述。

票数 7
EN

Stack Overflow用户

发布于 2013-11-15 09:15:48

下面是一个用于枚举的通用单元处理器

代码语言:javascript
复制
/** A cell processor to convert strings to enums. */
public class EnumCellProcessor<T extends Enum<T>> implements CellProcessor {

    private Class<T> enumClass;
    private boolean ignoreCase;

    /**
     * @param enumClass the enum class used for conversion
     */
    public EnumCellProcessor(Class<T> enumClass) {
        this.enumClass = enumClass;
    }

    /**
     * @param enumClass the enum class used for conversion
     * @param ignoreCase if true, the conversion is made case insensitive
     */
    public EnumCellProcessor(Class<T> enumClass, boolean ignoreCase) {
        this.enumClass = enumClass;
        this.ignoreCase = ignoreCase;
    }

    @Override
    public Object execute(Object value, CsvContext context) {
        if (value == null)
            return null;

        String valueAsStr = value.toString();

        for (T s : enumClass.getEnumConstants()) {
            if (ignoreCase ? s.name().equalsIgnoreCase(valueAsStr) : s.name().equals(valueAsStr)) {
                return s;
            }
        }

        throw new SuperCsvCellProcessorException(valueAsStr + " cannot be converted to enum " + enumClass.getName(), context, this);
    }

}

你会用到它

代码语言:javascript
复制
new EnumCellProcessor<Type>(Type.class);
票数 2
EN

Stack Overflow用户

发布于 2012-01-27 21:21:22

我试着重现你的错误,但对我来说一切正常。我使用SuperCSV 1.52:

代码语言:javascript
复制
  private enum ENUMS_VALUES{TEST1, TEST2, TEST3};
  @Test
  public void testEnum3() throws IOException
  {
    String testInput = new String("TEST1\nTEST2\nTEST3");
    ICsvBeanReader  reader = new CsvBeanReader(new StringReader(testInput), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
    final String[] header = new String[] {"header"};
    reader.read(this.getClass(), header, new CellProcessor[] {new CellProcessorAdaptor() {

      @Override
      public Object execute(Object pValue, CSVContext pContext)
      {
        return next.execute(ENUMS_VALUES.valueOf((String)pValue), pContext);
      }}});

  }

  @Test
  public void testEnum4() throws IOException
  {
    String testInput = new String("TEST1\nTEST2\nTEST3");
    ICsvBeanReader reader = new CsvBeanReader(new StringReader(testInput), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
    final String[] header = new String[] {"header"};
    reader.read(this.getClass(), header, new CellProcessor[] {new CellProcessorAdaptor()
    {

      @Override
      public Object execute(Object pValue, CSVContext pContext)
      {
        return ENUMS_VALUES.valueOf((String)pValue);
      }}});
  }

  public void setHeader(ENUMS_VALUES value)
  {
    System.out.println(value);
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9033756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档