Jackson工具类

  1 import com.fasterxml.jackson.core.JsonProcessingException;
  2 import com.fasterxml.jackson.core.type.TypeReference;
  3 import com.fasterxml.jackson.databind.DeserializationFeature;
  4 import com.fasterxml.jackson.databind.JavaType;
  5 import com.fasterxml.jackson.databind.ObjectMapper;
  6 import com.fasterxml.jackson.databind.SerializationFeature;
  7 import lombok.extern.slf4j.Slf4j;
  8 import org.springframework.util.StringUtils;
  9 
 10 import java.io.IOException;
 11 import java.text.SimpleDateFormat;
 12 
 13 @Slf4j
 14 public class JacksonUtil {
 15 
 16     public final static ObjectMapper mapper = new ObjectMapper();
 17     // 日期格式化
 18     private static final String STANDARD_FORMAT = "yyyy-MM--dd HH:mm:ss";
 19 
 20     static {
 21         // 取消默认的timestamp转换
 22         mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
 23         // 忽略空bean转json的错误
 24         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 25         // 设置日期格式
 26         mapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
 27         // 忽略json中存在,但java对象中不存在对应属性的情况
 28         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 29 
 30     }
 31 
 32     /**
 33      * Object转字符串
 34      *
 35      * @param obj
 36      * @param <T>
 37      * @return
 38      */
 39     public static <T> String object2String(T obj) {
 40         if (obj == null) {
 41             return null;
 42         }
 43         try {
 44             return obj instanceof String ? (String) obj : mapper.writeValueAsString(obj);
 45         } catch (JsonProcessingException e) {
 46             log.warn("Parse Object to String error: {}", e.getMessage());
 47             return null;
 48         }
 49     }
 50 
 51     /**
 52      * 转美化的 格式化的Json字符串
 53      *
 54      * @param obj
 55      * @param <T>
 56      * @return
 57      */
 58     public static <T> String object2StringPretty(T obj) {
 59         if (obj == null) {
 60             return null;
 61         }
 62         try {
 63             return obj instanceof String ? (String) obj : mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
 64         } catch (JsonProcessingException e) {
 65             log.warn("Parse Object to String error: {}", e.getMessage());
 66             return null;
 67         }
 68     }
 69 
 70     /**
 71      * json转java对象
 72      *
 73      * @param json
 74      * @param clazz
 75      * @param <T>
 76      * @return
 77      */
 78     public static <T> T string2Object(String json, Class<T> clazz) {
 79         if (StringUtils.isEmpty(json) || clazz == null) {
 80             return null;
 81         }
 82         try {
 83             return String.class.equals(clazz) ? (T) json : mapper.readValue(json, clazz);
 84         } catch (IOException e) {
 85             log.warn("Parse String to Object error: {}", e.getMessage());
 86             return null;
 87         }
 88     }
 89 
 90     /**
 91      * json转集合类型
 92      *
 93      * @param json
 94      * @param collectionClazz
 95      * @param elementClazz
 96      * @param <T>
 97      * @return
 98      */
 99     public static <T> T string2Object(String json, Class<?> collectionClazz, Class<?> elementClazz) {
100         if (StringUtils.isEmpty(json) || collectionClazz == null || elementClazz == null) {
101             return null;
102         }
103         JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClazz, elementClazz);
104         try {
105             return mapper.readValue(json, javaType);
106         } catch (IOException e) {
107             log.warn("Parse String to Object error: {}", e.getMessage());
108             return null;
109         }
110     }
111 
112     /**
113      * json转java对象
114      * 可以转集合类型,如 string2Object(json, new TypeReference<List<User>>() {})
115      *
116      * @param json
117      * @param typeReference
118      * @param <T>
119      * @return
120      */
121     public static <T> T string2Object(String json, TypeReference<T> typeReference) {
122         if (StringUtils.isEmpty(json) || typeReference == null) {
123             return null;
124         }
125         try {
126             return typeReference.getType().equals(String.class) ? (T) json : mapper.readValue(json, typeReference);
127         } catch (IOException e) {
128             log.warn("Parse String to Object error: {}", e.getMessage());
129             return null;
130         }
131     }
132 }

 

上一篇:NoClassDefFoundError


下一篇:日期前后端传值格式转换