JsonBinder.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package org.ccframe.commons.util;
  2. import java.io.IOException;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  7. import com.fasterxml.jackson.databind.DeserializationFeature;
  8. import com.fasterxml.jackson.databind.JsonNode;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.fasterxml.jackson.databind.node.NullNode;
  11. /**
  12. * JSON 转换器.(jackson)
  13. *
  14. * Jim
  15. */
  16. public class JsonBinder {
  17. private static Logger logger = LoggerFactory.getLogger(JsonBinder.class);
  18. private ObjectMapper mapper;
  19. private static JsonBinder normalBinder;
  20. private static JsonBinder nonNullBinder;
  21. public JsonNode toNode(String jsonString){
  22. try {
  23. return mapper.readTree(jsonString);
  24. } catch (IOException e) {
  25. logger.error("json parse error:" + jsonString,e);
  26. return NullNode.getInstance();
  27. }
  28. }
  29. public JsonNode objectToNode(Object object){
  30. return toNode(toJson(object));
  31. }
  32. private JsonBinder(Include inclusion) {
  33. mapper = new ObjectMapper();
  34. //设置输出时包含属性的风格
  35. mapper.setSerializationInclusion(inclusion);
  36. // mapper.getSerializationConfig().withSerializationInclusion(inclusion);
  37. //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
  38. mapper.getDeserializationConfig().without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  39. }
  40. /**
  41. * 创建输出全部属性到Json字符串的Binder.
  42. */
  43. public static synchronized JsonBinder buildNormalBinder() {
  44. if(normalBinder==null){
  45. normalBinder = new JsonBinder(Include.ALWAYS);
  46. }
  47. return normalBinder;
  48. }
  49. /**
  50. * 创建只输出非空属性到Json字符串的Binder.
  51. */
  52. public static synchronized JsonBinder buildNonNullBinder() {
  53. if(nonNullBinder==null){
  54. nonNullBinder = new JsonBinder(Include.NON_NULL);
  55. }
  56. return nonNullBinder;
  57. }
  58. /**
  59. * 如果JSON字符串为Null或"null"字符串,返回Null.
  60. * 如果JSON字符串为"[]",返回空集合.
  61. * 如果需要返回数组,使用数据类型如Product[].class
  62. * <p/>
  63. */
  64. public <T> T toBean(String jsonString, Class<T> clazz) {
  65. if (StringUtils.isEmpty(jsonString)) {
  66. return null;
  67. }
  68. try {
  69. return mapper.readValue(jsonString, clazz);
  70. } catch (IOException e) {
  71. logger.warn("parse json string error:" + jsonString, e);
  72. return null;
  73. }
  74. }
  75. /**
  76. * 如果对象为Null,返回"null".
  77. * 如果集合为空集合,返回"[]".
  78. */
  79. public String toJson(Object object) {
  80. try {
  81. return mapper.writeValueAsString(object);
  82. } catch (IOException e) {
  83. logger.warn("write to json string error:" + object, e);
  84. return null;
  85. }
  86. }
  87. /**
  88. * 取出Mapper做进一步的设置或使用其他序列化API.
  89. */
  90. public ObjectMapper getMapper() {
  91. return mapper;
  92. }
  93. }