It depends how much of a purist you are. Let me describe the "typical"
and the type safe answer.
1. typical
class MyEntity {
@Value(type = StringOrIntegerType.java)
List<Object> data;
}
where the type produces Integer or String objects when unmarshalling
(you can use the visitor pattern on Json.Value) and Json.Number and
Json.String when marshalling (you'll need to use an instanceof on
java.lang.Integer and java.lang.String here).
2. type safe
abstract class StringOrInteger {}
class StringOfStringOrInteger extends StringOrInteger { String value; ...
class IntegerOfStringOrInteger extends StringOrInteger { Integer value; ...
and then
class MyEntity {
@Value(type = StringOrIntegerType.java)
List<StringOrInteger> data;
}
note the bound change in the parameterisation of data's List type.
The StringOrIntegerType would essentially do the same as the previous
one, but here you could be type safe in both cases because you would
be able to implement the visitor pattern in the StringOrInteger class.
I'd personally recommend 1 since it is saner and less verbose, but
really, you should rethink mixing types... It smells fishy to me.
PL
--
Play, Learn, Earn -
www.kaChing.com