title: Umarshall JSON object with underscore and camelCase fields between Jackson and JAXB
body: <p>Using JAXB (2.2) and Jackson (1.9.13), I have trouble unmarshalling the following JSON object to my POJOs</p>
  
  <pre><code>{
     "userId": "foo",
     "group_id": "bar"
  }
  </code></pre>
  
  <p>Note that the payload contains both a camelCase and an underscore field.</p>
  
  <p>The POJO generated by xjc for my XML schema is as follows:</p>
  
  <pre><code>public class User {
      @XmlElement(required = true)
      protected String userId;
      @XmlElement(name = "group_id", required = true)
      protected String groupId;
  
      public String getUserId() { return userId; }       
      public void setUserId(String value) { this.userId = value; }
      public String getGroupId() { return groupId; }
      public void setGroupId(String value) { this.groupId = value; }
  }
  </code></pre>
  
  <p>Jackson fails with the following exception: </p>
  
  <pre><code>org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "group_id"
  </code></pre>
  
  <h2>What I tried so far and failed</h2>
  
  <h3>1. Use JAXB binding <code>underscoreBinding="asCharInWord"</code></h3>
  
  <p>Using the following JXB binding in my XML schema </p>
  
  <pre><code>&lt;jxb:globalBindings underscoreBinding="asCharInWord"/&gt;
  </code></pre>
  
  <p>generates the following POJO:</p>
  
  <pre><code>public class User {
      @XmlElement(required = true)
      protected String userId;
      @XmlElement(name = "group_id", required = true)
      protected String groupId;
  
      public String getUserId() { return userId; }
      public void setUserId(String value) { this.userId = value; }
      public String getGroup_Id() { return groupId; }
      public void setGroup_Id(String value) { this.groupId = value; }
  }
  </code></pre>
  
  <p>Note that JAXB now generated setters/getters with underscore for group IDs but the <code>group_id</code> field is still converted to CamelCase. Jackson's object mapper seems to ignore the property getters/setter names and still can't map <code>group_id</code> to <code>groupId</code>.</p>
  
  <h3>2. Jackson property naming strategy</h3>
  
  <p>Using Jackson's <a href="http://wiki.fasterxml.com/JacksonFeaturePropertyNamingStrategy" rel="nofollow noreferrer">PropertyNamingStrategy</a> <code>CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES</code> I can convert <code>group_id</code> to <code>groupId</code>, but now the object mapper fails on the <code>userId</code> JSON property.</p>
  
  <h3>3. Add a <code>JSONProperty</code> annotation to groupId</h3>
  
  <p>Adding a <code>JSONProperty</code> to the vanilla JAXB generated POJOs actually works   </p>
  
  <pre><code>public class User {
          /* ... */
          @XmlElement(name = "group_id", required = true)
          @JSONProperty("group_id")
          protected String groupId;
          /* ... */
     }
  </code></pre>
  
  <p>However, my XML schema is huge and manual instrumentation of the generated classes is not feasible, since we generate our classes often.</p>
  
  <h2>What should I do?</h2>
  
  <p>I see the following two remaining options to handle this problem:</p>
  
  <ol>
  <li>Implement a JAXB plugin that adds a <code>JSONProperty</code> annotation to each <code>XMLElement</code> with underscore names (my preferred next approach)</li>
  <li>Use a custom name generator for XJC as outlined in <a href="https://stackoverflow.com/questions/6267478/how-to-disable-java-naming-conventions-in-xjc">this</a> Stackoverflow answer.</li>
  </ol>
  
  <p>Have I missed the obvious?
  thanks for your thoughts.</p>