JsonUtils.java 832 B

123456789101112131415161718192021222324252627282930313233
  1. package com.wechi.adweb.bridge.util;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import lombok.extern.slf4j.Slf4j;
  5. /**
  6. * @author wfansh
  7. */
  8. @Slf4j
  9. public class JsonUtils {
  10. private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  11. public static String toJson(Object object) {
  12. try {
  13. return OBJECT_MAPPER.writeValueAsString(object);
  14. } catch (JsonProcessingException e) {
  15. log.error(e.getMessage(), e);
  16. return null;
  17. }
  18. }
  19. public <T> T fromJson(String json, Class<T> clazz) {
  20. try {
  21. return OBJECT_MAPPER.readValue(json, clazz);
  22. } catch (JsonProcessingException e) {
  23. log.error(e.getMessage(), e);
  24. return null;
  25. }
  26. }
  27. }