web/SpringBoot

[SpringBoot] ConvertUtils 클래스

뽀리님 2023. 12. 1. 13:44
@Slf4j
public class ConvertUtils {
    /**
     * String To JsonObject
     * @param jsonStr
     * @return
     */
    public static JSONObject strToJson(String jsonStr){
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject;
        } catch (Exception e) {
            log.error("String to JSON Parsing EXCEPTION:"+e.getMessage(),e);
            return new JSONObject();
        }
    }


    /**
     * JsonString TO DTO Object
     * @param jsonStr
     * @param valueType
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> T strToDTO(String jsonStr, Class<T> valueType) {
        try {
            ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
            T obj = mapper.readValue(jsonStr, valueType);
            return obj;
        } catch (JsonProcessingException ex){
            log.error("String TO Dto Parsing Exception:"+ex.getMessage(),ex);
            return null;
        }

    }

    /**
     * DTO Object to JsonString
     * @param obj
     * @return
     * @throws JsonProcessingException
     */
    public static String dtoToJsonString(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writeValueAsString(obj);
        return result;
    }

    /**
     * DTO Object TO JsonObject
     * @param obj
     * @return
     */
    public static JSONObject dtoToJson(Object obj) throws JsonProcessingException {
        return strToJson(dtoToJsonString(obj));
    }

    /**
     * Map To JsonString
     * @param map
     * @return
     */
    public static String mapToJsonStr(Map<String,Object> map){
        JSONObject json =  new JSONObject(map);
        return json.toString();
    }


    /**
     * Json Object To Map
     * @param obj
     * @return
     */
    public static Map<String, Object> jsonToMap(JSONObject obj){
        if (ObjectUtils.isEmpty(obj)) {
            log.error("BAD REQUEST obj : {}", obj);
            throw new IllegalArgumentException(String.format("BAD REQUEST obj %s", obj));
        } try {
            return new ObjectMapper().readValue(obj.toString(), Map.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e); throw new RuntimeException(e);
        }
    }

    /**
     * Json Array To List Map
     * @param jsonArray
     * @return
     */
    public static List<Map<String, Object>> jsonArrToListMap(JSONArray jsonArray) {
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("jsonArray is null.");
            throw new IllegalArgumentException("jsonArray is null");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Object jsonObject : jsonArray) {
            list.add(jsonToMap((JSONObject) jsonObject));
        } return list;
    }

    /**
     * map to Dto
     * @param obj
     * @param valueType
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> T mapToDto(Map<String,Object> obj, Class<T> valueType) throws JsonProcessingException {
        return strToDTO(mapToJsonStr(obj), valueType);
    }

    /**
     * Json String to Map
     * @param jsonString
     * @return
     */
    public static Map<String,Object> strToMap(String jsonString) {
        try {
            return new ObjectMapper().readValue(jsonString, Map.class);
        } catch (JsonProcessingException e) {
            log.error("String to MAP Parsing EXCEPTION:"+e.getMessage(),e);
            return new HashMap<>();
        }
    }


}