PageRenderTime 754ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/Jackson.java

https://gitlab.com/sxyseo/dubbox
Java | 49 lines | 34 code | 6 blank | 9 comment | 6 complexity | 8a8ffd638a8bb3bd6887f45acbc1a0b3 MD5 | raw file
  1. package com.alibaba.dubbo.common.json;
  2. import com.alibaba.dubbo.common.extension.ExtensionLoader;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializationFeature;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.util.TimeZone;
  9. /**
  10. * @author dylan
  11. */
  12. public class Jackson {
  13. private static Logger logger = LoggerFactory.getLogger(Jackson.class);
  14. private static ObjectMapper objectMapper;
  15. private static JacksonObjectMapperProvider getJacksonProvider() {
  16. return ExtensionLoader.getExtensionLoader(JacksonObjectMapperProvider.class).getDefaultExtension();
  17. }
  18. /**
  19. * 获取object mapper
  20. *
  21. * @return
  22. */
  23. public static ObjectMapper getObjectMapper() {
  24. if (objectMapper == null) {
  25. JacksonObjectMapperProvider jacksonObjectMapperProvider = getJacksonProvider();
  26. if (jacksonObjectMapperProvider != null) {
  27. objectMapper = jacksonObjectMapperProvider.getObjectMapper();
  28. }
  29. }
  30. if (objectMapper == null) {
  31. logger.warn("load objectMapper failed, use default config.");
  32. buildDefaultObjectMapper();
  33. }
  34. return objectMapper;
  35. }
  36. private static synchronized void buildDefaultObjectMapper() {
  37. objectMapper = new ObjectMapper();
  38. objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  39. // objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
  40. objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  41. objectMapper.setTimeZone(TimeZone.getDefault());
  42. }
  43. }