Identifies a class as being a surrogate class.
Surrogate classes are used in place of other classes during serialization.
For example, you may want to use a surrogate class to change the names or order of bean properties on a bean.
This interface has no methods to implement.
It's simply used by the framework to identify the class as a surrogate class when specified as a swap.
The following is an example of a surrogate class change changes a property name:
public class MySurrogate implements Surrogate {
// Public constructor that wraps the normal object during serialization.
public MySurrogate(NormalClass o) {...}
// Public no-arg constructor using during parsing.
// Not required if only used during serializing.
public MySurrogate() {...}
// Public method that converts surrogate back into normal object during parsing.
// The method name can be anything (e.g. "build", "create", etc...).
// Not required if only used during serializing.
public NormalClass unswap() {...}
}
Surrogate classes are associated with serializers and parsers using the BeanContextBuilder.pojoSwaps(Class...)
method.
JsonSerializer s = JsonSerializer
.create()
.pojoSwaps(MySurrogate.class)
.build();
JsonParser p = JsonParser
.create()
.pojoSwaps(MySurrogate.class)
.build();
Surrogates can also be associated using the
@Swap
annotation.
@Swap(MySurrogate.class)
public class NormalClass {...}
On a side note, a surrogate class is functionally equivalent to the following PojoSwap
implementation:
public class MySurrogate extends PojoSwap<NormalClass,MySurrogate> {
public MySurrogate swap(NormalClass o) throws SerializeException {
return new MySurrogate(o);
}
public NormalClass unswap(MySurrogate o, ClassMeta<?> hint) throws ParseException {
return o.unswap();
}
}