title: How to serialize a List content to a flat JSON object with Jackson?
body: <p>Given the following POJOs ..</p>
  
  <pre><code>public class City {
  
      private String title;
      private List&lt;Person&gt; people;
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class Person {
  
      private String name;
      private int age;
  }
  </code></pre>
  
  <p>I would like to let <a href="https://github.com/FasterXML/jackson" rel="noreferrer">Jackson</a> serialize instances of the classes to the following <em>example</em> JSON:</p>
  
  <pre><code>{
      "title" : "New York",
      "personName_1" : "Jane Doe",
      "personAge_1" : 42,
      "personName_2" : "John Doe",
      "personAge_2" : 23 
  }
  </code></pre>
  
  <p>The JSON format is defined by an external API which I cannot change.</p>
  
  <p>I already found that I can annotate the list field with a custom serializer such as:</p>
  
  <pre><code>@JsonSerialize(using = PeopleSerializer.class)
  private List&lt;Person&gt; people;
  </code></pre>
  
  <p>... and here is a basic implementation I tried:</p>
  
  <pre><code>public class PeopleSerializer extends JsonSerializer&lt;List&lt;Person&gt;&gt; {
  
      private static final int START_INDEX = 1;
  
      @Override
      public void serialize(List&lt;Person&gt; people, 
                            JsonGenerator generator, 
                            SerializerProvider provider) throws IOException {
          for (int i = 0; i &lt; people.size(); ++i) {
              Person person = people.get(i);
              int index = i + START_INDEX;
              serialize(person, index, generator);
          }
      }
  
      private void serialize(Person person, int index, JsonGenerator generator) throws IOException {
          generator.writeStringField(getIndexedFieldName("personName", index), 
                                     person.getName());
          generator.writeNumberField(getIndexedFieldName("personAge", index), 
                                     person.getAge());
      }
  
      private String getIndexedFieldName(String fieldName, int index) {
          return fieldName + "_" + index;
      }
  
  }
  </code></pre>
  
  <p>However, this fails with an:</p>
  
  <pre><code>JsonGenerationException: Can not write a field name, expecting a value
  </code></pre>
  
  <p>I also looked into using Jackson's <a href="http://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/util/Converter.html" rel="noreferrer">Converter</a> interface but that's not suitable for unwrapping the nested list objects.</p>
  
  <ul>
  <li>See: <a href="https://stackoverflow.com/a/41651324/356895">https://stackoverflow.com/a/41651324/356895</a></li>
  </ul>
  
  <p>I am also aware of <code>@JsonUnwrapped</code> but it is not designed to be used with lists.</p>
  
  <h1>Related posts</h1>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/30805409/serialize-list-content-in-a-flat-structure-in-jackson-json-java">Serialize List content in a flat structure in jackson json (Java)</a></li>
  <li><a href="https://stackoverflow.com/questions/14714328/jackson-how-to-add-custom-property-to-the-json-without-modifying-the-pojo">Jackson: How to add custom property to the JSON without modifying the POJO</a></li>
  <li><a href="https://stackoverflow.com/questions/17542240/how-to-serialize-only-the-id-of-a-child-with-jackson">How to serialize only the ID of a child with Jackson</a></li>
  </ul>
  
  <h1>Related posts (deserialization)</h1>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/19580856/jackson-list-serialization-nested-lists">Jackson list deserialization. nested Lists</a></li>
  </ul>
  
  <h1>Related library</h1>
  
  <ul>
  <li><a href="https://github.com/jonpeterson/jackson-module-json-interceptor" rel="noreferrer">Jackson JSON Interceptor Module</a></li>
  </ul>