This commit is contained in:
Rinjae
2025-09-05 18:57:45 +09:00
commit aacc1a389e
1229 changed files with 167963 additions and 0 deletions
@@ -0,0 +1,75 @@
package com.eactive.eai.util.json;
import java.util.Map;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.util.json.transformer.ValueTransformer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
public class JsonPathsTransform {
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
private static final ObjectMapper objectMapper = new ObjectMapper();
public static <T, R> String modifyValuesAtPaths(String jsonString,
Map<String, ValueTransformer<T, R>> pathTransformerMap, boolean isPretty) {
String path = null;
// DocumentContext documentContext = JsonPath.parse(jsonString);
// 변경헤도 별차이가 없음.
Configuration conf = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
DocumentContext documentContext = JsonPath.using(conf).parse(jsonString);
for (Map.Entry<String, ValueTransformer<T, R>> entry : pathTransformerMap.entrySet()) {
try {
path = entry.getKey();
ValueTransformer<T, R> transformer = entry.getValue();
// 현재 값 추출
T currentValue = (T) documentContext.read(path, String.class);
// 변환기 통해 새 값 생성
R newValue = transformer.transform(currentValue);
// 새로운 값으로 설정
documentContext.set(path, newValue);
if (logger.isDebug()) {
// FIXME : kbank - 로그내용 수정
String encLog = transformer.getClass().getName() + " : "
+ String.format("path=%s, orgText[%s] plain[%s] newText[%s]", path, currentValue,
transformer.getProperty("PLAIN_VALUE"), newValue);
System.out.println(encLog);
logger.debug(encLog);
}
} catch (Exception e) {
logger.error("SKIP ERROR jsonpath=" + path, e);
}
}
return printJson(documentContext.jsonString(), isPretty);
}
// JSON을 pretty하게 출력하는 메서드
private static String printJson(String jsonString, boolean isPretty) {
try {
Object jsonObject = objectMapper.readValue(jsonString, Object.class);
if (isPretty) {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
} else {
return objectMapper.writeValueAsString(jsonObject);
}
} catch (JsonProcessingException e) {
return jsonString;
}
}
}
@@ -0,0 +1,108 @@
package com.eactive.eai.util.json;
import java.util.Map;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.util.json.transformer.ValueTransformer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class JsonSimplePathsTransform {
static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
private static final ObjectMapper objectMapper = new ObjectMapper();
public static <T, R> String modifyValuesAtPaths(String jsonString,
Map<String, ValueTransformer<T, R>> pathTransformerMap, boolean isPretty) {
try {
JsonNode rootNode = objectMapper.readTree(jsonString);
for (Map.Entry<String, ValueTransformer<T, R>> entry : pathTransformerMap.entrySet()) {
String path = entry.getKey();
ValueTransformer<T, R> transformer = entry.getValue();
JsonNode currentNode = getNodeAtPath(rootNode, path);
if (currentNode != null && currentNode.isValueNode()) {
T currentValue = extractValue(currentNode);
R newValue = transformer.transform(currentValue);
if(logger.isDebug()) {
// FIXME : kbank - 로그내용 수정
String encLog = transformer.getClass().getName() +" : "+ String.format("path=%s, orgText[%s] plain[%s] newText[%s]", path, currentValue, transformer.getProperty("PLAIN_VALUE"), newValue);
System.out.println(encLog);
logger.debug(encLog);
}
setNodeValue(getParentNode(rootNode, getParentPath(path)), getFieldName(path), newValue);
}
}
if(isPretty) {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
}
else {
return objectMapper.writeValueAsString(rootNode);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static <T> T extractValue(JsonNode node) {
if (node.isTextual()) {
return (T) node.asText();
} else if (node.isInt()) {
return (T) (Integer) node.asInt();
} else if (node.isDouble()) {
return (T) (Double) node.asDouble();
} else if (node.isBoolean()) {
return (T) (Boolean) node.asBoolean();
} else {
return (T) node.asText();
}
}
private static <R> void setNodeValue(JsonNode parentNode, String fieldNameOrIndex, R newValue) {
if (parentNode.isArray()) {
((ArrayNode) parentNode).set(Integer.parseInt(fieldNameOrIndex),
objectMapper.convertValue(newValue, JsonNode.class));
} else if (parentNode.isObject()) {
((ObjectNode) parentNode).set(fieldNameOrIndex, objectMapper.convertValue(newValue, JsonNode.class));
}
}
private static String getParentPath(String path) {
int lastSlashIndex = path.lastIndexOf('/');
return lastSlashIndex == 0 ? "/" : path.substring(0, lastSlashIndex);
}
private static String getFieldName(String path) {
int lastSlashIndex = path.lastIndexOf('/');
return path.substring(lastSlashIndex + 1);
}
private static JsonNode getParentNode(JsonNode rootNode, String parentPath) {
return getNodeAtPath(rootNode, parentPath);
}
private static JsonNode getNodeAtPath(JsonNode rootNode, String path) {
String[] tokens = path.split("/");
JsonNode currentNode = rootNode;
for (String token : tokens) {
if (token.isEmpty())
continue;
if (token.matches("\\d+")) {
currentNode = currentNode.get(Integer.parseInt(token));
} else {
currentNode = currentNode.get(token);
}
if (currentNode == null) {
return null;
}
}
return currentNode;
}
}
@@ -0,0 +1,28 @@
package com.eactive.eai.util.json.transformer;
import java.util.HashMap;
import java.util.Map;
public class CustomValueTransformer implements ValueTransformer<String, String> {
private final Map<String, Object> properties = new HashMap<>();
@Override
public void setProperty(String propertyName, Object value) {
properties.put(propertyName, value);
}
@Override
public String getProperty(String propertyName) {
return (String) properties.get(propertyName);
}
@Override
public String transform(String value) {
// 예: 변환할 때 "prefix"와 "suffix" 속성을 사용
String prefix = (String) properties.getOrDefault("PREFIX", "");
String suffix = (String) properties.getOrDefault("SUFFIX", "");
properties.put("PLAIN_VALUE", value);
return prefix + value + suffix;
}
}
@@ -0,0 +1,7 @@
package com.eactive.eai.util.json.transformer;
public interface ValueTransformer<T, R> {
void setProperty(String propertyName, Object value);
String getProperty(String propertyName);
R transform(T value) throws Exception;
}