APS排产排程系统之JSON

背景

因需对象转字符串,需要序列化工具, fastjson2因存在BUG排除,增加jackson通用配置

代码实现

package com.olivia.sdk.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.SneakyThrows;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

public class JSON {

  private static final ObjectMapper mapper = new ObjectMapper();

  static {
    AtomicLong index = new AtomicLong(0);
//    ObjectMapper mapper = new ObjectMapper();
    // 配置日期格式化
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    // 注册Java 8日期时间模块
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    // 创建 JavaTimeModule
    // 定义日期时间格式
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    // 注册 LocalDateTime 序列化器
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
    // 注册 LocalDateTime 反序列化器
    javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));

    mapper.registerModule(javaTimeModule);
    // 禁用将日期序列化为时间戳
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);


    // 创建一个 SimpleModule 并注册 ToStringSerializer 用于 long 类型
    SimpleModule module = new SimpleModule();
    module.addSerializer(Long.class, ToStringSerializer.instance);
    module.addSerializer(long.class, ToStringSerializer.instance);
    mapper.getSerializerProvider().setNullKeySerializer(new JsonSerializer<>() {
      @Override
      public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeFieldName("NULL_KEY_" + index.incrementAndGet());
      }
    });
    mapper.registerModules(List.of(module, javaTimeModule));

  }

  public static ObjectMapper getMapper() {
    return JSON.mapper;
  }

  @SneakyThrows
  public static <T> T readValue(String content, Class<T> valueType) {
    if (Objects.isNull(content) || content.trim().isEmpty()) {
      return null;
    }
    return mapper.readValue(content, valueType);
  }

  @SneakyThrows
  public static <T> T readValue(String content) {
    if (Objects.isNull(content) || content.trim().isEmpty()) {
      return null;
    }
    return mapper.readValue(content, new TypeReference<>() {
    });
  }

  @SneakyThrows
  public static <T> T readValue(String content, TypeReference<T> typeReference) {
    return mapper.readValue(content,typeReference);
  }


  @SneakyThrows
  public static String toJSONString(Object object) {
    return mapper.writeValueAsString(object);
  }

  @SneakyThrows
  public static <T> List<T> readList(String content, Class<T> clazz) {
    if (Objects.isNull(content) || content.trim().isEmpty()) {
      return List.of();
    }
    return mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
  }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值