当前位置:网站首页>对象拷贝工具类(fastjson)
对象拷贝工具类(fastjson)
2022-07-21 05:02:00 【海比天蓝2022】
开发中,都会遇到对象拷贝的需求,本工具类支持嵌套对象的拷贝
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
package com.example.demo.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Objects;
public class BeanUtils {
/**
* 批量拷贝
*
* @param list 拷贝集合
* @param clazz 拷贝之后的类型
* @return 拷贝之后的列表
*/
public static <T> List<T> copyProperties(List<?> list, Class<T> clazz) {
if (Objects.isNull(list)) {
return null;
}
JSONArray jsonArray = (JSONArray) JSON.toJSON(list);
return jsonArray.toJavaList(clazz);
}
/**
* 单个拷贝
*
* @param o 拷贝对象
* @param clazz 拷贝之后的类型
* @return 拷贝之后的对象
*/
public static <T, O> T copyProperties(O obj, Class<T> clazz) {
if (Objects.isNull(obj)) {
return null;
}
JSONObject jsonObject = (JSONObject) JSON.toJSON(obj);
return jsonObject.toJavaObject(clazz);
}
}
边栏推荐
- Docker installation mysql8 in CentOS 8
- 微信公众号开发接入,利用微信公众平台申请测试号进行本地开发
- 3D数学之三角公式
- Read yaml configuration file based on Hydra Library (support command line parameters)
- URL 和 URI
- C -- compilation preprocessing
- Uniapp develops apps to solve uni Pagescrollto does not take effect
- Let you say goodbye to the project maintenance mode of 996
- Metauniverse iwemeta: brain computer interface on the air outlet, secretly decrypting the brain
- Project debug|no string argument constructor / factory method to deserialize from string val
猜你喜欢
FL Studio 20.9水果编曲软件中文汉化补丁包
京东爆款架构师成长手册首发,架构师光环你也值得拥有
The JSON tool converts objects to JSON format strings
Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (medium)
"New product release" B2B connector version NXP i.MX 8m Mini industrial core board
Development tools supporting data + code generation, yyds
Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
uniapp开发app解决uni.pageScrollTo不生效
STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
Importerror: DLL load failed while importing win32gui: the specified module cannot be found.
随机推荐
Interview with data center and Bi business (II): pit of organizational structure Sorting
Gradle项目构建工具入门
Lst list deletion and modification
【无标题】
QT笔记——QMetaEnum类
MySQL之DCL(数据控制语言)
flutter自定义form表单,封装form表单组件
数据库设计 数据库系统概论(第五版)
The JSON tool converts objects to JSON format strings
ctfshow MengXIn misc1
论文阅读 (61):Multi-Instance Attention Network for Few-Shot Learning
微信公众号开发接入,利用微信公众平台申请测试号进行本地开发
@Scheduled 定时任务详解
Wechat applet cloud database development
STL list合并
868. 筛质数
Gradually understand the deep belief network
Flutter通过Geolocator定位插件获经纬度调用高德周边信息接口
Mes réflexions sur les tests automatisés d'assurance - chômage
STL list output and addition