|
|
|
@ -1,15 +1,8 @@
|
|
|
|
|
package kr.xit.core.support.utils;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
|
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.cfg.CoercionAction;
|
|
|
|
|
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
@ -18,9 +11,25 @@ import kr.xit.core.spring.util.SpringUtils;
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JSON Util Class
|
|
|
|
|
* <pre>
|
|
|
|
|
* description : JSON Utility
|
|
|
|
|
*
|
|
|
|
|
* packageName : kr.xit.core.support.utils
|
|
|
|
|
* fileName : JsonUtils
|
|
|
|
|
* author : limju
|
|
|
|
|
* date : 2023-09-04
|
|
|
|
|
* ======================================================================
|
|
|
|
|
* 변경일 변경자 변경 내용
|
|
|
|
|
* ----------------------------------------------------------------------
|
|
|
|
|
* 2023-09-04 limju 최초 생성
|
|
|
|
|
*
|
|
|
|
|
* </pre>
|
|
|
|
|
* @see kr.xit.core.spring.config.support.CustomJacksonConfig
|
|
|
|
|
*/
|
|
|
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
|
|
|
|
public class JsonUtils {
|
|
|
|
@ -75,8 +84,11 @@ public class JsonUtils {
|
|
|
|
|
* @return T
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T toObjByObj(final Object obj, final Class<T> cls) {
|
|
|
|
|
final String str = toJson(obj);
|
|
|
|
|
return str != null ? toObject(str, cls) : null;
|
|
|
|
|
try {
|
|
|
|
|
return obj != null ? OM.convertValue(obj, cls) : null;
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
throw BizRuntimeException.create(e.getLocalizedMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -111,6 +123,91 @@ public class JsonUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Object의 key와 value를 추출
|
|
|
|
|
* -> key배열, value배열의 JSONObject return
|
|
|
|
|
* @param obj key, value를 추출할 Object
|
|
|
|
|
* @param keyName key배열의 JSON key name
|
|
|
|
|
* @param valueName value배열의 JSON key name
|
|
|
|
|
* @return JSONObject key배열, value배열의 JSONObject
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject extractObjKeyValue(final Object obj, final String keyName, final String valueName){
|
|
|
|
|
return extractJsonKeyValue(toObjByObj(obj, JSONObject.class), keyName, valueName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JSONObject의 key와 value를 추출
|
|
|
|
|
* -> key배열, value배열의 JSONObject return
|
|
|
|
|
* @param json
|
|
|
|
|
* @param keyName key배열의 JSON key name
|
|
|
|
|
* @param valueName value배열의 JSON key name
|
|
|
|
|
* @return JSONObject key배열, value배열의 JSONObject
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject extractJsonKeyValue(final JSONObject json, final String keyName, final String valueName){
|
|
|
|
|
final JSONObject rtnJson = new JSONObject();
|
|
|
|
|
final JSONArray keys = new JSONArray();
|
|
|
|
|
final JSONArray values = new JSONArray();
|
|
|
|
|
for (Object key : json.keySet()) {
|
|
|
|
|
Object value = json.get(key);
|
|
|
|
|
|
|
|
|
|
if (value instanceof JSONObject)
|
|
|
|
|
extractJsonKeyValue((JSONObject)value, keyName, valueName);
|
|
|
|
|
else if (value instanceof JSONArray)
|
|
|
|
|
((JSONArray)value).forEach(obj -> extractJsonKeyValue((JSONObject)obj, keyName, valueName));
|
|
|
|
|
else{
|
|
|
|
|
//System.out.println(key + ", " + value);
|
|
|
|
|
keys.add(String.valueOf(key));
|
|
|
|
|
values.add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rtnJson.put(keyName, keys);
|
|
|
|
|
rtnJson.put(valueName, values);
|
|
|
|
|
return rtnJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JSONArray의 key와 value를 추출
|
|
|
|
|
* -> key배열, value배열의 JSONObject return
|
|
|
|
|
* @param jsons JSONArray
|
|
|
|
|
* @param keyName key배열의 JSON key name
|
|
|
|
|
* @param valueName value배열의 JSON key name
|
|
|
|
|
* @return JSONObject key배열, value배열의 JSONObject
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject extractJsonArrayKeyValue(final net.sf.json.JSONArray jsons, final String keyName, final String valueName){
|
|
|
|
|
final JSONObject rtnJson = new JSONObject();
|
|
|
|
|
final JSONArray keys = new JSONArray();
|
|
|
|
|
final JSONArray values = new JSONArray();
|
|
|
|
|
for(int i = 0; i<jsons.size(); i++){
|
|
|
|
|
JSONObject json = extract(jsons.getJSONObject(i), keyName, valueName);
|
|
|
|
|
if(i == 0) rtnJson.put(keyName, json.get(keyName));
|
|
|
|
|
values.add(json.get(valueName));
|
|
|
|
|
}
|
|
|
|
|
rtnJson.put(valueName, values);
|
|
|
|
|
return rtnJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static JSONObject extract(net.sf.json.JSONObject json, String keyName, String valueName) {
|
|
|
|
|
final JSONObject rtnJson = new JSONObject();
|
|
|
|
|
final JSONArray keys = new JSONArray();
|
|
|
|
|
final JSONArray values = new JSONArray();
|
|
|
|
|
for (Object key : json.keySet()) {
|
|
|
|
|
Object value = json.get(key);
|
|
|
|
|
|
|
|
|
|
if (value instanceof JSONObject)
|
|
|
|
|
extractJsonKeyValue((JSONObject)value, keyName, valueName);
|
|
|
|
|
else if (value instanceof JSONArray)
|
|
|
|
|
((JSONArray)value).forEach(obj -> extractJsonKeyValue((JSONObject)obj, keyName, valueName));
|
|
|
|
|
else{
|
|
|
|
|
//System.out.println(key + ", " + value);
|
|
|
|
|
keys.add(String.valueOf(key));
|
|
|
|
|
values.add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rtnJson.put(keyName, keys);
|
|
|
|
|
rtnJson.put(valueName, values);
|
|
|
|
|
return rtnJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Json 데이터 보기 좋게 변환.
|
|
|
|
|
* @param obj Object json
|
|
|
|
@ -125,25 +222,6 @@ public class JsonUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ObjectMapper getObjectMapper() {
|
|
|
|
|
final ObjectMapper om = new ObjectMapper();
|
|
|
|
|
om.setSerializationInclusion(Include.NON_NULL);
|
|
|
|
|
// No serializer found for class 에러 - private 필드
|
|
|
|
|
om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
|
|
|
|
|
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
|
|
|
|
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
om.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, false);
|
|
|
|
|
|
|
|
|
|
om.coercionConfigDefaults()
|
|
|
|
|
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
|
|
|
|
|
// om.coercionConfigFor(LogicalType.Enum)
|
|
|
|
|
// .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
|
|
|
|
|
// om.coercionConfigFor(LogicalType.POJO)
|
|
|
|
|
// .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
|
|
|
|
|
|
|
|
|
|
return om;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Json 데이터 보기 좋게 변환.
|
|
|
|
|
* @param json String json
|
|
|
|
|