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