forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJSON.java
More file actions
156 lines (143 loc) · 5.56 KB
/
JSON.java
File metadata and controls
156 lines (143 loc) · 5.56 KB
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
151
152
153
154
155
156
package org.tron.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Drop-in replacement for {@code com.alibaba.fastjson.JSON}.
* Swap the import line; no other source changes required for basic usages.
*
* <p>All static methods delegate to a shared, thread-safe Jackson {@link ObjectMapper} that is
* configured to match the lenient parsing behavior historically provided by Fastjson:
* <ul>
* <li>Unquoted field names and single-quoted strings are accepted.</li>
* <li>Unknown properties are ignored (Fastjson default).</li>
* <li>Floating-point numbers are mapped to {@link java.math.BigDecimal} for precision.</li>
* <li>Case-insensitive property matching is enabled.</li>
* </ul>
*/
public final class JSON {
/**
* Shared, fully-configured Jackson mapper. Exposed for callers that hold a mapper reference.
*/
public static final ObjectMapper MAPPER = JsonMapper.builder()
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.build();
private JSON() {
}
// -------------------------------------------------------------------------
// parseObject
// -------------------------------------------------------------------------
/**
* Parses a JSON object string and returns a {@link JSONObject}.
* Mirrors {@code JSON.parseObject(String)} and {@code JSONObject.parseObject(String)}.
*/
public static JSONObject parseObject(String text) {
if (text == null || text.isEmpty()) {
return new JSONObject();
}
try {
return new JSONObject((ObjectNode) MAPPER.readTree(text));
} catch (Exception e) {
throw new JSONException("Failed to parse JSON object: " + e.getMessage(), e);
}
}
/**
* Parses a JSON string and deserializes it into the given Java type.
* Mirrors {@code JSON.parseObject(String, Class)}.
*/
public static <T> T parseObject(String text, Class<T> clazz) throws JsonProcessingException {
if (text == null || text.isEmpty()) {
return null;
}
return MAPPER.readValue(text, clazz);
}
// -------------------------------------------------------------------------
// parse (validate / generic parse — callers typically ignore the return value)
// -------------------------------------------------------------------------
/**
* Parses any valid JSON value. Returns {@code null} on failure (mirrors Fastjson behaviour).
* Mirrors {@code JSON.parse(String)}.
* @return a Jackson {@link JsonNode} representing the parsed JSON,
* or {@code null} if parsing fails.
*/
public static JsonNode parse(String text) {
if (text == null || text.isEmpty()) {
return null;
}
try {
return MAPPER.readTree(text);
} catch (Exception e) {
return null;
}
}
/**
* Parses a JSON array string and returns a {@link JSONArray}.
* Mirrors {@code JSON.parseArray(String)} and {@code JSONArray.parseArray(String)}.
*/
public static JSONArray parseArray(String text) {
return JSONArray.parseArray(text);
}
// -------------------------------------------------------------------------
// toJSONString
// -------------------------------------------------------------------------
/**
* Serializes an object to a compact JSON string.
* Mirrors {@code JSON.toJSONString(Object)}.
*/
public static String toJSONString(Object obj) {
if (obj == null) {
return "null";
}
// Unwrap our own wrapper types so the inner Jackson node is serialized
if (obj instanceof JSONObject) {
return ((JSONObject) obj).unwrap().toString();
}
if (obj instanceof JSONArray) {
return ((JSONArray) obj).unwrap().toString();
}
try {
return MAPPER.writeValueAsString(obj);
} catch (Exception e) {
throw new JSONException("Failed to serialise object: " + e.getMessage(), e);
}
}
/**
* Serializes an object to a JSON string, optionally pretty-printed.
* Mirrors {@code JSON.toJSONString(Object, boolean)}.
*/
public static String toJSONString(Object obj, boolean prettyFormat) {
if (!prettyFormat) {
return toJSONString(obj);
}
if (obj == null) {
return "null";
}
try {
if (obj instanceof JSONObject) {
return MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(((JSONObject) obj).unwrap());
}
if (obj instanceof JSONArray) {
return MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(((JSONArray) obj).unwrap());
}
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (Exception e) {
throw new JSONException("Failed to serialise object: " + e.getMessage(), e);
}
}
}