org.springblade.core.tool.jackson.JsonUtil源码,可参考编写个人jsonutil工具类

本文介绍了SpringBlade框架中org.springblade.core.tool.jackson.JsonUtil类的使用,包括对象序列化、反序列化为JSON字符串、字节、列表、Map等功能,以及异常处理和日期格式化的方法。
package org.springblade.core.tool.jackson;

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.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Exceptions;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.*;

/**
 * Jackson工具类
 *
 * @author Chill
 */
@Slf4j
public class JsonUtil {

   /**
    * 将对象序列化成json字符串
    *
    * @param value javaBean
    * @param <T>   T 泛型标记
    * @return jsonString json字符串
    */
   public static <T> String toJson(T value) {
      try {
         return getInstance().writeValueAsString(value);
      } catch (Exception e) {
         log.error(e.getMessage(), e);
      }
      return null;
   }

   /**
    * 将对象序列化成 json byte 数组
    *
    * @param object javaBean
    * @return jsonString json字符串
    */
   public static byte[] toJsonAsBytes(Object object) {
      try {
         return getInstance().writeValueAsBytes(object);
      } catch (JsonProcessingException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json反序列化成对象
    *
    * @param content   content
    * @param valueType class
    * @param <T>       T 泛型标记
    * @return Bean
    */
   public static <T> T parse(String content, Class<T> valueType) {
      try {
         return getInstance().readValue(content, valueType);
      } catch (Exception e) {
         log.error(e.getMessage(), e);
      }
      return null;
   }

   /**
    * 将json反序列化成对象
    *
    * @param content       content
    * @param typeReference 泛型类型
    * @param <T>           T 泛型标记
    * @return Bean
    */
   public static <T> T parse(String content, TypeReference<?> typeReference) {
      try {
         return getInstance().readValue(content, typeReference);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json byte 数组反序列化成对象
    *
    * @param bytes     json bytes
    * @param valueType class
    * @param <T>       T 泛型标记
    * @return Bean
    */
   public static <T> T parse(byte[] bytes, Class<T> valueType) {
      try {
         return getInstance().readValue(bytes, valueType);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }


   /**
    * 将json反序列化成对象
    *
    * @param bytes         bytes
    * @param typeReference 泛型类型
    * @param <T>           T 泛型标记
    * @return Bean
    */
   public static <T> T parse(byte[] bytes, TypeReference<?> typeReference) {
      try {
         return getInstance().readValue(bytes, typeReference);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json反序列化成对象
    *
    * @param in        InputStream
    * @param valueType class
    * @param <T>       T 泛型标记
    * @return Bean
    */
   public static <T> T parse(InputStream in, Class<T> valueType) {
      try {
         return getInstance().readValue(in, valueType);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json反序列化成对象
    *
    * @param in            InputStream
    * @param typeReference 泛型类型
    * @param <T>           T 泛型标记
    * @return Bean
    */
   public static <T> T parse(InputStream in, TypeReference<?> typeReference) {
      try {
         return getInstance().readValue(in, typeReference);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json反序列化成List对象
    * @param content content
    * @param valueTypeRef class
    * @param <T> T 泛型标记
    * @return List
    */
   public static <T> List<T> parseArray(String content, Class<T> valueTypeRef) {
      try {

         if (!StringUtil.startsWithIgnoreCase(content, StringPool.LEFT_SQ_BRACKET)) {
            content = StringPool.LEFT_SQ_BRACKET + content + StringPool.RIGHT_SQ_BRACKET;
         }

         List<Map<String, Object>> list = getInstance().readValue(content, new TypeReference<List<T>>() {
         });
         List<T> result = new ArrayList<>();
         for (Map<String, Object> map : list) {
            result.add(toPojo(map, valueTypeRef));
         }
         return result;
      } catch (IOException e) {
         log.error(e.getMessage(), e);
      }
      return null;
   }

   public static Map<String, Object> toMap(String content) {
      try {
         return getInstance().readValue(content, Map.class);
      } catch (IOException e) {
         log.error(e.getMessage(), e);
      }
      return null;
   }

   public static <T> Map<String, T> toMap(String content, Class<T> valueTypeRef) {
      try {
         Map<String, Map<String, Object>> map = getInstance().readValue(content, new TypeReference<Map<String, T>>() {
         });
         Map<String, T> result = new HashMap<>(16);
         for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            result.put(entry.getKey(), toPojo(entry.getValue(), valueTypeRef));
         }
         return result;
      } catch (IOException e) {
         log.error(e.getMessage(), e);
      }
      return null;
   }

   public static <T> T toPojo(Map fromValue, Class<T> toValueType) {
      return getInstance().convertValue(fromValue, toValueType);
   }

   /**
    * 将json字符串转成 JsonNode
    *
    * @param jsonString jsonString
    * @return jsonString json字符串
    */
   public static JsonNode readTree(String jsonString) {
      try {
         return getInstance().readTree(jsonString);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json字符串转成 JsonNode
    *
    * @param in InputStream
    * @return jsonString json字符串
    */
   public static JsonNode readTree(InputStream in) {
      try {
         return getInstance().readTree(in);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json字符串转成 JsonNode
    *
    * @param content content
    * @return jsonString json字符串
    */
   public static JsonNode readTree(byte[] content) {
      try {
         return getInstance().readTree(content);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   /**
    * 将json字符串转成 JsonNode
    *
    * @param jsonParser JsonParser
    * @return jsonString json字符串
    */
   public static JsonNode readTree(JsonParser jsonParser) {
      try {
         return getInstance().readTree(jsonParser);
      } catch (IOException e) {
         throw Exceptions.unchecked(e);
      }
   }

   public static ObjectMapper getInstance() {
      return JacksonHolder.INSTANCE;
   }

   private static class JacksonHolder {
      private static ObjectMapper INSTANCE = new JacksonObjectMapper();
   }

   public static class JacksonObjectMapper extends ObjectMapper {
      private static final long serialVersionUID = 4288193147502386170L;

      private static final Locale CHINA = Locale.CHINA;

      public JacksonObjectMapper() {
         super();
         //设置地点为中国
         super.setLocale(CHINA);
         //去掉默认的时间戳格式
         super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
         //设置为中国上海时区
         super.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
         //序列化时,日期的统一格式
         super.setDateFormat(new SimpleDateFormat(DateUtil.PATTERN_DATETIME, Locale.CHINA));
         //序列化处理
         super.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
         super.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
         super.findAndRegisterModules();
         //失败处理
         super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
         super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         //单引号处理
         super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
         //反序列化时,属性不存在的兼容处理s
         super.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
         //日期格式化
         super.registerModule(new BladeJavaTimeModule());
         super.findAndRegisterModules();
      }

      @Override
      public ObjectMapper copy() {
         return super.copy();
      }
   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值