title: How to serialize nested collection of objects with Jackson
body: <p>Having the following code:</p>
  
  <pre><code>public class ToSerialize {
    List&lt;NestedObject&gt; objs;
  
    public ToSerialize(List&lt;NestedObject&gt; objs) {
      this.objs = objs;
    }
  }
  
  public class NestedObject {
    int intValue = 0;
    String strValue = "Hello world";
  }
  </code></pre>
  
  <p>How, if possible, do I setup Jackson so I get the following CSV as String output:</p>
  
  <pre><code>obj1.intValue,obj1.strValue,obj2.intValue,obj2.strValue,0,"Hello world",0,"Hello world"
  </code></pre>
  
  <p>I looked into writing my own custom <code>JsonSerializer</code> but can't figure it out. Still, here's my current not working serializer implementation</p>
  
  <pre><code>  @Override
    public void serialize(List&lt;NestedObject&gt; values, JsonGenerator gen, SerializerProvider serializers)
      throws IOException
    {
      int i = 1;
      for (Iterator&lt;VsppAssetEntry&gt; iterator = values.iterator(); iterator.hasNext(); ) {
        VsppAssetEntry entry = iterator.next();
        String fieldName = "obj" + i;
        gen.writeObjectField(fieldName, entry);
        i++;
      }
  </code></pre>
  
  <p>this is throwing <code>com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value</code> obviously because Jackson wrote a fieldName <code>objs</code> before entering this method.</p>