-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathJSON.java
More file actions
148 lines (137 loc) · 5.65 KB
/
JSON.java
File metadata and controls
148 lines (137 loc) · 5.65 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
package org.tron.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
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;
import org.tron.core.Constant;
/**
* Drop-in replacement for {@code com.alibaba.fastjson.JSON}.
*
* @deprecated Compatibility shim from the fastjson removal. New code should use
* Jackson directly ({@link com.fasterxml.jackson.databind.ObjectMapper},
* {@link com.fasterxml.jackson.databind.JsonNode}) instead of this helper.
*/
@Deprecated
public final class JSON {
static final ObjectMapper MAPPER = JsonMapper.builder(buildFactory())
// Fastjson Feature.AllowUnQuotedFieldNames (default ON)
.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES)
// Fastjson Feature.AllowSingleQuotes (default ON)
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)
// Partial compatibility with Fastjson Feature.AllowArbitraryCommas:
// this only covers a single trailing comma like {"a":1,} or [1,2,].
// Repeated/arbitrary commas like {"a":1,,,,} and [1,,2] remain rejected.
.enable(JsonReadFeature.ALLOW_TRAILING_COMMA)
// Fastjson accepts a leading plus sign for numbers (for example +123, +0.5)
.enable(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS)
// Partial compatibility for Fastjson's asymmetric decimal behavior:
// Fastjson accepts +.5 but rejects .5 by default. Jackson cannot model only
// the signed form, so enabling this also accepts .5.
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
// Fastjson accepts a trailing decimal point for numbers (for example 5.)
.enable(JsonReadFeature.ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS)
// Fastjson accepts leading zeros for numbers (for example 007)
.enable(JsonReadFeature.ALLOW_LEADING_ZEROS_FOR_NUMBERS)
// Fastjson accepts unescaped control chars in strings (for example raw tab/newline)
.enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS)
// Fastjson accepts Java-style comments (// and /* */)
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
// Fastjson Feature.UseBigDecimal (default ON)
// https://github.com/alibaba/fastjson/wiki/deserialize_disable_bigdecimal_cn
.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true)
// Fastjson Feature.IgnoreNotMatch (default ON) — unknown fields silently ignored
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
// Fastjson serializes empty beans as "{}" without error
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
// Fastjson omits null-valued fields by default (WriteMapNullValue is OFF by default)
// https://github.com/alibaba/fastjson/wiki/WriteNull_cn
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build();
private static JsonFactory buildFactory() {
return JsonFactory.builder().streamReadConstraints(StreamReadConstraints.builder()
.maxNestingDepth(Constant.MAX_NESTING_DEPTH)
.maxTokenCount(Constant.MAX_TOKEN_COUNT)
.build()).build();
}
private JSON() {
}
/**
* Returns {@code true} when {@code text} is null, blank, or a
* lowercase {@code "null"} literal.
*/
static boolean isNullLiteral(String text) {
if (text == null) {
return true;
}
String trimmed = text.trim();
return trimmed.isEmpty() || "null".equals(trimmed);
}
public static JSONObject parseObject(String text) {
if (isNullLiteral(text)) {
return null;
}
try {
JsonNode node = MAPPER.readTree(text);
if (node == null || node.isNull()) {
return null;
}
if (!node.isObject()) {
throw new JSONException("can not cast to JSONObject.");
}
return new JSONObject((ObjectNode) node);
} catch (JSONException e) {
throw e;
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
public static JsonNode parse(String text) {
if (isNullLiteral(text)) {
return null;
}
try {
JsonNode node = MAPPER.readTree(text);
if (node == null || node.isNull()) {
return null;
}
return node;
} catch (JSONException e) {
throw e;
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
static JSONArray parseArray(String text) {
return JSONArray.parseArray(text);
}
public static String toJSONString(Object obj) {
return toJSONString(obj, false);
}
public static String toJSONString(Object obj, boolean pretty) {
if (obj == null) {
return "null";
}
try {
if (obj instanceof JSONObject) {
return pretty ? MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(((JSONObject) obj).unwrap())
: MAPPER.writeValueAsString(((JSONObject) obj).unwrap());
}
if (obj instanceof JSONArray) {
return pretty ? MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(((JSONArray) obj).unwrap())
: MAPPER.writeValueAsString(((JSONArray) obj).unwrap());
}
return pretty ? MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
: MAPPER.writeValueAsString(obj);
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
}