title: How to (De)serialize field from object based on annotation using Jackson?
body: <p>I need to configure Jackson in a specific way which I'll describe below.</p>
  
  <h1>Requirements</h1>
  
  <ol>
  <li>Annotated fields are serialized with only their id:
  
  <ul>
  <li>If the field is a normal object, serialize its <code>id</code></li>
  <li>If the field is a collection of objects, serialize an array of <code>id</code></li>
  </ul></li>
  <li>Annotated fields get their property names serialized differently:
  
  <ul>
  <li>If the field is a normal object, add <code>"_id"</code> suffix to property name</li>
  <li>If the field is a collection of objects, add <code>"_ids"</code> suffix to property name</li>
  </ul></li>
  <li>For the annotation I was thinking something like a custom <code>@JsonId</code>, ideally with an optional value to override the name just like <code>@JsonProperty</code> does</li>
  <li>The id property should be defined by the user, either using:
  
  <ul>
  <li>The already existing Jackson's <code>@JsonIdentityInfo</code></li>
  <li>Or by creating another class or field annotation</li>
  <li>Or by deciding which annotation to inspect for <code>id</code> property discoverability (useful for JPA scenarios, for example)</li>
  </ul></li>
  <li>Objects should be serialized with a wrapped root value</li>
  <li>Camel case naming should be converted to lower case with underscores</li>
  <li>All of this should be deserializable (by constructing an instance with just the id setted)</li>
  </ol>
  
  <h1>An example</h1>
  
  <p>Considering these POJO's:</p>
  
  <pre><code>//Inform Jackson which property is the id
  @JsonIdentityInfo(
      generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "id"
  )
  public abstract class BaseResource{
      protected Long id;
  
      //getters and setters
  }
  
  public class Resource extends BaseResource{
      private String name;
      @JsonId
      private SubResource subResource;
      @JsonId
      private List&lt;SubResource&gt; subResources;
  
      //getters and setters
  }
  
  public class SubResource extends BaseResource{
      private String value;
  
      //getters and setters
  }
  </code></pre>
  
  <p>A possible serialization of a <code>Resource</code> instance could be:</p>
  
  <pre><code>{
      "resource":{
          "id": 1,
          "name": "bla",
          "sub_resource_id": 2,
          "sub_resource_ids": [
              1,
              2,
              3
          ]
      }
  }
  </code></pre>
  
  <h1>So far...</h1>
  
  <ul>
  <li><p>Requirement <strong>#5</strong> can be accomplished by configuring <code>ObjectMapper</code> in the following way:</p>
  
  <pre><code>objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
  objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
  </code></pre>
  
  <p>And then using <code>@JsonRootName("example_root_name_here")</code> in my POJO's.</p></li>
  <li><p>Requirement <strong>#6</strong> can be accomplished by configuring <code>ObjectMapper</code> in the following way:</p>
  
  <pre><code>objectMapper.setPropertyNamingStrategy(
      PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
  </code></pre></li>
  </ul>
  
  <p>As you can see there are still lots of requirements to fulfill. For those wondering why I need such a configuration, it's because I'm developing a REST webservice for <a href="http://emberjs.com/" rel="noreferrer">ember.js</a> (more specifically Ember Data).
  You would appreciate very much if you could help with any of the requirements.</p>
  
  <p>Thanks!</p>