All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eleme.openapi.sdk.utils.JacksonUtils Maven / Gradle / Ivy

The newest version!
package eleme.openapi.sdk.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import eleme.openapi.sdk.api.exception.JsonParseException;

import java.io.IOException;
import java.util.*;

public class JacksonUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        //去掉默认的时间戳格式
        //objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        //设置为中国上海时区
        //objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

        //空值不序列化,入口网关验证签名会产生不一致不能去掉
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //反序列化时,属性不存在的兼容处理
        objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //单引号处理
        objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }

    private JacksonUtils() {
    }

    public static final ObjectMapper getInstance() {
        return objectMapper;
    }

    public static String obj2json(Object obj) {

        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new JsonParseException();
        }
    }

    public static  T json2pojo(String jsonStr, Class clazz) {
        try {
            return objectMapper.readValue(jsonStr, clazz);
        } catch (IOException e) {
            throw new JsonParseException();
        }
    }

    public static  T json2pojo(String jsonStr, JavaType javaType) {
        try {
            return objectMapper.readValue(jsonStr, javaType);
        } catch (IOException e) {
            throw new JsonParseException();
        }
    }

    public static  Map json2map(String jsonStr) {
        try {
            return objectMapper.readValue(jsonStr, Map.class);
        } catch (IOException e) {
            throw new JsonParseException();
        }
    }

    public static  Map json2map(String jsonStr, Class clazz) {
        Map> map;
        try {
            map = objectMapper.readValue(jsonStr,
                    new TypeReference>() {
                    });
        } catch (IOException e) {
            throw new JsonParseException();
        }
        Map result = new HashMap();
        for (Map.Entry> entry : map.entrySet()) {
            result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
        }
        return result;
    }

    public static  List json2list(String jsonArrayStr, Class clazz) {
        List> list = null;
        try {
            list = objectMapper.readValue(jsonArrayStr,
                    new TypeReference>() {
                    });
        } catch (IOException e) {
            throw new JsonParseException();
        }
        List result = new ArrayList();
        for (Map map : list) {
            result.add(map2pojo(map, clazz));
        }
        return result;
    }

    public static  T map2pojo(Map map, Class clazz) {
        return objectMapper.convertValue(map, clazz);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy