title: How do I serialize & deserialize CSV properly?
body: <p>I've been trying to serialize an object to a CSV <code>String</code> but the object contains a <code>List</code> and <code>@JsonUnwrapped</code> doesn't work on <code>List</code> objects.</p>
  
  <p><strong>Expected sample output</strong>:</p>
  
  <pre><code>color,part.name\n
  red,gearbox\n
  red,door\n
  red,bumper
  </code></pre>
  
  <p><strong>Actual output</strong>:</p>
  
  <pre><code>com.fasterxml.jackson.core.JsonGenerationException: Unrecognized column 'name':
  </code></pre>
  
  <p><strong>Here is my code</strong>: (Most of it is the 2 POJO's)</p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.annotation.JsonFormat;
  import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  import com.fasterxml.jackson.annotation.JsonRootName;
  import com.fasterxml.jackson.dataformat.csv.CsvMapper;
  import com.fasterxml.jackson.dataformat.csv.CsvSchema;
  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
  import java.io.IOException;
  import static java.util.Arrays.asList;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class NestedWrapping {
  
  @JsonRootName("Car")
  @JsonInclude(JsonInclude.Include.NON_DEFAULT)
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
  @JsonPropertyOrder({"color"})
  public static class Car {
  
      @JsonProperty("color")
      private String color;
  
      @JsonFormat(shape = JsonFormat.Shape.STRING)
      @JacksonXmlElementWrapper(useWrapping = false)
      private List&lt;Part&gt; parts;
  
      public String getColor() {
          return color;
      }
  
      public void setColor(String color) {
          this.color = color;
      }
  
      public List&lt;Part&gt; getParts() {
          return parts;
      }
  
      public void setParts(List&lt;Part&gt; parts) {
          this.parts = parts;
      }
  
  }
  
  @JsonInclude(JsonInclude.Include.NON_DEFAULT)
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
  @JsonPropertyOrder({
      "name"
  })
  public static class Part {
  
      @JsonProperty("name")
      private String name;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  
  public static void main(String args[]) {
      try {
          Car car = new Car();
          car.setColor("red");
          Part part1 = new Part();
          part1.setName("geabox");
          Part part2 = new Part();
          part2.setName("door");
          Part part3 = new Part();
          part3.setName("bumper");
          car.setParts(asList(part1, part2, part3));
          System.out.println("serialized: " + serialize(car, Car.class, true));
      } catch (IOException ex) {
          Logger.getLogger(NestedWrapping.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  public static final synchronized String serialize(final Object object, final Class type, final Boolean withHeaders) throws IOException {
      CsvMapper csvMapper = new CsvMapper();
      CsvSchema csvSchema;
      if (withHeaders) {
          csvSchema = csvMapper.schemaFor(type).withHeader();
      } else {
          csvSchema = csvMapper.schemaFor(type).withoutHeader();
      }
      return csvMapper.writer(csvSchema).writeValueAsString(object);
  }
  
  }
  </code></pre>
  
  <p>Nothing I try seems to work, I've read every <a href="https://stackoverflow.com/questions/13386930/why-doesnt-jsonunwrapped-work-for-lists">post</a> on stackoverflow and github about the topic but I can't find a working solution.</p>
  
  <p>Sorry about any pointless annotations that I've left behind for no reason and if you answer with code, please feel free to remove them.</p>