Menu

Indexed mapping for arrays (e. g. Double[])

Help
2013-10-28
2013-10-28
  • Andy Schönemann

    I try to use indexed mapping together with arrays and get an ArrayStoreException at the following example. What's wrong here? When I replace Double[] with List<Double> all is working fine.

    1) Bean:

    import java.util.Arrays;
    
    public class ArrayBean {
    
      Double[] array;
      public ArrayBean() {
        super();
      }
    
      public ArrayBean(Double[] array) {
        super();
        this.array = array;
      }
    
      public Double[] getArray() {
        return array;
      }
    
      public void setArray(Double[] array) {
        this.array = array;
      }
    
    }
    

    2) Main:

    import org.supercsv.cellprocessor.ParseDouble;
    import org.supercsv.cellprocessor.ift.CellProcessor;
    import org.supercsv.io.dozer.CsvDozerBeanReader;
    import org.supercsv.io.dozer.ICsvDozerBeanReader;
    import org.supercsv.prefs.CsvPreference;
    
    ublic class ArrayTest {
    
      private static final String[] FIELD_MAPPING = new String[] {
          "array[0]",
          "array[1]" };
    
      private static void readWithCsvDozerBeanReader() throws Exception {
    
        final CellProcessor[] processors = new CellProcessor[] {
            new ParseDouble(),
            new ParseDouble()
        };
    
        ICsvDozerBeanReader beanReader = null;
        try {
          beanReader =
              new CsvDozerBeanReader(new FileReader("src/test/resources/arrayTest.csv"),
                  CsvPreference.STANDARD_PREFERENCE);
    
          beanReader.getHeader(true); // ignore the header
          beanReader.configureBeanMapping(ArrayBean.class, FIELD_MAPPING);
    
          ArrayBean arrayBean;
          while ((arrayBean = beanReader.read(ArrayBean.class, processors)) != null) {
            System.out.println(String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(),
                beanReader.getRowNumber(), arrayBean));
          }
    
        } finally {
          if (beanReader != null) {
            beanReader.close();
          }
        }
      }
    
      public static void main(String[] args) throws Exception {
        readWithCsvDozerBeanReader();
      }
    
    }
    

    3) csv file:

    "array[0]","array[1]"
    1.0,2.0
    
     
  • James Bassett

    James Bassett - 2013-10-28

    Hi Andy,

    Thanks for the comprehensive explanation - it makes it so much easier :)

    This is a quirk with Dozer (see bug 39), where it needs a few hints to do the mapping.

    You'll notice that beanReader.configureBeanMapping() is overloaded and one method accepts an array of hint types - this is passed to Dozer as a hint to the type of each column.

    So you can simply set up the hints (both of your columns are Doubles):

    private static final Class<?>[] HINT_TYPES = 
        new Class<?>[]{Double.class, Double.class};
    

    And then pass those hints in when configuring the bean mapping:

    beanReader.configureBeanMapping(ArrayBean.class, 
        FIELD_MAPPING, HINT_TYPES);
    

    That should get you up and running again. Also, you probably don't want the bean mapping in the first row of your CSV file and in your code...