jackson使用

<!-- jackson start -->

    <dependency>

  <groupId>com.fasterxml.jackson.core</groupId>

  <artifactId>jackson-core</artifactId>

  <version>2.6.3</version>

    </dependency>

   

    <dependency>

  <groupId>com.fasterxml.jackson.core</groupId>

  <artifactId>jackson-databind</artifactId>

  <version>2.6.3</version>

    </dependency>

    <dependency>

  <groupId>com.fasterxml.jackson.core</groupId>

  <artifactId>jackson-annotations</artifactId>

  <version>2.6.3</version>

    </dependency>

    <!-- jackson end -->

 

 

  1. import java.io.IOException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.TimeZone;  
  4.   
  5. import org.apache.log4j.Logger;  
  6.   
  7. import com.fasterxml.jackson.annotation.JsonInclude.Include;  
  8. import com.fasterxml.jackson.core.JsonGenerationException;  
  9. import com.fasterxml.jackson.core.JsonParseException;  
  10. import com.fasterxml.jackson.core.type.TypeReference;  
  11. import com.fasterxml.jackson.databind.DeserializationFeature;  
  12. import com.fasterxml.jackson.databind.JsonMappingException;  
  13. import com.fasterxml.jackson.databind.ObjectMapper;  
  14. import com.fasterxml.jackson.databind.SerializationFeature;  
  15.   
  16. public class JsonUtils {  
  17.     private static Logger logger = Logger.getLogger(JsonUtils.class);  
  18.     private static final ObjectMapper objectMapper;  
  19.     static {  
  20.         objectMapper = new ObjectMapper();  
  21.         //去掉默认的时间戳格式  
  22.         objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);  
  23.         //设置为中国上海时区  
  24.         objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));  
  25.         objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);  
  26.         //空值不序列化  
  27.         objectMapper.setSerializationInclusion(Include.NON_NULL);  
  28.         //反序列化时,属性不存在的兼容处理  
  29.         objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);  
  30.         //序列化时,日期的统一格式  
  31.         objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));  
  32.   
  33.         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);  
  34.         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
  35.         //单引号处理  
  36.         objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);  
  37.     }  
  38.   
  39.     public static <T> T toObject(String json, Class<T> clazz) {  
  40.         try {  
  41.             return objectMapper.readValue(json, clazz);  
  42.         } catch (JsonParseException e) {  
  43.             logger.error(e.getMessage(), e);  
  44.         } catch (JsonMappingException e) {  
  45.             logger.error(e.getMessage(), e);  
  46.         } catch (IOException e) {  
  47.             logger.error(e.getMessage(), e);  
  48.         }  
  49.         return null;  
  50.     }  
  51.   
  52.     public static <T> String toJson(T entity) {  
  53.         try {  
  54.             return objectMapper.writeValueAsString(entity);  
  55.         } catch (JsonGenerationException e) {  
  56.             logger.error(e.getMessage(), e);  
  57.         } catch (JsonMappingException e) {  
  58.             logger.error(e.getMessage(), e);  
  59.         } catch (IOException e) {  
  60.             logger.error(e.getMessage(), e);  
  61.         }  
  62.         return null;  
  63.     }  
  64.   
  65.     public static <T> T toCollection(String json, TypeReference<T> typeReference) {  
  66.         try {  
  67.             return objectMapper.readValue(json, typeReference);  
  68.         } catch (JsonParseException e) {  
  69.             logger.error(e.getMessage(), e);  
  70.         } catch (JsonMappingException e) {  
  71.             logger.error(e.getMessage(), e);  
  72.         } catch (IOException e) {  
  73.             logger.error(e.getMessage(), e);  
  74.         }  
  75.         return null;  
  76.     }  
  77.   
  78. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值