1

Given two Classes

public class Inventory {
    private List<InventoryLine> lines;
}

and

public class InventoryLine {
    private String itemName;
    private int quantity;
    private String description;
}

I would like to print the following CSV file (using ',' as the delimiter but for visibility's sake I used spaces here):

Name    Quantity    Description
Book    4           A book
Watch   0           Gold watch
Phone   8           iPhone 8

Using the Conversion annotation and a class implementing the Conversion interface provided would it be possible to create the above CSV (by printing the Inventory class)? I see that they have provided examples with a List but they merge them into one column using a delimiter. I don't see any examples with this case.

user2827048
  • 539
  • 6
  • 18

2 Answers2

1

Author of the library here. The parser doesn't handle a hierarchical structure of classes. You can only write the list itself:

new CsvRoutines().writeAll(inventory.getLines(), InventoryLine.class, new File("/path/to/your.csv"), "UTF-8");

Hope this helps.

Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29
0

If you consider using other library.

try this one

maven dependency

   <dependency>
        <groupId>io.github.francisfernandez28</groupId>
        <artifactId>libraries</artifactId>
        <version>0.2.5-SNAPSHOT</version>
    </dependency>

Usage:

String csvString = CSVUtils.generateCSVStringFromListOfModel(inventory.getLines(),",");

then write

csvString

into your file.

francis
  • 1
  • 2
  • This doesn't solve his problem. He wants to write the instance of `Inventory` itself and let the library take care of any internally stored collections of objects. In his case there's only one, but it could very well be a complex hierarchy not suitable for the CSV format. My suggestion is pretty much the same as yours: write the result of `inventory.getLines()` – Jeronimo Backes Aug 11 '18 at 05:56