123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- package com.kmall.admin.utils.jackson;
- import com.fasterxml.jackson.annotation.JsonFilter;
- import com.fasterxml.jackson.annotation.JsonInclude;
- import com.fasterxml.jackson.core.JsonGenerationException;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonParseException;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fasterxml.jackson.databind.JsonMappingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializationFeature;
- import com.fasterxml.jackson.databind.module.SimpleModule;
- import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
- import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Reader;
- import java.io.StringWriter;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- import java.util.Locale;
- /**
- * @author Scott Chen
- * @version 1.0
- * 2017-09-18 18:42
- */
- public class JacksonUtil {
- private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
- private static ObjectMapper objectMapper;
- private static final String DYNC_INCLUDE = "DYNC_INCLUDE";//包含的标识
- private static final String DYNC_EXCLUDE = "DYNC_EXCLUDE";//过滤的标识
- @JsonFilter(DYNC_EXCLUDE)
- interface DynamicExclude{
- }
- @JsonFilter(DYNC_INCLUDE)
- interface DynamicInclude{
- }
- /**
- * 禁止调用无参构造
- *
- * @throws IllegalAccessException
- */
- private JacksonUtil() throws IllegalAccessException {
- throw new IllegalAccessException("Can't create an instance!");
- }
- /**
- * 懒惰单例模式得到ObjectMapper实例
- * 此对象为Jackson的核心
- */
- private static final ObjectMapper objectMapper() {
- if (objectMapper== null){
- objectMapper= new ObjectMapper();
- // 格式化国家环境指定
- objectMapper.setLocale(Locale.SIMPLIFIED_CHINESE);
- //序列化时,如果没有为类型找到访问者,则会抛出异常以将其指定为非可序列化类型; 如果禁用,它们将被序列化为空对象
- objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
- //反序列化时,遇到未知属性是否抛JsonMappingException异常.默认JsonMappingException
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- //设置null值不参与序列化(字段不被显示)
- objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- //序列化Unicode编码非ASCII字符
- //objectMapper.registerModule(unicodeSerModule());
- }
- return objectMapper;
- }
- /**
- * ObjectMapper 实例
- * @return
- */
- public static ObjectMapper getObjectMapper() {
- return objectMapper();
- }
- /**
- * 创建Json处理器的静态方法
- * 用于序列化
- * @param content Json字符串
- * @return
- */
- private static JsonParser getParser(String content){
- try{
- return objectMapper().getFactory().createParser(content);
- }catch (IOException ioe){
- return null;
- }
- }
- /**
- * 创建Byte Json处理器的静态方法
- * 用于序列化
- * @param data byte array
- * @return
- */
- private static JsonParser getByteParser(byte[] data){
- try{
- return objectMapper().getFactory().createParser(data);
- }catch (IOException ioe){
- return null;
- }
- }
- /**
- * 创建Reader Json处理器的静态方法
- * 用于序列化
- * @param r Reader
- * @return
- */
- private static JsonParser getReaderParser(Reader r){
- try{
- return objectMapper().getFactory().createParser(r);
- }catch (IOException ioe){
- return null;
- }
- }
- /**
- * 创建InputStream Json处理器的静态方法
- * 用于序列化
- * @param in InputStream
- * @return
- */
- private static JsonParser getReaderParser(InputStream in){
- try{
- return objectMapper().getFactory().createParser(in);
- }catch (IOException ioe){
- return null;
- }
- }
- /**
- * 创建Json生成器的静态方法, 使用标准输出
- * 用于反序列化
- * @return
- */
- private static JsonGenerator getGenerator(StringWriter sw){
- try{
- return objectMapper().getFactory().createGenerator(sw);
- }catch (IOException e) {
- return null;
- }
- }
- /**
- * Json对象序列化
- */
- public static String toJson(Object obj){
- StringWriter sw= new StringWriter();
- JsonGenerator jsonGen= getGenerator(sw);
- if (jsonGen== null){
- try {
- sw.close();
- } catch (IOException e) {
- }
- return null;
- }
- try {
- //由于在getGenerator方法中指定了OutputStream为sw
- //因此调用writeObject会将数据输出到sw
- jsonGen.writeObject(obj);
- //由于采用流式输出 在输出完毕后务必清空缓冲区并关闭输出流
- jsonGen.flush();
- jsonGen.close();
- return sw.toString();
- } catch (JsonGenerationException jge) {
- logger.error("toJSON序列化失败, 异常类型【JsonGenerationException】,错误原因:{}", jge.getMessage());
- } catch (IOException ioe) {
- logger.error("toJSON序列化失败, 异常类型【IOException】, 错误原因:{}", ioe.getMessage());
- ioe.printStackTrace();
- }
- return null;
- }
- /**
- * 将Json Byte反序列化成对象
- *
- * @param data
- * @param clazz
- * @return
- */
- public static <T> T fromByteJson(byte[] data, Class<T> clazz) {
- try {
- JsonParser jp= getByteParser(data);
- return jp.readValueAs(clazz);
- } catch (JsonParseException e){
- logger.error(String.format("fromByteJson反序列化失败, 异常类型【JsonParseException】, 错误原因:{}", e.getMessage()));
- } catch (JsonMappingException e){
- logger.error(String.format("fromByteJson反序列化失败, 异常类型【JsonMappingException】, 错误原因:{}", e.getMessage()));
- } catch (IOException e){
- logger.error(String.format("fromByteJson反序列化失败, 异常类型【IOException】, 错误原因:{}", e.getMessage()));
- }
- return null;
- }
- /**
- * 将Json String反序列化成对象
- *
- * @param json
- * @param clazz
- * @return
- */
- public static <T> T fromStringJson(String json, Class<T> clazz) {
- try {
- JsonParser jp= getParser(json);
- return (T) jp.readValueAs(clazz);
- } catch (JsonParseException e) {
- logger.error(String.format("fromStringJson反序列化失败, 异常类型【JsonParseException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- } catch (JsonMappingException e) {
- logger.error(String.format("fromStringJson反序列化失败, 异常类型【JsonMappingException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- } catch (IOException e) {
- logger.error(String.format("fromStringJson反序列化失败, 异常类型【IOException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- }
- return null;
- }
- /**
- * 将Json Array或List反序列化为对象
- *
- * @param json
- * @return
- */
- public static <T> T fromListJson(String json, TypeReference<?> typeReference) {
- try {
- //写成List.class是不行的
- JsonParser jp= getParser(json);
- return (T) jp.readValueAs(typeReference);
- } catch (JsonParseException e) {
- logger.error(String.format("fromListJson反序列化失败, 异常类型【JsonParseException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- } catch (JsonMappingException e) {
- logger.error(String.format("fromListJson反序列化失败, 异常类型【JsonMappingException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- } catch (IOException e) {
- logger.error(String.format("fromListJson反序列化失败, 异常类型【IOException】, 错误原因:{}", e.getMessage()));
- logger.error("decode(String, Class<T>)", e);
- }
- return null;
- }
- }
|