JsonUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright Notice:
  3. * Copyright 1998-2008, Huawei Technologies Co., Ltd. ALL Rights Reserved.
  4. *
  5. * Warning: This computer software sourcecode is protected by copyright law
  6. * and international treaties. Unauthorized reproduction or distribution
  7. * of this sourcecode, or any portion of it, may result in severe civil and
  8. * criminal penalties, and will be prosecuted to the maximum extent
  9. * possible under the law.
  10. */
  11. package com.xzl.web.utils;
  12. import com.fasterxml.jackson.databind.DeserializationFeature;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14. import com.fasterxml.jackson.databind.SerializationFeature;
  15. import com.fasterxml.jackson.databind.node.ObjectNode;
  16. import java.io.IOException;
  17. public class JsonUtil {
  18. private static ObjectMapper objectMapper;
  19. static {
  20. objectMapper = new ObjectMapper();
  21. // 设置FAIL_ON_EMPTY_BEANS属性,当序列化空对象不要抛异常
  22. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  23. // 设置FAIL_ON_UNKNOWN_PROPERTIES属性,当JSON字符串中存在Java对象没有的属性,忽略
  24. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  25. }
  26. /**
  27. * Convert Object to JsonString
  28. *
  29. * @param jsonObj
  30. * @return
  31. */
  32. public static String jsonObj2Sting(Object jsonObj) {
  33. String jsonString = null;
  34. try {
  35. jsonString = objectMapper.writeValueAsString(jsonObj);
  36. } catch (IOException e) {
  37. System.out.printf("pasre json Object[{}] to string failed.", jsonString);
  38. }
  39. return jsonString;
  40. }
  41. /**
  42. * Convert JsonString to Simple Object
  43. *
  44. * @param jsonString
  45. * @param cls
  46. * @return
  47. */
  48. public static <T> T jsonString2SimpleObj(String jsonString, Class<T> cls) {
  49. T jsonObj = null;
  50. try {
  51. jsonObj = objectMapper.readValue(jsonString, cls);
  52. } catch (IOException e) {
  53. System.out.printf("pasre json Object[{}] to string failed.", jsonString);
  54. }
  55. return jsonObj;
  56. }
  57. /**
  58. * Method that will convert object to the ObjectNode.
  59. *
  60. * @param value the source data; if null, will return null.
  61. * @return the ObjectNode data after converted.
  62. * @throws JsonException
  63. */
  64. public static <T> ObjectNode convertObject2ObjectNode(T object) throws Exception {
  65. if (null == object) {
  66. return null;
  67. }
  68. ObjectNode objectNode = null;
  69. if (object instanceof String) {
  70. objectNode = convertJsonStringToObject((String) object, ObjectNode.class);
  71. } else {
  72. objectNode = convertValue(object, ObjectNode.class);
  73. }
  74. return objectNode;
  75. }
  76. /**
  77. * Method that will convert the json string to destination by the type(cls).
  78. *
  79. * @param jsonString the source json string; if null, will return null.
  80. * @param cls the destination data type.
  81. * @return
  82. * @throws JsonException
  83. */
  84. public static <T> T convertJsonStringToObject(String jsonString, Class<T> cls) throws Exception {
  85. if (StringUtil.strIsNullOrEmpty(jsonString)) {
  86. return null;
  87. }
  88. try {
  89. T object = objectMapper.readValue(jsonString, cls);
  90. return object;
  91. } catch (Exception e) {
  92. throw new Exception(e);
  93. }
  94. }
  95. /**
  96. * Method that will convert from given value into instance of given value type.
  97. *
  98. * @param fromValue
  99. * @param toValueType
  100. * @return
  101. * @throws JsonException
  102. */
  103. private static <T> T convertValue(Object fromValue, Class<T> toValueType) throws Exception {
  104. try {
  105. return objectMapper.convertValue(fromValue, toValueType);
  106. } catch (IllegalArgumentException e) {
  107. throw new Exception(e);
  108. }
  109. }
  110. }