Skip to content

Commit bce499d

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:ReplaceJsonAPI' into beta
1 parent 92069fa commit bce499d

File tree

12 files changed

+136
-34
lines changed

12 files changed

+136
-34
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.genexus.specific.android;
2+
3+
import com.genexus.common.interfaces.IExtensionJSONSerialization;
4+
import com.genexus.json.JSONObjectWrapper;
5+
6+
import java.util.Iterator;
7+
import java.util.LinkedHashMap;
8+
import java.util.Map;
9+
10+
public class AndroidJSONSerialization implements IExtensionJSONSerialization {
11+
@Override
12+
public Iterator<Map.Entry<String, Object>> getJSONObjectIterator(JSONObjectWrapper obj) {
13+
Map<String, Object> map = new LinkedHashMap<>();
14+
for (Iterator<String> it = obj.keys(); it.hasNext(); ) {
15+
String k = it.next();
16+
map.put(k, null); // value is not used for now, so we just set it as null
17+
}
18+
19+
return map.entrySet().iterator();
20+
}
21+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static void init()
3232
// connect GXSilentTrn
3333
SpecificImplementation.GXSilentTrnSdt = new GXSilentTrnSdt();
3434
SpecificImplementation.SdtMessages_Message = new SdtMessages_Message();
35+
SpecificImplementation.JsonSerialization = new AndroidJSONSerialization();
3536

3637
SpecificImplementation.KeepDecimals = true;
3738
SpecificImplementation.MillisecondMask = "SSS";

common/src/main/java/com/genexus/GXSimpleCollection.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import com.genexus.internet.IGxJSONAble;
1717
import com.genexus.internet.IGxJSONSerializable;
1818
import com.genexus.internet.StringCollection;
19+
import com.genexus.json.JSONArrayWrapper;
1920
import com.genexus.util.GXMap;
2021
import com.genexus.util.Quicksort;
2122
import com.genexus.xml.XMLReader;
22-
2323
import com.genexus.xml.XMLWriter;
2424

2525
import org.json.JSONArray;
@@ -33,7 +33,7 @@ public class GXSimpleCollection<T> extends GXBaseList<T> {
3333
protected String xmlElementsName;
3434
protected String containedXmlNamespace = "";
3535
protected int remoteHandle = -1;
36-
protected JSONArray jsonArr = new JSONArray();
36+
protected JSONArrayWrapper jsonArr = new JSONArrayWrapper();
3737

3838
public GXSimpleCollection()
3939
{
@@ -979,7 +979,7 @@ public void tojson()
979979
}
980980
public void tojson(boolean includeState)
981981
{
982-
jsonArr = new JSONArray();
982+
jsonArr = new JSONArrayWrapper();
983983
int size = size();
984984
for (int i = 0; i < size; i++)
985985
{
@@ -1118,7 +1118,7 @@ public boolean fromJSonString(String s, GXBaseCollection<SdtMessages_Message> me
11181118
{
11191119
try
11201120
{
1121-
jsonArr = new JSONArray(s);
1121+
jsonArr = new JSONArrayWrapper(s);
11221122
FromJSONObject(jsonArr);
11231123
return true;
11241124
}
@@ -1144,7 +1144,7 @@ public void fromStringCollectionJsonString(String s)
11441144
this.clear();
11451145
try
11461146
{
1147-
jsonArr = new JSONArray(s);
1147+
jsonArr = new JSONArrayWrapper(s);
11481148
for (int i = 0; i < jsonArr.length(); i++)
11491149
{
11501150
JSONArray jsonObj = jsonArr.getJSONArray(i);
@@ -1224,7 +1224,7 @@ public void fromStringCollectionClientJsonString(String s)
12241224
{
12251225
this.clear();
12261226
try {
1227-
jsonArr = new JSONArray(s);
1227+
jsonArr = new JSONArrayWrapper(s);
12281228
} catch (JSONException e) {
12291229
// TODO Auto-generated catch block
12301230
e.printStackTrace();
@@ -1291,7 +1291,7 @@ private boolean processArray(GxUnknownObjectCollection oldParent, GxUnknownObjec
12911291

12921292
public JSONArray getRawJSONArray()
12931293
{
1294-
return jsonArr;
1294+
return (JSONArray)jsonArr;
12951295
}
12961296

12971297
// toString of GxUnknownObjectCollection as client proc expect, table info + rows.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.genexus.common.interfaces;
2+
3+
import com.genexus.json.JSONObjectWrapper;
4+
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
8+
public interface IExtensionJSONSerialization {
9+
Iterator<Map.Entry<String, Object>> getJSONObjectIterator(JSONObjectWrapper obj);
10+
}

common/src/main/java/com/genexus/common/interfaces/SpecificImplementation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class SpecificImplementation {
2626
public static IExtensionSdtMessages_Message SdtMessages_Message;
2727
public static ICryptoAlhorithms Algorithms;
2828
public static IExtensionModelContext ModelContext;
29+
public static IExtensionJSONSerialization JsonSerialization;
2930
public static boolean KeepDecimals;
3031
public static String MillisecondMask;
3132
public static boolean SupportPending;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.genexus.json;
2+
3+
import org.json.JSONArray;
4+
5+
import java.util.LinkedHashMap;
6+
7+
public class JSONArrayWrapper extends JSONArray implements java.io.Serializable{
8+
9+
public JSONArrayWrapper() {
10+
super();
11+
}
12+
13+
public JSONArrayWrapper(String string) {
14+
super(string);
15+
}
16+
17+
public JSONArrayWrapper(JSONArray array) {
18+
super(array);
19+
}
20+
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Map.Entry;
66
import org.json.JSONException;
77
import org.json.JSONObject;
8-
public class JSONObjectWrapper extends JSONObject{
8+
public class JSONObjectWrapper extends JSONObject implements java.io.Serializable{
99
private Map<String, Object> map;
1010

1111
public JSONObjectWrapper() {
@@ -15,25 +15,27 @@ public JSONObjectWrapper() {
1515
}
1616

1717
public JSONObjectWrapper(String string) {
18-
super(string);
18+
super(new JSONTokenerWrapper(string));
1919
if (map == null)
2020
map = new LinkedHashMap<String, Object>();
2121
}
2222

23-
public JSONObjectWrapper(Map<?,?> m) {
24-
super(m);
23+
public JSONObjectWrapper(JSONTokenerWrapper token) {
24+
super(token);
2525
if (map == null)
2626
map = new LinkedHashMap<String, Object>();
27-
else {
28-
map = new LinkedHashMap<String, Object>(m.size());
29-
for (final Entry<?, ?> e : m.entrySet()) {
30-
if (e.getKey() == null) {
31-
throw new NullPointerException("Null key.");
32-
}
33-
final Object value = e.getValue();
34-
if (value != null)
35-
this.map.put(String.valueOf(e.getKey()), value);
27+
}
28+
29+
public JSONObjectWrapper(Map<?,?> m) {
30+
super(m);
31+
map = new LinkedHashMap<String, Object>(m.size());
32+
for (final Entry<?, ?> e : m.entrySet()) {
33+
if (e.getKey() == null) {
34+
throw new NullPointerException("Null key.");
3635
}
36+
final Object value = e.getValue();
37+
if (value != null)
38+
this.map.put(String.valueOf(e.getKey()), value);
3739
}
3840
}
3941

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

common/src/main/java/com/genexus/util/GXProperties.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,8 @@ public boolean fromJSonString(String s, GXBaseCollection < SdtMessages_Message >
127127
if (!s.isEmpty()) {
128128
try {
129129
JSONObjectWrapper jObj = new JSONObjectWrapper(s);
130-
Iterator < String > keys = jObj.keys();
131-
while (keys.hasNext()) {
132-
String key = keys.next();
130+
for (Map.Entry<String, Object> e : jObj.entrySet()) {
131+
String key = e.getKey();
133132
this.put(key, jObj.get(key).toString());
134133
}
135134
return true;

common/src/main/java/com/genexus/xml/GXXMLSerializable.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import com.genexus.common.classes.AbstractGXFile;
44
import com.genexus.ModelContext;
55
import com.genexus.common.interfaces.SpecificImplementation;
6+
import com.genexus.json.JSONArrayWrapper;
7+
import com.genexus.json.JSONObjectWrapper;
68
import com.genexus.util.GXProperties;
79

810
import org.json.JSONArray;
911
import org.json.JSONException;
1012
import org.json.JSONObject;
11-
import com.genexus.json.JSONObjectWrapper;
1213

1314
import java.io.Serializable;
1415
import java.util.Iterator;
1516
import java.lang.reflect.Method;
17+
import java.util.Map;
1618
import java.util.concurrent.ConcurrentHashMap;
1719

1820
import com.genexus.internet.IGxJSONAble;
@@ -39,7 +41,7 @@ public GXXMLSerializable(int remoteHandle, ModelContext context, String type)
3941
private static final String GET_METHOD_NAME = "getgxTv_";
4042
private static final String SET_METHOD_NAME = "setgxTv_";
4143
private JSONObjectWrapper jsonObj = new JSONObjectWrapper();
42-
private JSONArray jsonArr = new JSONArray();
44+
private JSONArrayWrapper jsonArr = new JSONArrayWrapper();
4345
protected boolean isArrayObject = false;
4446
protected String arrayItemName;
4547
protected String type;
@@ -239,7 +241,11 @@ public void AddObjectProperty(String name, Object prop, boolean includeState, bo
239241
}
240242
else if (isArrayObject)
241243
{
242-
jsonArr = (JSONArray)((IGxJSONAble)prop).GetJSONObject(includeState);
244+
Object obj = ((IGxJSONAble)prop).GetJSONObject(includeState);
245+
if (obj instanceof JSONArrayWrapper)
246+
jsonArr = (JSONArrayWrapper)obj;
247+
else
248+
jsonArr = new JSONArrayWrapper((JSONArray)obj);
243249
}
244250
else
245251
{
@@ -291,14 +297,17 @@ public Object GetJSONObject(boolean includeState, boolean includeNoInitialized)
291297
}
292298

293299
/*Recorre el iterador y pone los _N del campo al final asi no se pierden los valores null cuando se asigna el valor al campo*/
294-
private Iterator getFromJSONObjectOrderIterator(Iterator it)
300+
private Iterator getFromJSONObjectOrderIterator(Iterator<Map.Entry<String, Object>> it)
295301
{
296302
java.util.Vector<String> v = new java.util.Vector<String>();
297303
java.util.Vector<String> vAtEnd = new java.util.Vector<String>();
298304
String name;
299305
while(it.hasNext())
300306
{
301-
name = (String)it.next();
307+
/* Even though the map entry value is never used and the method could receive just the map's keys list,
308+
its entries set provides the keys in a specific order required by this method */
309+
Map.Entry<String, Object> entry = it.next();
310+
name = entry.getKey();
302311
String map = getJsonMap(name);
303312
String className = CommonUtil.classNameNoPackage(this.getClass());
304313
Method getMethod = getMethod(GET_METHOD_NAME + className + "_" + (map != null? map : name));
@@ -356,10 +365,11 @@ public void FromJSONObject(Object obj)
356365
{
357366
if (obj instanceof JSONObject)
358367
obj = new JSONObjectWrapper((JSONObject)obj);
359-
Iterator it = getFromJSONObjectOrderIterator(((JSONObjectWrapper)obj).keys());
360-
while(it.hasNext())
368+
Iterator objIterator = getJSONObjectIterator((JSONObjectWrapper) obj);
369+
Iterator modifiedObjIterator = getFromJSONObjectOrderIterator(objIterator);
370+
while(modifiedObjIterator.hasNext())
361371
{
362-
name = (String)it.next();
372+
name = (String)modifiedObjIterator.next();
363373
map = getJsonMap(name);
364374
setMethod = getMethod(SET_METHOD_NAME + className + "_" + (map != null? map : name));
365375
getMethod = getMethod(GET_METHOD_NAME + className + "_" + (map != null? map : name));
@@ -426,6 +436,14 @@ else if (!currObj.equals(""))
426436
}
427437
}
428438
}
439+
440+
private Iterator<Map.Entry<String, Object>> getJSONObjectIterator(JSONObjectWrapper obj) {
441+
if (SpecificImplementation.JsonSerialization == null)
442+
return obj.entrySet().iterator();
443+
444+
return SpecificImplementation.JsonSerialization.getJSONObjectIterator(obj);
445+
}
446+
429447
private Object convertValueToParmType(Object value, Class parmClass) throws Exception
430448
{
431449
if (parmClass.getName().equals("java.util.Date"))
@@ -469,7 +487,7 @@ private void collectionFromJSONArray(JSONArray jsonArray, GXSimpleCollection gxC
469487
else
470488
{
471489
if (gxColl.getElementsType() == java.math.BigDecimal.class)
472-
gxColl.addBase(DecimalUtil.stringToDec(jsonArray.getString(i)));
490+
gxColl.addBase(DecimalUtil.stringToDec(jsonArray.get(i).toString()));
473491
else
474492
gxColl.addBase(currObj);
475493
}

0 commit comments

Comments
 (0)