Merge pull request #7611 from keymanapp/fix/android/json-obj-parser

fix(android/engine): Handle parsing empty JSONArray as JSONObject
This commit is contained in:
Darcy Wong 2022-11-03 11:06:28 +07:00 committed by GitHub
commit ad58fa7f1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,6 +16,8 @@ import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
@ -43,6 +45,13 @@ public final class JSONParser {
}
jsonStr = strBuilder.toString();
if (type == JSONObject.class) {
String EMPTY_JSONARRAY_FORMATSTR = "^\\[\\](\\n)*$";
Pattern pattern = Pattern.compile(EMPTY_JSONARRAY_FORMATSTR);
Matcher matcher = pattern.matcher(jsonStr);
if (matcher.matches()) {
// Treat empty JSONArray as empty JSON Object. Issue #7564
jsonStr = "{}";
}
obj = (T) new JSONObject(jsonStr);
} else if (type == JSONArray.class) {
obj = (T) new JSONArray(jsonStr);