泛型
Jackson 中泛型处理
在序列化和反序列化的过程中,泛型是永远离不开的主题,泛型的类型参考如下:
index | Name | Example |
---|---|---|
1 | ParameterizedType | 参数化类型,即泛型;例如:List、 |
2 | TypeVariable | 类型变量,即泛型中的变量;例如:T、K、 |
3 | GenericArrayType | 泛型数组类型,用来描述 |
4 | Class | |
5 | WildcardType | 泛型表达式(或者通配符表达式 |
类型
public <T> T readValue(JsonParser p, Class<T> valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), p, _typeFactory.constructType(valueType));
}
@Override
@SuppressWarnings("unchecked")
public <T> T readValue(JsonParser p, TypeReference<?> valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), p, _typeFactory.constructType(valueTypeRef));
}
@Override
@SuppressWarnings("unchecked")
public final <T> T readValue(JsonParser p, ResolvedType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), p, (JavaType) valueType);
}
public <T> T readValue(JsonParser p, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), p, valueType);
}
最长接受的参数是:Class、JavaType、TypeReference。
JavaType
在
Base class for type token classes used both to contain information and as keys for deserializers.
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory.
Since 2.2 this implements {@link java.lang.reflect.Type} to allow
it to be pushed through interfaces that only expose that type.
用于包含信息和作为反序列化器的键的类型标记类的基类。只能通过
// 1. 通过objectMapper.construct
JavaType javaType = JacksonConstant.OM.constructType(type);
// 2. 通过TypeProvier
JavaType javaType1 = TypeFactory.defaultInstance().constructType(type);
其实方式
public JavaType constructType(Type t) {
//本质还是通过TypeFactory来实现的
return _typeFactory.constructType(t);
}
那么
protected JavaType _fromAny(ClassStack context, Type type, TypeBindings bindings)
{
JavaType resultType;
// simple class?
if (type instanceof Class<?>) {
// Important: remove possible bindings since this is type-erased thingy
resultType = _fromClass(context, (Class<?>) type, EMPTY_BINDINGS);
}
// But if not, need to start resolving.
else if (type instanceof ParameterizedType) {
resultType = _fromParamType(context, (ParameterizedType) type, bindings);
}
else if (type instanceof JavaType) { // [databind#116]
// no need to modify further if we already had JavaType
return (JavaType) type;
}
else if (type instanceof GenericArrayType) {
resultType = _fromArrayType(context, (GenericArrayType) type, bindings);
}
else if (type instanceof TypeVariable<?>) {
resultType = _fromVariable(context, (TypeVariable<?>) type, bindings);
}
else if (type instanceof WildcardType) {
resultType = _fromWildcard(context, (WildcardType) type, bindings);
} else {
// sanity check
throw new IllegalArgumentException("Unrecognized Type: "+((type == null) ? "[null]" : type.toString()));
}
// 21-Feb-2016, nateB/tatu: as per [databind#1129] (applied for 2.7.2),
// we do need to let all kinds of types to be refined, esp. for Scala module.
if (_modifiers != null) {
TypeBindings b = resultType.getBindings();
if (b == null) {
b = EMPTY_BINDINGS;
}
for (TypeModifier mod : _modifiers) {
JavaType t = mod.modifyType(resultType, type, b, this);
if (t == null) {
throw new IllegalStateException(String.format(
"TypeModifier %s (of type %s) return null for type %s",
mod, mod.getClass().getName(), resultType));
}
resultType = t;
}
}
return resultType;
}
TypeReference
public abstract class TypeReference<T> implements Comparable<TypeReference<T>>
{
protected final Type _type;
protected TypeReference()
{
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) { // sanity check, should never happen
throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
}
_type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}
public Type getType() { return _type; }
}
通用的使用方式是:
Map<String, Staff> json2Map = JacksonConstant.OM.readValue(staffMapJson, new TypeReference<Map<String, Staff>>() {
});
构建一个内部匿名类,名字是运行类下的
getClass().getGenericSuperclass()
获取Map<String, Staff>
,这样的话就保留了需要的类型。
使用
入参为class
Staff staff1 = mapper.readValue(jsonInString, Staff.class);
//Pretty print
String prettyStaff1 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff1);
入参为type
如果入参的是
Object obj = JacksonConstant.OM.readValue(json, JacksonConstant.OM.constructType(type));
if (obj instanceof Staff) {
return (Staff) obj;
}
如果入参为
Map<String, Staff> json2Map = JacksonConstant.OM.readValue(staffMapJson, new TypeReference<Map<String, Staff>>() {
});
同理可得,对于
public static List<Staff> json2List() throws IOException {
String json = "[{\"name\":\"rb.x\",\"age\":1,\"position\":\"sh\",\"salary\":100.23,\"skills\":[\"java\",\"mysql\"]}]";
//在反序列化为List的过程中,list<T> 和Map<K,V>本质上是parameterizedType
List<Staff> staffList = JacksonConstant.OM.readValue(json, new TypeReference<List<Staff>>() {
});
System.out.println(staffList.size());
return staffList;
}