localhost-back/src/main/java/io/company/localhost/utils/JacksonUtil.java
2024-12-09 12:45:15 +09:00

208 lines
5.6 KiB
Java

package io.company.localhost.utils;
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;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
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;
//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
*
* @param Object
* @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> T fromJson(String jsonString, Class<T> targetType) {
if (StringUtils.hasText(jsonString) == false) {
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
* @param TypeReference<T> - ex) new TypeReference<List<MapDto>>() {}
* @return T
*/
public static <T> T fromJson(String jsonString, TypeReference<T> targetType) {
if (StringUtils.hasText(jsonString) == false) {
return null;
}
try {
ObjectMapper objectMapper = jacksonMapper();
return objectMapper.readValue(jsonString, targetType);
} catch (JsonProcessingException e) {
log.error("fromJson Exception", e);
return null;
}
}
/**
* json -> List<E>
*
* @paramjsonString
* @param targetType
* @return List<T>
*/
public static <E> List<E> listFromJson(String jsonString, Class<E> targetType) {
if (StringUtils.hasText(jsonString) == false) {
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> T xmlToJsonByJackson(String filePath) {
if (StringUtils.hasText(filePath) == false) {
return null;
}
InputStream inputStream = null;
try {
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + filePath);
log.debug("filePath by classpath");
Map<String, Object> 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<String, Object> 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<String, Object> toMap(Object source) {
return to(source, Map.class);
}
public static <T> T to(Object source, Class<T> targetType) {
if (Objects.isNull(source)) {
return null;
}
ObjectMapper objectMapper = jacksonMapper();
return objectMapper.convertValue(source, targetType);
}
public static <T> T to(Object source, TypeReference<T> targetType) {
String jsonStr = toJson(source);
return fromJson(jsonStr, targetType);
}
public static <E> List<E> toList(Object source, Class<E> targetType) {
String jsonStr = toJson(source);
return listFromJson(jsonStr, targetType);
}
}