content: package com.ncst.seckill.util;
  
  import com.alibaba.fastjson.JSON;
  
  /**
   * @Date 2020/10/14 10:01
   * @Author by LSY
   * @Description
   */
  public class JsonUtils {
      public static <T> String beanToString(T value) {
          if (value == null) {
              return null;
          }
          Class<?> clazz = value.getClass();
          if (clazz == int.class || clazz == Integer.class) {
              return "" + value;
          } else if (clazz == String.class) {
              return (String) value;
          } else if (clazz == long.class || clazz == Long.class) {
              return "" + value;
          } else {
              return JSON.toJSONString(value);
          }
      }
  
      @SuppressWarnings("unchecked")
      public static <T> T stringToBean(String str, Class<T> clazz) {
          if (str == null || str.length() <= 0 || clazz == null) {
              return null;
          }
          if (clazz == int.class || clazz == Integer.class) {
              return (T) Integer.valueOf(str);
          } else if (clazz == String.class) {
              return (T) str;
          } else if (clazz == long.class || clazz == Long.class) {
              return (T) Long.valueOf(str);
          } else {
              return JSON.toJavaObject(JSON.parseObject(str), clazz);
          }
      }
  
  }
  