title: Custom JSON Deserialization with Jackson
body: <p>I'm using the <a href="http://www.flickr.com/services/api/" rel="noreferrer">Flickr API</a>. When calling the <a href="http://www.flickr.com/services/api/flickr.test.login.html" rel="noreferrer"><code>flickr.test.login</code></a> method, the default JSON result is:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "user": {
          "id": "21207597@N07",
          "username": {
              "_content": "jamalfanaian"
          }
      },
      "stat": "ok"
  }
  </code></pre>
  
  <p>I'd like to parse this response into a Java object:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class FlickrAccount {
      private String id;
      private String username;
      // ... getter &amp; setter ...
  }
  </code></pre>
  
  <p>The JSON properties should be mapped like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>"user" -&gt; "id" ==&gt; FlickrAccount.id
  "user" -&gt; "username" -&gt; "_content" ==&gt; FlickrAccount.username
  </code></pre>
  
  <p>Unfortunately, I'm not able to find a nice, elegant way to do this using Annotations. My approach so far is, to read the JSON String into a <code>Map&lt;String, Object&gt;</code> and get the values from there.</p>
  
  <pre class="lang-java prettyprint-override"><code>Map&lt;String, Object&gt; value = new ObjectMapper().readValue(response.getStream(),
          new TypeReference&lt;HashMap&lt;String, Object&gt;&gt;() {
          });
  @SuppressWarnings( "unchecked" )
  Map&lt;String, Object&gt; user = (Map&lt;String, Object&gt;) value.get("user");
  String id = (String) user.get("id");
  @SuppressWarnings( "unchecked" )
  String username = (String) ((Map&lt;String, Object&gt;) user.get("username")).get("_content");
  FlickrAccount account = new FlickrAccount();
  account.setId(id);
  account.setUsername(username);
  </code></pre>
  
  <p>But I think, this is the most non-elegant way, ever. Is there any simple way, either using Annotations or a custom Deserializer?</p>
  
  <p>This would be very obvious for me, but of course it doesn't work:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class FlickrAccount {
      @JsonProperty( "user.id" ) private String id;
      @JsonProperty( "user.username._content" ) private String username;
      // ... getter and setter ...
  }
  </code></pre>