1

I'm trying to parse the following TSV data into a nested object but my "title" field is always null within the Nested class.

I've included the method at the bottom which converts the TSV data to the object.

value1 | metaData1 | valueA |
value2 | metaData2 | valueB |
value3 | metaData3 | valueC |

public class Data {
    @Parsed(index = 0)
    private String value0;

    @Parsed(index = 1)
    private String foo;

    @Nested
    MetaData metaData;

    public static class MetaData {
        @Parsed(index = 1)
        private String title;
    }
}

public <T> List<T> convertFileToData(File file, Class<T> clazz, boolean removeHeader) {

    BeanListProcessor<T> rowProcessor = new BeanListProcessor<>(clazz);
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setDelimiter('|');
    settings.setProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(removeHeader);

    CsvParser parser = new CsvParser(settings);
    parser.parseAll(file);

    return rowProcessor.getBeans();
}
Ilyas Patel
  • 400
  • 2
  • 11
  • 27

1 Answers1

0

You forgot to define an index on your Metadata.title:

public static class MetaData {
    @Parsed(index=1)
    private String title;
}

Also, you are setting the delimiter to \t while your input is using | as the separator.

Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29
  • When I use index=1, then I it works, I get a value in the title attribute, however, when I use index=2 then I get null which is odd. I've updated the question to remove the \t, that was a typo. – Ilyas Patel Aug 30 '17 at 15:13
  • I tried setting index = 2 in `Metadata.title` and it worked. Are you using the latest version? – Jeronimo Backes Aug 30 '17 at 15:28
  • I've updated to the latest version and it is working. thanks! One thing I had to do to make it work though is I had to add the index I wanted to use to the nested class in the outer class first. I've updated the original question so you can see this -- look at attribute "foo". Is this how it is suppose to work? – Ilyas Patel Aug 30 '17 at 16:18
  • If I take attribute foo out, then I get null in the title attribute in the inner class. – Ilyas Patel Aug 30 '17 at 16:27