UJson.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.genersoft.iot.vmp.utils;
  2. import com.fasterxml.jackson.databind.DeserializationFeature;
  3. import com.fasterxml.jackson.databind.JsonNode;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.node.ObjectNode;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.Objects;
  12. /**
  13. * @author gaofuwang
  14. * @version 1.0
  15. * @date 2022/3/11 10:17
  16. */
  17. public class UJson {
  18. private static Logger logger = LoggerFactory.getLogger(UJson.class);
  19. public static final ObjectMapper JSON_MAPPER = new ObjectMapper();
  20. static {
  21. JSON_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
  22. }
  23. private ObjectNode node;
  24. public UJson(){
  25. this.node = JSON_MAPPER.createObjectNode();
  26. }
  27. public UJson(String json){
  28. if(StringUtils.isBlank(json)){
  29. this.node = JSON_MAPPER.createObjectNode();
  30. }else{
  31. try {
  32. this.node = JSON_MAPPER.readValue(json, ObjectNode.class);
  33. }catch (Exception e){
  34. logger.error(e.getMessage(), e);
  35. this.node = JSON_MAPPER.createObjectNode();
  36. }
  37. }
  38. }
  39. public UJson(ObjectNode node){
  40. this.node = node;
  41. }
  42. public String asText(String key){
  43. JsonNode jsonNode = node.get(key);
  44. if(Objects.isNull(jsonNode)){
  45. return "";
  46. }
  47. return jsonNode.asText();
  48. }
  49. public String asText(String key, String defaultVal){
  50. JsonNode jsonNode = node.get(key);
  51. if(Objects.isNull(jsonNode)){
  52. return "";
  53. }
  54. return jsonNode.asText(defaultVal);
  55. }
  56. public UJson put(String key, String value){
  57. this.node.put(key, value);
  58. return this;
  59. }
  60. public UJson put(String key, Integer value){
  61. this.node.put(key, value);
  62. return this;
  63. }
  64. public static UJson json(){
  65. return new UJson();
  66. }
  67. public static UJson json(String json){
  68. return new UJson(json);
  69. }
  70. public static <T> T readJson(String json, Class<T> clazz){
  71. if(StringUtils.isBlank(json)){
  72. return null;
  73. }
  74. try {
  75. return JSON_MAPPER.readValue(json, clazz);
  76. }catch (Exception e){
  77. logger.error(e.getMessage(), e);
  78. return null;
  79. }
  80. }
  81. public static String writeJson(Object object) {
  82. try{
  83. return JSON_MAPPER.writeValueAsString(object);
  84. }catch (Exception e){
  85. logger.error(e.getMessage(), e);
  86. return "";
  87. }
  88. }
  89. @Override
  90. public String toString() {
  91. return node.toString();
  92. }
  93. public int asInt(String key, int defValue) {
  94. JsonNode jsonNode = this.node.get(key);
  95. if(Objects.isNull(jsonNode)){
  96. return defValue;
  97. }
  98. return jsonNode.asInt(defValue);
  99. }
  100. public UJson getSon(String key) {
  101. JsonNode sonNode = this.node.get(key);
  102. if(Objects.isNull(sonNode)){
  103. return new UJson();
  104. }
  105. return new UJson((ObjectNode) sonNode);
  106. }
  107. public UJson set(String key, ObjectNode sonNode) {
  108. this.node.set(key, sonNode);
  109. return this;
  110. }
  111. public UJson set(String key, UJson sonNode) {
  112. this.node.set(key, sonNode.node);
  113. return this;
  114. }
  115. public Iterator<Map.Entry<String, JsonNode>> fields() {
  116. return this.node.fields();
  117. }
  118. public ObjectNode getNode() {
  119. return this.node;
  120. }
  121. public UJson setAll(UJson json) {
  122. this.node.setAll(json.node);
  123. return this;
  124. }
  125. }