I have a class that implements java.util.Set
. For JSON serialization however, I want to represent instances of this class as a JSON object where elements of the set are represented by a custom String
encoding, which is why the class has a synthetic property elements
(i.e., there is no real attribute, the value is always computed on the fly).
public class MySet implements Set<...> {
public String getElements() {
// Return String encoding of the set's elements.
}
public void setElements(String encoding) {
// Parse encoding and set elements of the set accordingly.
}
// A few more properties also accessible by getters/setters.
}
The class has a few more properties, also accessible by getters/setters. They should also be JSON-serialized. All in all, all properties should be serialized as key/value pairs of a JSON object.
Intended JSON serialization:
{
"elements": "...encoding...",
// other properties as key/value pairs
}
In this regard, the class can be seen as a POJO. In fact, this is what I was assuming Jackson to consider; i.e., that Jackson would ignore the aspect that the class is also a Set
. However, Jackson seems to prefer that the class is a Set and serializes it as such; i.e., an array JSON structure.
Is it possible to achieve this kind of de-/serializion just using @Json...
annotations without the need to implement a custom de-/serializer?