Skip to content

Commit c99ea79

Browse files
jotapegJuan Pablo González
andcommitted
Fix incompatible JSONTokenerWrapper for Android (#942)
* Revert "Fix StackOverflowException when instantiating JSONObject on Android (#938)" This reverts commit c0f92a2. * Add specific JSONTokenerWrapper for Android --------- Co-authored-by: Juan Pablo González <[email protected]> (cherry picked from commit 1f810bb)
1 parent feed9cf commit c99ea79

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

android/src/main/java/com/genexus/specific/android/AndroidJSONSerialization.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.genexus.common.interfaces.IExtensionJSONSerialization;
44
import com.genexus.json.JSONObjectWrapper;
5+
import org.json.JSONTokener;
56

67
import java.util.Iterator;
78
import java.util.LinkedHashMap;
@@ -18,4 +19,9 @@ public Iterator<Map.Entry<String, Object>> getJSONObjectIterator(JSONObjectWrapp
1819

1920
return map.entrySet().iterator();
2021
}
22+
23+
@Override
24+
public JSONTokener getJSONTokener(String s) {
25+
return new AndroidJSONTokenerWrapper(s);
26+
}
2127
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.genexus.specific.android;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.json.JSONTokener;
6+
7+
import com.genexus.json.JSONObjectWrapper;
8+
9+
public class AndroidJSONTokenerWrapper extends JSONTokener {
10+
public AndroidJSONTokenerWrapper(String string) {
11+
super(string);
12+
}
13+
14+
public Object nextValue() throws JSONException {
15+
char c = this.nextClean();
16+
this.back();
17+
if (c == '{') {
18+
try {
19+
return new JSONObjectWrapper((JSONObject) super.nextValue());
20+
} catch (StackOverflowError e) {
21+
throw new JSONException("JSON Array or Object depth too large to process.", e);
22+
}
23+
}
24+
else
25+
return super.nextValue();
26+
}
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.genexus.common.interfaces;
22

33
import com.genexus.json.JSONObjectWrapper;
4+
import org.json.JSONTokener;
45

56
import java.util.Iterator;
67
import java.util.Map;
78

89
public interface IExtensionJSONSerialization {
910
Iterator<Map.Entry<String, Object>> getJSONObjectIterator(JSONObjectWrapper obj);
11+
JSONTokener getJSONTokener(String s);
1012
}

common/src/main/java/com/genexus/json/JSONObjectWrapper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import java.util.*;
44

55
import java.util.Map.Entry;
6+
7+
import com.genexus.common.interfaces.SpecificImplementation;
68
import org.json.JSONException;
79
import org.json.JSONObject;
10+
811
public class JSONObjectWrapper extends JSONObject implements java.io.Serializable{
912
private Map<String, Object> map;
1013

@@ -15,7 +18,11 @@ public JSONObjectWrapper() {
1518
}
1619

1720
public JSONObjectWrapper(String string) {
18-
super(new JSONTokenerWrapper(string));
21+
super(SpecificImplementation.JsonSerialization != null
22+
? SpecificImplementation.JsonSerialization.getJSONTokener(string)
23+
: new JSONTokenerWrapper(string)
24+
);
25+
1926
if (map == null)
2027
map = new LinkedHashMap<String, Object>();
2128
}

common/src/main/java/com/genexus/json/JSONTokenerWrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.genexus.json;
22

3-
import org.json.JSONObject;
43
import org.json.JSONTokener;
54
import org.json.JSONException;
65

@@ -15,7 +14,7 @@ public Object nextValue() throws JSONException {
1514
this.back();
1615
if (c == '{') {
1716
try {
18-
return new JSONObjectWrapper((JSONObject) super.nextValue());
17+
return new JSONObjectWrapper(this);
1918
} catch (StackOverflowError e) {
2019
throw new JSONException("JSON Array or Object depth too large to process.", e);
2120
}

0 commit comments

Comments
 (0)