package cn.web.config;

import java.util.TimeZone;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import cn.constant.TimeFormat;

@Configuration
public class JacksonConfig {

	@Bean
	public ObjectMapper objectMapper()
	{
		ObjectMapper objectMapper = new ObjectMapper()
				//设置序列化反序列化采用直接处理字段的方式，不依赖setter 和 getter
				//.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
				//.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
				/**显示格式不同：SerializationFeature.INDENT_OUTPUT SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS*/
				.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, Boolean.TRUE)
				//序列化设置 关闭日志输出为时间戳的设置
				.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false)
				//忽略空Bean转json的错误
				.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false)
				//反序列化设置 关闭反序列化时Jackson发现无法找到对应的对象字段，便会抛出UnrecognizedPropertyException: Unrecognized field xxx异常
				.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
				//"Asia/Shanghai"
				.setTimeZone(TimeZone.getTimeZone("GMT+8"))
				//所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
				.setDateFormat(TimeFormat.SDF_YYYYMMDDHMS)
				// 自动查找并注册Java 8相关模块
				.findAndRegisterModules();
		
		return objectMapper;
	}
}