Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Jackson Serializers of Same Return Type
    primarykey
    data
    text
    <p>I am using Jackson for JSON serialization and have written a couple custom <code>String</code> serializers, one for each getter method of a class. The methods each return the same type, <code>Set&lt;String&gt;</code>, but each uses a different serializer.</p> <p>Unfortunately, Jackson is not using each serializer, one for each method, but is using one serializer for both. It appears to take whatever method comes first alphabetically and uses its serializer for both methods. What I expect is that the serializer annotated on the first method is used for the first method, and the serializer annotated on the second method is used on the second method. Debugging appears to indicate that Jackson has the serializers in a map keyed by return type of the method (which is the same for both).</p> <p><em>An Example:</em></p> <pre><code>public class FooBar { private Set&lt;String&gt; foos = new HashSet&lt;String&gt;(); private Set&lt;String&gt; bars = new HashSet&lt;String&gt;(); @JsonProperty("FooWrapper") @JsonSerialize(contentUsing = FooSerializer.class) public Set&lt;String&gt; getFoos() { return foos; } @JsonProperty("BarWrapper") @JsonSerialize(contentUsing = BarSerializer.class) public Set&lt;String&gt; getBars() { return bars; } } </code></pre> <p>Any suggestions on how to get the <code>getFoos()</code> method to serialize with a <code>FooSerializer</code>, and the <code>getBars()</code> method to serialize with the <code>BarSerializer</code>? In this example, the <code>BarSerializer</code> is invoked for both methods.</p> <p>Note, if I change the signature of one of the methods to another collection type so they are different - <code>List&lt;String&gt;</code> for example - the serialization works.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload