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

com.github.hdy.common.util.ObjectMappers Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package com.github.hdy.common.util;

import com.fasterxml.jackson.core.JsonParser;
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.github.hdy.common.exceptions.CustomException;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

public abstract class ObjectMappers {

    private static final String DateFormat = Dates.FORMAT_DATETIME;
    private static final boolean PrettyFormat = false;

    /********************************************************************************************************************************************************
     * newMapper
     *********************************************************************************************************************************************************/
    /**
     * 创建Mapper转换器
     */
    private static ObjectMapper newMapper(String dateFormat, ObjectMapper mapper) {
        if (mapper == null) {
            mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 单引号
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);// 未引号字段
            if (dateFormat == null) {
                dateFormat = DateFormat;
            }
            mapper.setDateFormat(Dates.newDateFormat(dateFormat));
        }
        return mapper;
    }

    /**
     * 创建Mapper转换器,
     */
    public static ObjectMapper newMapper() {
        return newMapper(null, null);
    }

    public static ObjectMapper newMapper(String dateFormat) {
        return newMapper(dateFormat, null);
    }

    public static ObjectMapper newMapper(ObjectMapper mapper) {
        return newMapper(null, mapper);
    }

    /********************************************************************************************************************************************************
     * toJSONString
     *********************************************************************************************************************************************************/
    public static String toJSONString(Object o) {
        return toJSONString(o, null, null, PrettyFormat);
    }

    public static String toJSONString(Object o, boolean prettyFormat) {
        return toJSONString(o, null, null, prettyFormat);
    }

    public static String toJSONString(Object o, String dateFormat) {
        return toJSONString(o, dateFormat, null, PrettyFormat);
    }

    public static String toJSONString(Object o, String dateFormat, boolean prettyFormat) {
        return toJSONString(o, dateFormat, null, prettyFormat);
    }

    public static String toJSONString(Object o, ObjectMapper mapper) {
        return toJSONString(o, null, mapper, PrettyFormat);
    }

    public static String toJSONString(Object o, ObjectMapper mapper, boolean prettyFormat) {
        return toJSONString(o, null, mapper, prettyFormat);
    }

    private static String toJSONString(Object o, String dateFormat, ObjectMapper mapper, boolean prettyFormat) {
        String jsonString = null;
        try {
            mapper = newMapper(mapper);
            if (dateFormat != null) {
                mapper.setDateFormat(Dates.newDateFormat(dateFormat));
            }
            if (prettyFormat) {
                jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
            } else {
                jsonString = mapper.writeValueAsString(o);
            }
        } catch (JsonProcessingException e) {
            throw new CustomException(e);
        }
        return jsonString;
    }

    /********************************************************************************************************************************************************
     * toBean
     *********************************************************************************************************************************************************/
    public static  T toBean(String jsonString, Class clazz) {
        return toBean(jsonString, clazz, null, null);
    }

    public static  T toBean(String jsonString, Class clazz, ObjectMapper mapper) {
        return toBean(jsonString, clazz, null, mapper);
    }

    public static  T toBean(String jsonString, TypeReference typeReference) {
        return toBean(jsonString, null, typeReference, null);
    }

    public static  T toBean(String jsonString, TypeReference typeReference, ObjectMapper mapper) {
        return toBean(jsonString, null, typeReference, mapper);
    }

    private static  T toBean(String jsonString, Class clazz, TypeReference type, ObjectMapper mapper) {
        try {
            mapper = newMapper(mapper);
            if (type != null) {
                return mapper.readValue(jsonString, type);
            } else {
                return mapper.readValue(jsonString, clazz);
            }
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /********************************************************************************************************************************************************
     * toList
     *********************************************************************************************************************************************************/
    public static  List toList(String jsonString, Class clazz) {
        return toList(jsonString, clazz, null);
    }

    @SuppressWarnings("unchecked")
    public static  List toList(String jsonString, Class clazz, ObjectMapper mapper) {
        List objs = Lists.newArrayList();
        List> maps = toBean(jsonString, List.class, mapper);
        if (maps != null) {
            for (Map map : maps) {
                objs.add(convert(map, clazz, mapper));
            }
        }
        return objs;
    }

    /********************************************************************************************************************************************************
     * toMap
     *********************************************************************************************************************************************************/
    @SuppressWarnings("unchecked")
    public static Map toMap(Object obj) {
        return newMapper().convertValue(obj, Map.class);
    }

    @SuppressWarnings("unchecked")
    public static Map toMap(String jsonString) {
        return toBean(jsonString, Map.class);
    }

    @SuppressWarnings("unchecked")
    public static Map toMap(String jsonString, ObjectMapper mapper) {
        return toBean(jsonString, Map.class, mapper);
    }

    /********************************************************************************************************************************************************
     * convert
     *********************************************************************************************************************************************************/
    public static  T convert(Object fromValue, Class toValueType) {
        return convert(fromValue, toValueType, null);
    }

    public static  T convert(Object fromValue, Class toValueType, ObjectMapper mapper) {
        return newMapper(mapper).convertValue(fromValue, toValueType);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy