快捷搜索:  as

工具类:Gson——获取你想要的Type

/**

* Gson获取Type对象类

*

* https://www.jianshu.com/p/9ddd2d5b2d37

*/

public class TypeFactory {

public static Type $List(Type type) {

return $Gson$Types.newParameterizedTypeWithOwner(null, List.class, type);

}

public static Type $Set(Type type) {

return $Gson$Types.newParameterizedTypeWithOwner(null, Set.class, type);

}

public static Type $HashMap(Type type, Type type2) {

return $Gson$Types.newParameterizedTypeWithOwner(null, HashMap.class, type, type2);

}

public static Type $Map(Type type, Type type2) {

return $Gson$Types.newParameterizedTypeWithOwner(null, Map.class, type, type2);

}

public static Type $Parameterized(Type ownerType, Type rawType, Type... typeArguments) {

return $Gson$Types.newParameterizedTypeWithOwner(ownerType, rawType, typeArguments);

}

public static Type $Array(Type type) {

return $Gson$Types.arrayOf(type);

}

public static Type $SubtypeOf(Type type) {

return $Gson$Types.subtypeOf(type);

}

public static Type $SupertypeOf(Type type) {

return $Gson$Types.supertypeOf(type);

}

}

示例

List dims = gson.fromJson(json, TypeFactory.$List(Dim.class));

您可能还会对下面的文章感兴趣: