package cn.meowrain.aioj.backend.framework.core.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;

/**
 * Jackson 全局配置
 * 解决 Long 类型在前端 JavaScript 精度丢失问题
 */
@AutoConfiguration
@ConditionalOnClass(ObjectMapper.class)
public class JacksonConfiguration {

	/**
	 * 自定义 Jackson ObjectMapper 配置
	 * 将 Long 和 long 类型序列化为 String
	 */
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		return builder -> {
			// 注册 JavaTimeModule 处理时间类型
			builder.modules(new JavaTimeModule());
			
			// Long 和 long 类型序列化为 String，避免前端精度丢失
			builder.serializerByType(Long.class, ToStringSerializer.instance);
			builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
		};
	}

}
