Search code examples
csvdozersupercsv

SuperCSV with Dozer - How to map to a List of Lists?


Using SuperCSV with Dozer (version 2.2.0), I've been able to successfully map data into a List. However, I now have a case where I want to map into a List of Lists. In the object I'm trying to map to, I have a field that looks something like this:

List<List<String>> myListOfLists;

For my field mapping I did this:

"myListOfLists[0][0]", "myListOfLists[0][1]", "myListOfLists[1][0]", etc.

However, this results in the following error:

org.dozer.MappingException: No read or write method found for field (myListOfLists[0]) in class (class com.foo.MyClassBeingMappedTo)

I can't seem to find any examples of nested collections, but it seems like this should be possible. Is there a way I can map the data into my List<List<String>> field?


Solution

  • I got around this by creating a wrapper class for my inner List. I created an inner class like this:

    public static class InnerListWrapper
    {
        private List<String> innerList;
    
        public List<String> getInnerList()
        {
            return innerList;
        }
        public void setInnerList(List<String> innerList)
        {
            this.innerList = innerList;
        }
    }
    

    Then my outer List looks like this:

    List<InnerListWrapper> myListOfLists;
    

    Then the fields mapping is simply "myListOfLists[0].innerList[0]","myListOfLists[0].innerList[1]", ""myListOfLists[1].innerList[0]", etc.

    Not the cleanest, but like the comment above says, Dozer doesn't seem to support nested Lists. So you have to add another class in between.