/************************************************************ * * @packageName : io.company.localhost.utils * @fileName : JacksonUtil.java * @author : 조인제 * @date : 24.12.06 * @description : * * =========================================================== * DATE AUTHOR NOTE * ----------------------------------------------------------- * 24.12.06 조인제 최초 생성 * *************************************************************/ package io.company.localhost.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import io.company.localhost.common.config.JacksonCommonConfig; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ClassPathResource; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; //json 변환 유틸 @Slf4j @UtilityClass public class JacksonUtil { private ObjectMapper jacksonMapper() { ObjectMapper objectMapper = ContextUtil.getBean(ObjectMapper.class); if (objectMapper == null) { objectMapper = JacksonCommonConfig.jackson2ObjectMapperBuilder().build(); } return objectMapper.copy(); } private XmlMapper xmlMapper() { return new XmlMapper(); } /** * object -> json * * @return String */ public static String toJson(Object source) { if (Objects.isNull(source)) { return null; } try { ObjectMapper objectMapper = jacksonMapper(); return objectMapper.writeValueAsString(source); } catch (JsonProcessingException e) { log.error("toJson Exception", e); return null; } } /** * json -> object * * @param jsonString * @param targetType * @return T */ public static T fromJson(String jsonString, Class targetType) { if (!StringUtils.hasText(jsonString)) { return null; } try { ObjectMapper objectMapper = jacksonMapper(); return objectMapper.readValue(jsonString, targetType); } catch (JsonProcessingException e) { log.error("fromJson Exception", e); return null; } } /** * json -> object * * @param jsonString * @return T */ public static T fromJson(String jsonString, TypeReference targetType) { if (!StringUtils.hasText(jsonString)) { return null; } try { ObjectMapper objectMapper = jacksonMapper(); return objectMapper.readValue(jsonString, targetType); } catch (JsonProcessingException e) { log.error("fromJson Exception", e); return null; } } /** * json -> List * * @paramjsonString * @param targetType * @return List */ public static List listFromJson(String jsonString, Class targetType) { if (!StringUtils.hasText(jsonString)) { return Collections.emptyList(); } try { ObjectMapper objectMapper = jacksonMapper(); CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, targetType); return objectMapper.readValue(jsonString, listType); } catch (JsonProcessingException e) { log.error("listFromJson Exception", e); return Collections.emptyList(); } } @SuppressWarnings("unchecked") public static T xmlToJsonByJackson(String filePath) { if (!StringUtils.hasText(filePath)) { return null; } InputStream inputStream = null; try { File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + filePath); log.debug("filePath by classpath"); Map map = xmlMapper().readValue(file, Map.class); return (T) map; } catch (Exception e) { // log.error("FileNotFoundException", e); // jar 배포시 ResourceUtils 사용 안되어 아래 방법 사용 try { ClassPathResource resource = new ClassPathResource(filePath); inputStream = resource.getInputStream(); log.debug("filePath by ClassPathResource"); Map map = xmlMapper().readValue(inputStream, Map.class); return (T) map; } catch (IOException ioe) { log.error("xmlToJson Exception", e); return null; } } finally { if (inputStream != null) { try { // inputStream.close(); } catch (IOException e) { // } } } } @SuppressWarnings("unchecked") public static Map toMap(Object source) { return to(source, Map.class); } public static T to(Object source, Class targetType) { if (Objects.isNull(source)) { return null; } ObjectMapper objectMapper = jacksonMapper(); return objectMapper.convertValue(source, targetType); } public static T to(Object source, TypeReference targetType) { String jsonStr = toJson(source); return fromJson(jsonStr, targetType); } public static List toList(Object source, Class targetType) { String jsonStr = toJson(source); return listFromJson(jsonStr, targetType); } }