Understanding getInteger vs getIntValue in FastJSON and Fixing the isRight() Validation Bug
This article explains the difference between FastJSON's getInteger and getIntValue methods, analyzes a common validation bug in a custom isRight() implementation, and provides corrected code examples and optional global switches for robust response handling in Java backend projects.
FastJSON provides two similar methods, getInteger() and getIntValue(), to retrieve a value associated with a key from a JSONObject. The key distinction is that getInteger() returns an Integer object, while getIntValue() returns a primitive int.
Bug Demonstration
In a project framework, the interface com.funtester.base.interfaces.IBase#isRight is used to verify whether a response conforms to a standard structure. An incorrect implementation used response.getIntValue("code") == 0, which can produce false positives when the outer JSON structure changes and the code field is missing.
@Override
public boolean isRight(JSONObject response) {
try {
return response.getIntValue("code") == 0;
} catch (Exception e) {
return false;
}
}Bug Analysis
Typical API responses contain a top‑level object with code, msg, and data. The method extracts the code value and compares it with 0. If the response format changes (e.g., the code field disappears), the method still returns true, which is incorrect.
Corrected Implementation
The fix is to use getInteger() so that a missing key yields null instead of a default primitive value, and to add an extra check for the HTTP status when needed.
@Override
public boolean isRight(JSONObject response) {
try {
return response.getInteger("code") == 0;
} catch (Exception e) {
return false;
}
}When the response may also contain an HTTP status code, the method can be extended as follows:
@Override
public boolean isRight(JSONObject response) {
try {
return response.getInteger("FunTester") == HttpStatus.SC_OK &&
response.getInteger("code") == 0;
} catch (Exception e) {
return false;
}
}For performance testing, a global switch Common.VERIFY can be added to skip verification when not needed:
@Override
public boolean isRight(JSONObject response) {
try {
return Common.VERIFY &&
response.getInteger("FunTester") == HttpStatus.SC_OK &&
response.getInteger("code") == 0;
} catch (Exception e) {
return false;
}
}Practical Demonstrations
Demo 1 – Normal Usage
public static void main(String[] args) throws IOException {
JSONObject json = getJson("code=0");
output(json.getInteger("code") == 0);
output(json.getIntValue("code") == 0);
}Both calls output true because the key exists.
Demo 2 – Bug Scenario
public static void main(String[] args) throws IOException {
JSONObject json = getJson("code=0");
output(json.getIntValue("co") == 0);
output(null == json.getInteger("co"));
}When the key is misspelled, getIntValue("co") returns 0, leading to a misleading true, while getInteger("co") correctly returns null.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
