title: Jackson - serialize/deserialize property as JSON value
body: <p>Using Jackson 2, I'm looking for a <strong>generic</strong> way to be serialize objects as a single value (then serialize them back later populating only that single field) without having to repetitively create a JsonSerializer / JsonDeserializer to handle each case. The @JsonIdentityInfo annotation comes pretty close but misses the mark a little bit since, as far as I can tell, it will always serialize the full child object on the first occurrence. </p>
  
  <p>Here is an example of what I want to do. Given the classes:</p>
  
  <pre><code>class Customer {
  
      Long id = 99;
      String name = "Joe"; 
      // Lots more properties
  }
  
  class Order {
      String orderNum = "11111"
      @WhateverAnnotationsINeedHereToDoWhatIWant
      Customer customer;
  }
  </code></pre>
  
  <p>I would like Order to serialize as either (either would be perfectly acceptable to me):</p>
  
  <pre><code>{"orderNum":"11111", "customer":"99"}
  {"orderNum":"11111", "customer":{"id":99}}
  </code></pre>
  
  <p>What @JsonIdentityInfo does makes it more difficult to deal with on the client-side (we can assume that the client knows how to map the customer ID back into the full customer information).</p>
  
  <p>@JsonIgnoreProperties could also come pretty close for the second JSON shown but would mean I would have to opt-out of <strong>everything</strong> but the one I want.</p>
  
  <p>When I deserialize back I would just want the Customer to be initialzed with the "id" field only.</p>
  
  <p>Any magic way to do this that I'm missing without getting into the soupy internals of Jackson? As far as I can tell, JsonDeserializer/JsonSerializer has no contextual information on the parent so there doesn't seem to be an easy way to create a @JsonValueFromProperty("id") type Jackson Mix-in then just look at that annotation in the custom Serializer/Deserializer.</p>
  
  <p>Any ideas would be greatly appreciated. Thanks!</p>