260212_01_DOC: 프로젝트 전체에 구글 java 스타일 적용

This commit is contained in:
2026-02-12 11:22:05 +09:00
parent bc5af86bcd
commit 88e9845c1c
61 changed files with 3029 additions and 2667 deletions

View File

@@ -3,122 +3,120 @@ 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() {
// 유틸 클래스 - 인스턴스 생성 방지
}
private JsonUtil() {
// 유틸 클래스 - 인스턴스 생성 방지
}
/**
* 임의의 자바 객체를 Map으로 변환한다.
* 주의: JSON 문자열(String)을 파싱하는 용도가 아니라,
* 이미 객체로 있는 DTO/Entity 등을 Map으로 바꾸는 용도이다.
*/
public static <T> Map<String, Object> toMap(ObjectMapper objectMapper, T source) {
if (source == null) {
return Collections.emptyMap();
}
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(source, Map.class);
return map;
/**
* 임의의 자바 객체를 Map으로 변환한다. 주의: JSON 문자열(String)을 파싱하는 용도가 아니라, 이미 객체로 있는 DTO/Entity 등을 Map으로 바꾸는
* 용도이다.
*/
public static <T> Map<String, Object> toMap(ObjectMapper objectMapper, T source) {
if (source == null) {
return Collections.emptyMap();
}
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.convertValue(source, Map.class);
return map;
}
/**
* JSON 문자열을 Map으로 파싱한다.
*
* @param json JSON 텍스트 (null 또는 blank면 빈 Map 반환)
*/
public static Map<String, Object> parseJsonToMap(ObjectMapper objectMapper, String json) {
if (json == null || json.isBlank()) {
return Collections.emptyMap();
}
try {
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new IllegalArgumentException("JSON을 Map으로 파싱하는 중 오류가 발생했습니다.", e);
}
/**
* JSON 문자열을 Map으로 파싱한다.
*
* @param json JSON 텍스트 (null 또는 blank면 빈 Map 반환)
*/
public static Map<String, Object> parseJsonToMap(ObjectMapper objectMapper, String json) {
if (json == null || json.isBlank()) {
return Collections.emptyMap();
}
try {
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
});
} 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);
}
/**
* 객체를 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);
}
/**
* 객체를 사람이 읽기 좋은 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> T fromJson(ObjectMapper objectMapper, String json, Class<T> 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, UserInfoDto.class)
*/
public static <T> T fromJson(ObjectMapper objectMapper, String json, Class<T> 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<List<UserInfoDto>>() {})
*/
public static <T> T fromJson(ObjectMapper objectMapper, String json, TypeReference<T> typeRef) {
if (json == null || json.isBlank()) {
return null;
}
try {
return objectMapper.readValue(json, typeRef);
} catch (IOException e) {
throw new IllegalArgumentException("JSON을 제네릭 타입으로 파싱하는 중 오류가 발생했습니다.", e);
}
/**
* JSON 문자열을 제네릭 타입으로 역직렬화한다. ex) fromJson(mapper, json, new TypeReference<List<UserInfoDto>>()
* {})
*/
public static <T> T fromJson(ObjectMapper objectMapper, String json, TypeReference<T> 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> T deepCopy(ObjectMapper objectMapper, T source, Class<T> targetType) {
if (source == null) {
return null;
}
return objectMapper.convertValue(source, targetType);
/**
* JSON 기반 deep copy (또는 타입 변환). 내부적으로 convertValue를 사용하므로, source를 targetType 형태로 깊은 복사/매핑한다.
* <p>
* ex) UserInfoDto dto2 = deepCopy(mapper, dto1, UserInfoDto.class);
*/
public static <T> T deepCopy(ObjectMapper objectMapper, T source, Class<T> targetType) {
if (source == null) {
return null;
}
return objectMapper.convertValue(source, targetType);
}
/**
* 서로 다른 타입 간에 JSON 기반 매핑을 할 때 사용.
*
* ex) UserInfoDto dto = convert(mapper, entity, UserInfoDto.class);
*/
public static <S, T> T convert(ObjectMapper objectMapper, S source, Class<T> targetType) {
if (source == null) {
return null;
}
return objectMapper.convertValue(source, targetType);
/**
* 서로 다른 타입 간에 JSON 기반 매핑을 할 때 사용.
* <p>
* ex) UserInfoDto dto = convert(mapper, entity, UserInfoDto.class);
*/
public static <S, T> T convert(ObjectMapper objectMapper, S source, Class<T> targetType) {
if (source == null) {
return null;
}
return objectMapper.convertValue(source, targetType);
}
}