content: package com.moti.utils;
  
  import com.alibaba.fastjson.JSON;
  
  /**
   * @ClassName: BeanUtils
   * @Description: TODO
   * @author: 莫提
   * @date 2020/10/9 17:30
   * @Version: 1.0
   **/
  public class BeanUtils {
      /**
       * bean 转 String
       */
      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);
          }
      }
  
  
      /**
       * string 转 bean
       */
      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);
          }
      }
  }
  