title: Dynamic JSON serialization based on the property value
body: <p>I am implementing a REST server using Spring Boot, JAXB2 and Jackson. This server supports JSON and XML and it is based on official specifications.</p>
  
  <p>I am currently having a serialization problem specific to the JSON format and I do not know how it can be solved?</p>
  
  <p>The specification defines several primitive types as string, integer, etc. that can be extended and depending of this extension, the result of the serialization is not the same.</p>
  
  <p>The Java classes</p>
  
  <pre><code>public class PrimitiveType {
  
      @XmlAttribute
      private String id
      @XmlElement(name = "extension")
      private List&lt;Extension&gt; extensions = new ArrayList&lt;&gt;();
  
      // Getters and Setters
  }
  
  public class StringType extends PrimitiveType {
  
      @XmlAttribute
      private String value;
  
      // Getter and Setter
  }
  </code></pre>
  
  <p>JSON</p>
  
  <pre><code>// Without id and/or extension(s)
  "code" : "abc"
  
  // With id and/or extension(s)
  "code ": "abc",
  "_code": {
      "id": "1",
      "extension" : [ {
          "url" : "http://mydomain/ext/1",
          "valueString" : "abc-1"
      }]
  }
  </code></pre>
  
  <p>I do not have any problem with the XML but it is not the same for the JSON. I do not know how I can add a property dynamically on the same level. I had a look on the JsonSerializer but it seems that it allows to change the serialization on the object itself.</p>
  
  <p>Does anyone had a chance to do this kind of things before? </p>