title: Jackson ObjectMapper - specify serialization order of object properties
body: <p>I'm implementing a RESTful web service where user has to send a signed verification token along with the request so that I could ensure that the request has not been tampered by a middle man. My current implementation is as follows.</p>
  
  <p>Verification token is a VerifData object serialized into a String and then hashed and encrypted.</p>
  
  <pre><code>class VerifData {
      int prop1;
      int prop2;
  }
  </code></pre>
  
  <p>In my service, I put data to be serialized into an instance of VerifData and then serialize it using Jackson ObjectMapper and passed along to the verification engine along with the verification token.</p>
  
  <pre><code>VerfiData verifData = new VerifData(12345, 67890);
  ObjectMapper mapper = new ObjectMapper();
  String verifCodeGenerated = mapper.writeValueAsString(verifData);
  </code></pre>
  
  <p>But it seems that each time the application container is started, the order of properties being mapped into a string by ObjectMapper changes.</p>
  
  <p>Ex: one time it would be</p>
  
  <pre><code>{"prop1":12345,"prop2":67890}
  </code></pre>
  
  <p>and another time it would be</p>
  
  <pre><code>{"prop2":67890,"prop1":12345}
  </code></pre>
  
  <p>So if client has serialized the VerifData instance as into the first String, there is 50% chance of it being failed even though it is correct.</p>
  
  <p>Is there a way to get around this? Can I specify the order of properties to map by ObjectMapper (like in ascending order)? Or is there any other way to best implement this verification step. Both client and server implementations are developed by me. I use Java Security API for signing and verifying.</p>