文章待補充,先寫寫以下知識點好了。
NULL值處理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject區別
JSON作為一個輕量級的文本數據交換格式常常用於web后台的數據封裝和傳輸。JSON及其工具包給開發帶來非常多的好處,提高了開發效率。然而,世間總是免不了存在一些坑,吾輩需笑看人生路,潛心研碼。NULL值作為一個特殊情況,在處理的時候尤其需要小心處理。不幸的是,隨着我們使用的工具類的作者的不同,對NULL的處理也有不同。此處,就扒一扒如題兩家JSONObject工具類對NULL的處理。
問題來了,這兩家對NULL是如何處理的,示例代碼如下:
String resp = "{" +
" \"id\": \"123456fdae23\",\n" +
" \"userName\": \"dodoro\",\n" +
" \"cnName\": \"清風明月多多愛工作\",\n" +
" \"anull\": null,\n" +
" \"anothernull\": null,\n" +
"}";
JSONObject jsonObject = JSONObject.fromObject(resp);
if(jsonObject.getString("anull").equals("null")){
System.out.println("null became a string");
}
com.alibaba.fastjson.JSONObject userInfo = com.alibaba.fastjson.JSONObject.parseObject(resp);
if(userInfo.getString("anull") == null){
System.out.println("alibaba NB, null is a null");
}
System.out.println(jsonObject);
運行的測試結果如下:
null became a string alibaba NB, null is a null
結果很明顯,net.sf.json.JSONObject將null轉換成了一個字符串"null",com.alibaba.fastjson則是將null轉換為了null,兩者是有不同的。
首先,net.sf.json.JSONObject 提供了一個將其他類型的數據轉換為轉換為JSONObject的方法fromObject()
public static JSONObject fromObject(Object object) {
return fromObject(object, new JsonConfig());
}
public static JSONObject fromObject(Object object, JsonConfig jsonConfig) {
if(object != null && !JSONUtils.isNull(object)) {
if(object instanceof Enum) { //是否是枚舉類型
throw new JSONException("\'object\' is an Enum. Use JSONArray instead");
} else if(!(object instanceof Annotation) && (object == null || !object.getClass().isAnnotation())) { //是否是注解類型
if(object instanceof JSONObject) { //是不是JSONObject
return _fromJSONObject((JSONObject)object, jsonConfig);
} else if(object instanceof DynaBean) {
return _fromDynaBean((DynaBean)object, jsonConfig);
} else if(object instanceof JSONTokener) {
return _fromJSONTokener((JSONTokener)object, jsonConfig);
} else if(object instanceof JSONString) { //是不是符合JSON格式的字符串
return _fromJSONString((JSONString)object, jsonConfig);
} else if(object instanceof Map) { //是不是Map
return _fromMap((Map)object, jsonConfig);
} else if(object instanceof String) { //是不是字符串
return _fromString((String)object, jsonConfig);
} else if(!JSONUtils.isNumber(object) && !JSONUtils.isBoolean(object) && !JSONUtils.isString(object)) {
if(JSONUtils.isArray(object)) { //是不是數組
throw new JSONException("\'object\' is an array. Use JSONArray instead");
} else {
return _fromBean(object, jsonConfig); //從對象轉換為JSONObject,需要滿足Bean的getter和setter
}
} else {
return new JSONObject();
}
} else {
throw new JSONException("\'object\' is an Annotation.");
}
} else {
return new JSONObject(true);
}
}
從其源代碼可以看出net.sf.json功能強大,接口簡潔,然而主要看的並不是這里,而是它的_fromJSONString((JSONString)object, jsonConfig); 方法。
private static JSONObject _fromString(String str, JsonConfig jsonConfig) {
if(str != null && !"null".equals(str)) {
return _fromJSONTokener(new JSONTokener(str), jsonConfig);
} else {
fireObjectStartEvent(jsonConfig);
fireObjectEndEvent(jsonConfig);
return new JSONObject(true);
}
}
private static JSONObject _fromJSONTokener(JSONTokener tokener, JsonConfig jsonConfig) {
try {
if(tokener.matches("null.*")) {
fireObjectStartEvent(jsonConfig);
fireObjectEndEvent(jsonConfig);
return new JSONObject(true);
} else if(tokener.nextClean() != 123) {
throw tokener.syntaxError("A JSONObject text must begin with \'{\'");
} else {
fireObjectStartEvent(jsonConfig);
Collection exclusions = jsonConfig.getMergedExcludes();
PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
JSONObject jsonObject = new JSONObject();
while(true) {
char jsone = tokener.nextClean();
switch(jsone) {
case '\u0000':
throw tokener.syntaxError("A JSONObject text must end with \'}\'");
case '}':
fireObjectEndEvent(jsonConfig);
return jsonObject;
default:
tokener.back();
String key = tokener.nextValue(jsonConfig).toString(); //此處取得一個token
jsone = tokener.nextClean();
if(jsone == 61) {
if(tokener.next() != 62) {
tokener.back();
}
} else if(jsone != 58) {
throw tokener.syntaxError("Expected a \':\' after a key");
}
char peek = tokener.peek();
boolean quoted = peek == 34 || peek == 39;
Object v = tokener.nextValue(jsonConfig); //獲取當前key的value
if(!quoted && JSONUtils.isFunctionHeader(v)) {
String params = JSONUtils.getFunctionParams((String)v); //轉換為String裊
//省略.....
}
nextValue方法是JSONTokener類的方法,代碼如下:
public Object nextValue(JsonConfig jsonConfig) {
char c = this.nextClean();
switch(c) {
case '\"':
case '\'':
return this.nextString(c);
case '[':
this.back();
return JSONArray.fromObject(this, jsonConfig);
case '{':
this.back();
return JSONObject.fromObject(this, jsonConfig);
default:
StringBuffer sb = new StringBuffer();
char b;
for(b = c; c >= 32 && ",:]}/\\\"[{;=#".indexOf(c) < 0; c = this.next()) {
sb.append(c);
}
this.back();
String s = sb.toString().trim();
if(s.equals("")) {
throw this.syntaxError("Missing value.");
} else if(s.equalsIgnoreCase("true")) {
return Boolean.TRUE;
} else if(s.equalsIgnoreCase("false")) {
return Boolean.FALSE;
} else if(!s.equals("null") && (!jsonConfig.isJavascriptCompliant() || !s.equals("undefined"))) {
if((b < 48 || b > 57) && b != 46 && b != 45 && b != 43) {
if(!JSONUtils.isFunctionHeader(s) && !JSONUtils.isFunction(s)) {
switch(this.peek()) {
case ',':
case '[':
case ']':
case '{':
case '}':
throw new JSONException("Unquotted string \'" + s + "\'");
default:
return s;
}
} else {
return s;
}
} else {
if(b == 48) {
if(s.length() > 2 && (s.charAt(1) == 120 || s.charAt(1) == 88)) {
try {
return new Integer(Integer.parseInt(s.substring(2), 16));
} catch (Exception var13) {
;
}
} else {
try {
return new Integer(Integer.parseInt(s, 8));
} catch (Exception var12) {
;
}
}
}
try {
return new Integer(s);
} catch (Exception var11) {
try {
return new Long(s);
} catch (Exception var10) {
try {
return new Double(s);
} catch (Exception var9) {
return s;
}
}
}
}
} else {
return JSONNull.getInstance(); //返回一個JSONNUll對象
}
}
}
public final class JSONNull implements JSON {
public String toString() {
return "null"; //返回一個“null”字符串
}
public String toString(int indentFactor) {
return this.toString();
}
}
至此可以看到net.sf.json將NULL轉換為一個“null” 字符串處理。如果直接對存在null值得JSON對象使用getString進行操作,那么就容易出現使用getString("anull") == null的操作,而fastjson則是將null直接轉換為一個null對象,所以可以直接使用getString("anull") == null進行比較。
萬幸的是,如果我們通過JSON的方法將JSON對象轉換為封裝相同字段的JAVA對象,無論是哪種JSON工具包,最后都會把NULL轉換為null。
所以建議后台獲取的JSON對象,如果想要使用,最好先轉換為JAVABean來避免不同工具包之間的差異所帶來的異常故障。
