Json version of XMLElements choice : <p>For a Java code with the following annotation:</p>

<pre><code>@JsonProperty(value="Contact")
    @NotNull(message = "ContactUser or CompanyName is required.")
    @Valid
    @XmlElements(value = {
            @XmlElement(name = "ContactUser", type = ContactUser.class, required = true),
            @XmlElement(name = "CompanyName", type = String.class, required = true) })  
    private Object contactInfo;
</code></pre>

<p>Result set when I use the object for GET is :</p>

<pre><code>"Contact":{
"ContactUser":{
"Title": "Miss",
"LastName": "Hello"
}
}
</code></pre>

<p>or </p>

<pre><code>"Contact": "Hello Company"
</code></pre>

<p>Is there a way so it returns:</p>

<pre><code>"ContactUser":{
"Title": "Miss",
"LastName": "Hello"
} 
</code></pre>

<p>or </p>

<pre><code>"CompanyName": "Hello Company"
</code></pre>

<p>instead? In xml, with the code, you can do:</p>

<pre><code> &lt;CompanyName&gt;Hello Company&lt;/CompanyName&gt;
</code></pre>

<p>or </p>

<pre><code>&lt;ContactUser&gt;
        &lt;Title&gt;Miss&lt;/Title&gt;
        &lt;LastName&gt;Hello&lt;/LastName&gt;
         &lt;/ContactUser&gt;
</code></pre>

<p>I have tried using JsonTypeInfo but it doesnt seem to handle String.class:</p>

<pre><code> @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =JsonTypeInfo.As.WRAPPER_OBJECT, visible=true)
    @JsonSubTypes({
        @JsonSubTypes.Type(name = "ContactUserJ", value = ContactUser.class),
        @JsonSubTypes.Type(name = "CompanyNameJ" , value = String.class)
    })
</code></pre>
