JSONUtils.java 5.05 KB
package com.wondertek.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class JSONUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module())
            // 使jackson识别被@JsonFormat标识的字段
            .registerModule(new JavaTimeModule())
            // 设置jackson反序列化时遇到类中不存在的字段时不抛出异常
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    private JSONUtils() {
    }

    public static ObjectMapper getInstance() {
        return objectMapper;
    }

    /**
     * javaBean,list,array convert to json string
     */
    public static String obj2json(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("获取异常信息:{}", e.getMessage());
        }
        return null;
    }

    public static JsonNode parseJsonNode(String jsonChar) {
        return jsonToObject(jsonChar, JsonNode.class);
    }

    public static <T> T obj2T(Object obj, Class<T> t) {
        return objectMapper.convertValue(obj, new TypeReference<T>() {
        });
    }

    public static <T> T jsonToObject(String jsonChar, TypeReference<T> t) {
        try {
            return objectMapper.readValue(jsonChar, t);
        } catch (Exception e) {
            log.error("json字符串反序列化失败,json字符串:{},类型:{}", jsonChar, t.getType().getTypeName());
        }
        return null;
    }

    public static <T> List<T> jsonToList(String jsonChar, Class<T> t) {
        return jsonToObject(jsonChar, new TypeReference<List<T>>() {
        });
    }

    public static <T> T jsonToObject(String jsonChar, Class<T> t) {
        try {
            return objectMapper.readValue(jsonChar, t);
        } catch (Exception e) {
            log.error("json字符串反序列化失败,json字符串:{},类型:{}", jsonChar, t.getName());
        }
        return null;
    }

    /**
     * 获取节点指定字段的值
     * <p><b>Title:</b> getString</p>
     * <p><b>Description:</b> </p>
     * 
     * @author Zewei.Zhou
     * @param jsonNode 节点
     * @param fieldName 字段名称
     * @return
     */
    public static String getString(JsonNode jsonNode, String fieldName) {
        if (jsonNode == null) {
            return null;
        }
        JsonNode node = jsonNode.get(fieldName);
        
        return (node == null || node.isNull()) ? null : node.asText();
    }

    /**
     * 获取节点指定字段的值
     * <p><b>Title:</b> nodeToString</p>
     * <p><b>Description:</b> </p>
     *
     * @author Zewei.Zhou
     * @param jsonNode 节点
     * @param fieldName 字段名称
     * @return
     */
    public static String nodeToString(JsonNode jsonNode, String fieldName) {
        if (jsonNode == null) {
            return null;
        }
        JsonNode node = jsonNode.get(fieldName);

        return (node == null || node.isNull()) ? null : node.toString();
    }

    /**
     * 获取节点指定字段的int值,默认为0
     * <p><b>Title:</b> getInt</p>
     * <p><b>Description:</b> </p>
     * 
     * @author Zewei.Zhou
     * @param jsonNode 节点
     * @param fieldName 字段名称
     * @return
     */
    public static int getInt(JsonNode jsonNode, String fieldName) {
        if (jsonNode == null) {
            return 0;
        }
        JsonNode node = jsonNode.get(fieldName);
        return node == null || !node.canConvertToInt() ? 0 : node.asInt();
    }

    /**
     * 获取节点指定字段的值
     * <p><b>Title:</b> getInteger</p>
     * <p><b>Description:</b> </p>
     * 
     * @author Zewei.Zhou
     * @param jsonNode 节点
     * @param fieldName 字段名称
     * @return
     */
    public static Integer getInteger(JsonNode jsonNode, String fieldName) {
        if (jsonNode == null) {
            return null;
        }
        JsonNode node = jsonNode.get(fieldName);
        return node == null || !node.canConvertToInt() ? null : node.asInt();
    }

    /**
     * 获取节点指定字段的值
     * <p><b>Title:</b> getLong</p>
     * <p><b>Description:</b> </p>
     * 
     * @author Zewei.Zhou
     * @param jsonNode 节点
     * @param fieldName 字段名称
     * @return
     */
    public static Long getLong(JsonNode jsonNode, String fieldName) {
        if (jsonNode == null) {
            return null;
        }
        JsonNode node = jsonNode.get(fieldName);
        return node == null || !node.canConvertToLong() ? null : node.asLong();
    }
}