/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/Jackson.java
Java | 49 lines | 34 code | 6 blank | 9 comment | 6 complexity | 8a8ffd638a8bb3bd6887f45acbc1a0b3 MD5 | raw file
- package com.alibaba.dubbo.common.json;
- import com.alibaba.dubbo.common.extension.ExtensionLoader;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializationFeature;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.TimeZone;
- /**
- * @author dylan
- */
- public class Jackson {
- private static Logger logger = LoggerFactory.getLogger(Jackson.class);
- private static ObjectMapper objectMapper;
- private static JacksonObjectMapperProvider getJacksonProvider() {
- return ExtensionLoader.getExtensionLoader(JacksonObjectMapperProvider.class).getDefaultExtension();
- }
- /**
- * 获取object mapper
- *
- * @return
- */
- public static ObjectMapper getObjectMapper() {
- if (objectMapper == null) {
- JacksonObjectMapperProvider jacksonObjectMapperProvider = getJacksonProvider();
- if (jacksonObjectMapperProvider != null) {
- objectMapper = jacksonObjectMapperProvider.getObjectMapper();
- }
- }
- if (objectMapper == null) {
- logger.warn("load objectMapper failed, use default config.");
- buildDefaultObjectMapper();
- }
- return objectMapper;
- }
- private static synchronized void buildDefaultObjectMapper() {
- objectMapper = new ObjectMapper();
- objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
- // objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
- objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- objectMapper.setTimeZone(TimeZone.getDefault());
- }
- }