美文网首页
工具类:Gson——获取你想要的Type

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

作者: 楷桐 | 来源:发表于2018-02-03 19:15 被阅读19次
    /**
     * Gson获取Type工具类
     * <p>
     * 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<Dim> dims = gson.fromJson(json, TypeFactory.$List(Dim.class));
    

    相关文章

      网友评论

          本文标题:工具类:Gson——获取你想要的Type

          本文链接:https://www.haomeiwen.com/subject/jzzuzxtx.html