Skip to content

Commit 9faa3ca

Browse files
authored
Json Null Serialization property was not working in all cases. (#819)
Issue: 97823
1 parent ba1d738 commit 9faa3ca

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,18 +2755,21 @@ public static Object convertObjectTo(Object obj, Class toClass, boolean fail) th
27552755
{
27562756
try
27572757
{
2758-
2759-
if (objStr.isEmpty() || objStr.equals("null"))
2760-
objStr ="0";
2761-
else
2758+
if (objStr.equals("null"))
2759+
return null;
2760+
else
27622761
{
2763-
int i = objStr.indexOf(".") ;
2764-
if (i >= 0)
2762+
if (objStr.isEmpty() || objStr.equals("null"))
2763+
objStr = "0";
2764+
else
27652765
{
2766-
if (objStr.indexOf('E') == -1 && objStr.indexOf('e') == -1)
2767-
objStr = objStr.substring(0, i);
2768-
else
2769-
objStr = CommonUtil.strUnexponentString(objStr);
2766+
int i = objStr.indexOf(".");
2767+
if (i >= 0) {
2768+
if (objStr.indexOf('E') == -1 && objStr.indexOf('e') == -1)
2769+
objStr = objStr.substring(0, i);
2770+
else
2771+
objStr = CommonUtil.strUnexponentString(objStr);
2772+
}
27702773
}
27712774
}
27722775
}
@@ -2795,7 +2798,7 @@ else if (className.equals("long") || className.equals("java.lang.Long") || class
27952798
}
27962799
else if (className.equals("string") || className.indexOf("java.lang.String") != -1)
27972800
{
2798-
return objStr.equals("null") ? "" : objStr;
2801+
return objStr.equals("null") ? null : objStr;
27992802
}
28002803
else if (className.equals("double") || className.equals("java.lang.Double") || className.equals("[D"))
28012804
{
@@ -2831,7 +2834,7 @@ else if (className.equals("boolean") || className.equals("java.lang.Boolean") ||
28312834
{
28322835
try
28332836
{
2834-
return objStr.equals("null") ? false : Boolean.valueOf(objStr);
2837+
return objStr.equals("null") ? null : Boolean.valueOf(objStr);
28352838
}
28362839
catch(Exception e)
28372840
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ else if (!currObj.equals(""))
402402
}
403403
}
404404
if (setClass != null)
405-
setMethod.invoke(this, new Object[] { convertValueToParmType(currObj, setClass) });
405+
{
406+
Object parmType = convertValueToParmType(currObj, setClass);
407+
if (parmType != null)
408+
setMethod.invoke(this, new Object[] { parmType });
409+
}
406410
}
407411
}
408412
catch (java.lang.ClassCastException ex)
@@ -419,6 +423,8 @@ private Object convertValueToParmType(Object value, Class parmClass) throws Exce
419423
{
420424
if (parmClass.getName().equals("java.util.Date"))
421425
{
426+
if (value.toString().equals("null"))
427+
return null;
422428
return localUtil.ctot(value.toString(), 0);
423429
}
424430
return CommonUtil.convertObjectTo(value, parmClass);

common/src/main/java/json/org/json/JSONObject.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -901,16 +901,11 @@ public JSONObject put(String key, Object value) throws JSONException {
901901
if (key == null) {
902902
throw new JSONException("Null key.");
903903
}
904-
if (value != null) {
905-
testValidity(value);
906-
if (!has(key))
907-
{
908-
this.nameIndexList.add(key);
909-
}
910-
this.myHashMap.put(key, value);
911-
} else {
912-
remove(key);
913-
}
904+
testValidity(value);
905+
if (!has(key)) {
906+
this.nameIndexList.add(key);
907+
}
908+
this.myHashMap.put(key, value);
914909
return this;
915910
}
916911

java/src/test/java/com/genexus/TestCommonUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void testConvertObjectTo() {
234234
try{
235235
Class integerClass = Class.forName("java.lang.Integer");
236236
result = CommonUtil.convertObjectTo(obj, integerClass, true);
237-
Assert.assertEquals(Integer.valueOf("0"), result);
237+
Assert.assertEquals(null, result);
238238
} catch (Exception e){
239239
Assert.fail("Test failed " + e);
240240
}
@@ -254,7 +254,7 @@ public void testConvertObjectTo() {
254254
try{
255255
Class stringClass = Class.forName("java.lang.String");
256256
result = CommonUtil.convertObjectTo(obj, stringClass, true);
257-
Assert.assertEquals("", result);
257+
Assert.assertEquals(null, result);
258258
} catch (Exception e){
259259
Assert.fail("Test failed " + e);
260260
}
@@ -294,7 +294,7 @@ public void testConvertObjectTo() {
294294
try{
295295
Class booleanClass = Class.forName("java.lang.Boolean");
296296
result = CommonUtil.convertObjectTo(obj, booleanClass, true);
297-
Assert.assertEquals(false, result);
297+
Assert.assertEquals(null, result);
298298
} catch (Exception e){
299299
Assert.fail("Test failed " + e);
300300
}

0 commit comments

Comments
 (0)