• 对象值的复制


     public static <T> T obj2Obj(Object origin, Class<T> clazz) {
    Gson gson = new Gson();
    if(origin == null) { return null; } return gson.fromJson(gson.toJson(origin), clazz); }
    //把一个list对象转成含有相同属性的另外一个list
    public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
    Gson gson = new Gson();
    if(origin == null) { return null; } Type[] argumentTypes = new Type[]{type}; Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null); return gson.fromJson(gson.toJson(origin), parameterizedType); }
    public static <T> T obj2Obj(@C("待转对象")Object origin, @C("转成的类")Class<T> clazz) {
    if(origin == null) {
    return null;
    }
    return gson.fromJson(gson.toJson(origin), clazz);
    }

    public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
    if(origin == null) {
    return null;
    }

    Type[] argumentTypes = new Type[]{type};
    Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null);
    return gson.fromJson(gson.toJson(origin), parameterizedType);
    }
  • 相关阅读:
    leetcode 2 Add Two Numbers
    leetcode1
    二叉树的最大高度和最大宽度
    插入排序
    eventEmitter学习
    用node.js做一个爬虫
    HTLM5 WebSocket权威指南
    (new Function("return " + json))();
    JS中的this变量的使用介绍
    node.js 模块加载原理
  • 原文地址:https://www.cnblogs.com/coderising/p/11219530.html
Copyright © 2020-2023  润新知