All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.json.TioJsonKit Maven / Gradle / Ivy

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.json;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import com.litongjava.tio.utils.hutool.StrUtil;
import com.litongjava.tio.utils.map.SyncWriteMap;

/**
 * Tio JsonKit
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TioJsonKit {

  public static final TioJsonKit me = new TioJsonKit();

  // 缓存 ToJson 对象
  protected static SyncWriteMap, TioToJson> cache = new SyncWriteMap<>(512, 0.25F);

  // StringBuilder 最大缓冲区大小
  protected static int maxBufferSize = 1024 * 512;

  // 将 Model 当成 Bean 只对 getter 方法进行转换
  protected static boolean treatModelAsBean = false;

  // 是否跳过 null 值的字段,不对其进行转换
  protected static boolean skipNullValueField = false;

  // long to string
  protected static boolean longToString = true;

  // 对 Model 和 Record 的字段名进行转换的函数。例如转成驼峰形式对 oracle 支持更友好
  protected static Function modelAndRecordFieldNameConverter = null;

  protected static Function> toJsonFactory = null;

  public TioToJson getToJson(Object object) {
    TioToJson ret = cache.get(object.getClass());
    if (ret == null) {
      ret = createToJson(object);
      cache.putIfAbsent(object.getClass(), ret);
    }
    return ret;
  }

  /**
   * 添加 ToJson 转换接口实现类,自由定制任意类型数据的转换规则
   * 
   * 例子:
   *     ToJson toJson = (value, depth, ret) -> {
   *       ret.addLong(value.getTime());
   *     };
   *     
   *     TioJson.addToJson(Timestamp.class, toJson);
   *     
   *     以上代码为 Timestamp 类型的 json 转换定制了转换规则
   *     将其转换成了 long 型数据
   * 
*/ public static void addToJson(Class type, TioToJson toJson) { Objects.requireNonNull(type, "type can not be null"); Objects.requireNonNull(toJson, "toJson can not be null"); cache.put(type, toJson); } protected TioToJson createToJson(Object value) { // 优先使用 toJsonFactory 创建 ToJson 实例,方便用户优先接管 ToJson 转换器的创建 if (toJsonFactory != null) { TioToJson tj = toJsonFactory.apply(value); if (tj != null) { return tj; } } // 基础类型 ----------------------------------------- if (value instanceof String) { return new StrToJson(); } if (value instanceof Number) { if (value instanceof Integer) { return new IntToJson(); } if (value instanceof Long) { return new LongToJson(); } if (value instanceof Double) { return new DoubleToJson(); } if (value instanceof Float) { return new FloatToJson(); } return new NumberToJson(); } if (value instanceof Boolean) { return new BooleanToJson(); } if (value instanceof Character) { return new CharacterToJson(); } if (value instanceof Enum) { return new EnumToJson(); } if (value instanceof java.util.Date) { if (value instanceof Timestamp) { return new TimestampToJson(); } if (value instanceof Time) { return new TimeToJson(); } return new DateToJson(); } if (value instanceof Temporal) { if (value instanceof LocalDateTime) { return new LocalDateTimeToJson(); } if (value instanceof LocalDate) { return new LocalDateToJson(); } if (value instanceof LocalTime) { return new LocalTimeToJson(); } } if (value instanceof Map) { return new MapToJson(); } if (value instanceof Collection) { return new CollectionToJson(); } if (value.getClass().isArray()) { return new ArrayToJson(); } if (value instanceof Enumeration) { return new EnumerationToJson(); } if (value instanceof Iterator) { return new IteratorToJson(); } if (value instanceof Iterable) { return new IterableToJson(); } BeanToJson beanToJson = buildBeanToJson(value); if (beanToJson != null) { return beanToJson; } return new UnknownToJson(); } public static boolean checkDepth(int depth, JsonResult ret) { if (depth < 0) { ret.addNull(); return true; } else { return false; } } static class StrToJson implements TioToJson { public void toJson(String str, int depth, JsonResult ret) { escape(str, ret.sb); } } static class CharacterToJson implements TioToJson { public void toJson(Character ch, int depth, JsonResult ret) { escape(ch.toString(), ret.sb); } } static class IntToJson implements TioToJson { public void toJson(Integer value, int depth, JsonResult ret) { ret.addInt(value); } } static class LongToJson implements TioToJson { public void toJson(Long value, int depth, JsonResult ret) { ret.addLong(value); } } static class DoubleToJson implements TioToJson { public void toJson(Double value, int depth, JsonResult ret) { if (value.isInfinite() || value.isNaN()) { ret.addNull(); } else { ret.addDouble(value); } } } static class FloatToJson implements TioToJson { public void toJson(Float value, int depth, JsonResult ret) { if (value.isInfinite() || value.isNaN()) { ret.addNull(); } else { ret.addFloat(value); } } } // 接管 int、long、double、float 之外的 Number 类型 static class NumberToJson implements TioToJson { public void toJson(Number value, int depth, JsonResult ret) { ret.addNumber(value); } } static class BooleanToJson implements TioToJson { public void toJson(Boolean value, int depth, JsonResult ret) { ret.addBoolean(value); } } static class EnumToJson implements TioToJson { public void toJson(Enum en, int depth, JsonResult ret) { ret.addEnum(en); } } static class TimestampToJson implements TioToJson { public void toJson(Timestamp ts, int depth, JsonResult ret) { ret.addTimestamp(ts); } } static class TimeToJson implements TioToJson