package com.kri.common.json; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.Collections; import java.util.Map; public final class JsonUtil { private JsonUtil() { // 유틸 클래스 - 인스턴스 생성 방지 } /** * 임의의 자바 객체를 Map으로 변환한다. 주의: JSON 문자열(String)을 파싱하는 용도가 아니라, 이미 객체로 있는 DTO/Entity 등을 Map으로 바꾸는 * 용도이다. */ public static Map toMap(ObjectMapper objectMapper, T source) { if (source == null) { return Collections.emptyMap(); } @SuppressWarnings("unchecked") Map map = objectMapper.convertValue(source, Map.class); return map; } /** * JSON 문자열을 Map으로 파싱한다. * * @param json JSON 텍스트 (null 또는 blank면 빈 Map 반환) */ public static Map parseJsonToMap(ObjectMapper objectMapper, String json) { if (json == null || json.isBlank()) { return Collections.emptyMap(); } try { return objectMapper.readValue(json, new TypeReference>() { }); } catch (IOException e) { throw new IllegalArgumentException("JSON을 Map으로 파싱하는 중 오류가 발생했습니다.", e); } } /** * 객체를 JSON 문자열로 직렬화한다. */ public static String toJson(ObjectMapper objectMapper, Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException e) { throw new IllegalArgumentException("객체를 JSON 문자열로 변환하는 중 오류가 발생했습니다.", e); } } /** * 객체를 사람이 읽기 좋은 pretty JSON 문자열로 직렬화한다. */ public static String toPrettyJson(ObjectMapper objectMapper, Object value) { try { return objectMapper.writerWithDefaultPrettyPrinter() .writeValueAsString(value); } catch (JsonProcessingException e) { throw new IllegalArgumentException("객체를 pretty JSON 문자열로 변환하는 중 오류가 발생했습니다.", e); } } /** * JSON 문자열을 주어진 타입으로 역직렬화한다. ex) fromJson(mapper, json, UserInfoDto.class) */ public static T fromJson(ObjectMapper objectMapper, String json, Class type) { if (json == null || json.isBlank()) { return null; } try { return objectMapper.readValue(json, type); } catch (IOException e) { throw new IllegalArgumentException("JSON을 " + type.getSimpleName() + " 으로 파싱하는 중 오류가 발생했습니다.", e); } } /** * JSON 문자열을 제네릭 타입으로 역직렬화한다. ex) fromJson(mapper, json, new TypeReference>() * {}) */ public static T fromJson(ObjectMapper objectMapper, String json, TypeReference typeRef) { if (json == null || json.isBlank()) { return null; } try { return objectMapper.readValue(json, typeRef); } catch (IOException e) { throw new IllegalArgumentException("JSON을 제네릭 타입으로 파싱하는 중 오류가 발생했습니다.", e); } } /** * JSON 기반 deep copy (또는 타입 변환). 내부적으로 convertValue를 사용하므로, source를 targetType 형태로 깊은 복사/매핑한다. *

* ex) UserInfoDto dto2 = deepCopy(mapper, dto1, UserInfoDto.class); */ public static T deepCopy(ObjectMapper objectMapper, T source, Class targetType) { if (source == null) { return null; } return objectMapper.convertValue(source, targetType); } /** * 서로 다른 타입 간에 JSON 기반 매핑을 할 때 사용. *

* ex) UserInfoDto dto = convert(mapper, entity, UserInfoDto.class); */ public static T convert(ObjectMapper objectMapper, S source, Class targetType) { if (source == null) { return null; } return objectMapper.convertValue(source, targetType); } }