package com.ruoyi.common.utils; import cn.hutool.core.util.ArrayUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * JSON 工具类 * * @author 芋道源码 */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class JsonUtils { private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class); public static String toJsonString(Object object) { if (StringUtils.isNull(object)) { return null; } try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } public static T parseObject(String text, Class clazz) { if (StringUtils.isEmpty(text)) { return null; } try { return objectMapper.readValue(text, clazz); } catch (IOException e) { throw new RuntimeException(e); } } public static T parseObject(byte[] bytes, Class clazz) { if (ArrayUtil.isEmpty(bytes)) { return null; } try { return objectMapper.readValue(bytes, clazz); } catch (IOException e) { throw new RuntimeException(e); } } public static T parseObject(String text, TypeReference typeReference) { if (StringUtils.isBlank(text)) { return null; } try { return objectMapper.readValue(text, typeReference); } catch (IOException e) { throw new RuntimeException(e); } } public static Map parseMap(String text) { if (StringUtils.isBlank(text)) { return null; } try { return objectMapper.readValue(text, new TypeReference>() { }); } catch (IOException e) { throw new RuntimeException(e); } } public static List parseArray(String text, Class clazz) { if (StringUtils.isEmpty(text)) { return new ArrayList<>(); } try { return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); } catch (IOException e) { throw new RuntimeException(e); } } }