Skip to content

Commit 5227cca

Browse files
halibobo1205claude
andcommitted
refactor(json): remove 10 unused Fastjson-compat methods from JSON wrappers
Remove methods that were implemented for Fastjson API compatibility but have zero call sites in the java-tron codebase: JSONObject: getDouble(), toMap(), parseArray() (proxy) JSONArray: isEmpty(), getJSONArray(int), getBoolean(int), toJavaList(), add(JSONArray), add(String), add(Object) Also fix EventParserJsonTest to call JSONArray.parseArray() directly and clean up 22 corresponding test methods in JsonCompatibilityFuzzTest. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4ef0864 commit 5227cca

4 files changed

Lines changed: 3 additions & 446 deletions

File tree

common/src/main/java/org/tron/json/JSONArray.java

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public int size() {
4949
return node.size();
5050
}
5151

52-
public boolean isEmpty() {
53-
return node.isEmpty();
54-
}
55-
5652
private void rangeCheck(int index) {
5753
if (index < 0 || index >= node.size()) {
5854
throw new IndexOutOfBoundsException(
@@ -81,22 +77,6 @@ public JSONObject getJSONObject(int index) {
8177
throw new JSONException("Element at index " + index + " is not an object");
8278
}
8379

84-
public JSONArray getJSONArray(int index) {
85-
rangeCheck(index);
86-
JsonNode child = node.get(index);
87-
if (child.isNull()) {
88-
return null;
89-
}
90-
if (child.isArray()) {
91-
return new JSONArray((ArrayNode) child);
92-
}
93-
// Fastjson auto-parses stringified JSON arrays
94-
if (child.isTextual()) {
95-
return JSON.parseArray(child.asText());
96-
}
97-
throw new JSONException("Element at index " + index + " is not an array");
98-
}
99-
10080
public String getString(int index) {
10181
rangeCheck(index);
10282
JsonNode child = node.get(index);
@@ -113,64 +93,6 @@ public String getString(int index) {
11393
return child.asText(null);
11494
}
11595

116-
public Boolean getBoolean(int index) {
117-
rangeCheck(index);
118-
JsonNode child = node.get(index);
119-
if (child.isNull()) {
120-
return null;
121-
}
122-
if (child.isBoolean()) {
123-
return child.booleanValue();
124-
}
125-
if (child.isNumber()) {
126-
return child.longValue() != 0;
127-
}
128-
if (child.isTextual()) {
129-
String text = child.asText();
130-
if ("true".equalsIgnoreCase(text) || "1".equals(text)) {
131-
return Boolean.TRUE;
132-
}
133-
if ("false".equalsIgnoreCase(text) || "0".equals(text)) {
134-
return Boolean.FALSE;
135-
}
136-
throw new JSONException("Cannot cast '" + text + "' to Boolean");
137-
}
138-
throw new JSONException("Cannot cast " + child.getNodeType() + " to Boolean");
139-
}
140-
141-
public <T> List<T> toJavaList(Class<T> clazz) {
142-
try {
143-
List<T> result = new ArrayList<>();
144-
for (JsonNode element : node) {
145-
if (element == null || element.isNull()) {
146-
result.add(null);
147-
continue;
148-
}
149-
if (clazz == JSONObject.class) {
150-
if (!element.isObject()) {
151-
throw new JSONException(
152-
"Element is not an object, cannot convert to JSONObject");
153-
}
154-
result.add(clazz.cast(new JSONObject((ObjectNode) element)));
155-
} else if (clazz == JSONArray.class) {
156-
if (!element.isArray()) {
157-
throw new JSONException(
158-
"Element is not an array, cannot convert to JSONArray");
159-
}
160-
result.add(clazz.cast(new JSONArray((ArrayNode) element)));
161-
} else {
162-
result.add(JSON.MAPPER.treeToValue(element, clazz));
163-
}
164-
}
165-
return result;
166-
} catch (JSONException e) {
167-
throw e;
168-
} catch (Exception e) {
169-
throw new JSONException(
170-
"Failed to convert JSONArray to List<" + clazz.getSimpleName() + ">", e);
171-
}
172-
}
173-
17496
// -------------------------------------------------------------------------
17597
// Mutation helpers
17698
// -------------------------------------------------------------------------
@@ -180,35 +102,6 @@ public JSONArray add(JSONObject value) {
180102
return this;
181103
}
182104

183-
public JSONArray add(JSONArray value) {
184-
node.add(value == null ? node.nullNode() : value.unwrap());
185-
return this;
186-
}
187-
188-
public JSONArray add(String value) {
189-
node.add(value);
190-
return this;
191-
}
192-
193-
public JSONArray add(Object value) {
194-
if (value == null) {
195-
node.add(node.nullNode());
196-
return this;
197-
}
198-
if (value instanceof JSONObject) {
199-
return add((JSONObject) value);
200-
}
201-
if (value instanceof JSONArray) {
202-
return add((JSONArray) value);
203-
}
204-
if (value instanceof JsonNode) {
205-
node.add((JsonNode) value);
206-
return this;
207-
}
208-
node.add(JSON.MAPPER.valueToTree(value));
209-
return this;
210-
}
211-
212105
/** Returns the underlying Jackson {@link ArrayNode}. */
213106
@JsonValue
214107
public ArrayNode unwrap() {

common/src/main/java/org/tron/json/JSONObject.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
import com.fasterxml.jackson.databind.node.ArrayNode;
66
import com.fasterxml.jackson.databind.node.ObjectNode;
77
import java.math.BigDecimal;
8-
import java.util.Iterator;
9-
import java.util.LinkedHashMap;
108
import java.util.LinkedHashSet;
119
import java.util.List;
12-
import java.util.Map;
1310
import java.util.Set;
1411

1512
/**
@@ -37,11 +34,6 @@ public static JSONObject parseObject(String text) {
3734
return JSON.parseObject(text);
3835
}
3936

40-
/** {@code JSONObject.parseArray(String)} */
41-
public static JSONArray parseArray(String text) {
42-
return JSONArray.parseArray(text);
43-
}
44-
4537
// -------------------------------------------------------------------------
4638
// Field access
4739
// -------------------------------------------------------------------------
@@ -194,31 +186,6 @@ public int getIntValue(String key) {
194186
throw new JSONException("Cannot cast " + child.getNodeType() + " to int");
195187
}
196188

197-
public Double getDouble(String key) {
198-
JsonNode child = node.get(key);
199-
if (child == null || child.isNull()) {
200-
return null;
201-
}
202-
if (child.isNumber()) {
203-
return child.doubleValue();
204-
}
205-
if (child.isBoolean()) {
206-
return child.booleanValue() ? 1.0 : 0.0;
207-
}
208-
if (child.isTextual()) {
209-
String text = child.asText().trim();
210-
if (text.isEmpty() || "null".equalsIgnoreCase(text)) {
211-
return null;
212-
}
213-
try {
214-
return Double.parseDouble(text);
215-
} catch (NumberFormatException e) {
216-
throw new JSONException("Cannot cast '" + text + "' to Double", e);
217-
}
218-
}
219-
throw new JSONException("Cannot cast " + child.getNodeType() + " to Double");
220-
}
221-
222189
public BigDecimal getBigDecimal(String key) {
223190
JsonNode child = node.get(key);
224191
if (child == null || child.isNull()) {
@@ -472,24 +439,6 @@ public Set<String> keySet() {
472439
return keys;
473440
}
474441

475-
// -------------------------------------------------------------------------
476-
// Conversion
477-
// -------------------------------------------------------------------------
478-
479-
public Map<String, Object> toMap() {
480-
try {
481-
Map<String, Object> map = new LinkedHashMap<>();
482-
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
483-
while (fields.hasNext()) {
484-
Map.Entry<String, JsonNode> entry = fields.next();
485-
map.put(entry.getKey(), JSON.MAPPER.treeToValue(entry.getValue(), Object.class));
486-
}
487-
return map;
488-
} catch (Exception e) {
489-
throw new JSONException("Failed to convert JSONObject to Map", e);
490-
}
491-
}
492-
493442
/** Returns the underlying Jackson {@link ObjectNode}. */
494443
@JsonValue
495444
public ObjectNode unwrap() {

framework/src/test/java/org/tron/common/logsfilter/EventParserJsonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public synchronized void testEventParser() {
6060
topicList.add(ByteArray
6161
.fromHexString("0xb7685f178b1c93df3422f7bfcb61ae2c6f66d0947bb9eb293259c231b986b81b"));
6262

63-
JSONArray entryArr = JSONObject.parseArray(abiStr);
63+
JSONArray entryArr = JSONArray.parseArray(abiStr);
6464
JSONObject entry = new JSONObject();
6565

6666
for (int i = 0; i < entryArr.size(); i++) {

0 commit comments

Comments
 (0)