Skip to content

Commit 9aa6d30

Browse files
committed
fix for PT.toJSON for primitive arrays
1 parent fdedad7 commit 9aa6d30

File tree

9 files changed

+134
-103
lines changed

9 files changed

+134
-103
lines changed
-501 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211219114745
1+
20211230184313
-501 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211219114745
1+
20211230184313
-501 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/javajs/util/PT.java

Lines changed: 103 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -786,110 +786,115 @@ public static boolean isNonStringPrimitive(Object info) {
786786
// because in JavaScript those would be false for unwrapped primitives
787787
// coming from equivalent of Array.get()
788788
// Strings will need their own escaped processing
789-
790-
return info instanceof Number || info instanceof Boolean;
791-
}
792-
793-
// private static Object arrayGet(Object info, int i) {
794-
// /**
795-
// *
796-
// * Note that info will be a primitive in JavaScript
797-
// * but a wrapped primitive in Java.
798-
// *
799-
// * @j2sNative
800-
// *
801-
// * return info[i];
802-
// */
803-
// {
804-
// return Array.get(info, i);
805-
// }
806-
// }
807-
//
808-
@SuppressWarnings("unchecked")
809-
public static String toJSON(String infoType, Object info) {
810-
if (info == null)
811-
return packageJSON(infoType, null);
812-
if (isNonStringPrimitive(info))
813-
return packageJSON(infoType, info.toString());
814-
String s = null;
815-
SB sb = null;
816-
while (true) {
817-
if (info instanceof String) {
818-
s = (String) info;
789+
/**
790+
* @j2sNative
791+
*
792+
* if(typeof info == "number" || typeof info == "boolean") {
793+
* return true;
794+
* }
795+
*
796+
*
797+
*/
798+
{}
799+
return info instanceof Number || info instanceof Boolean;
800+
}
801+
802+
@SuppressWarnings({ "unused", "unchecked", "null" })
803+
public static String toJSON(String infoType, Object info) {
804+
if (info == null)
805+
return packageJSON(infoType, null);
806+
if (isNonStringPrimitive(info))
807+
return packageJSON(infoType, info.toString());
808+
String s = null;
809+
SB sb = null;
810+
while (true) {
811+
if (info instanceof String) {
812+
s = (String) info;
819813
// /**
820814
// * @j2sNative
821815
// *
822816
// * if (typeof s == "undefined") s = "null"
823817
// *
824818
// */
825-
826-
if (s.indexOf("{\"") != 0) {
827-
//don't doubly fix JSON strings when retrieving status
828-
// what about \1 \2 \3 etc.?
829-
s = esc(s);
830-
}
831-
break;
832-
}
833-
if (info instanceof JSONEncodable) {
834-
// includes javajs.util.BS, org.jmol.script.SV
835-
if ((s = ((JSONEncodable) info).toJSON()) == null)
836-
s = "null"; // perhaps a list has a null value (group3List, for example)
837-
break;
838-
}
839-
sb = new SB();
840-
if (info instanceof Map) {
841-
sb.append("{ ");
842-
String sep = "";
843-
for (String key : ((Map<String, ?>) info).keySet()) {
844-
sb.append(sep).append(
845-
packageJSON(key, toJSON(null, ((Map<?, ?>) info).get(key))));
846-
sep = ",";
847-
}
848-
sb.append(" }");
849-
break;
850-
}
851-
if (info instanceof Lst) {
852-
sb.append("[ ");
853-
int n = ((Lst<?>) info).size();
854-
for (int i = 0; i < n; i++) {
855-
if (i > 0)
856-
sb.appendC(',');
857-
sb.append(toJSON(null, ((Lst<?>) info).get(i)));
858-
}
859-
sb.append(" ]");
860-
break;
861-
}
862-
if (info instanceof M34) {
863-
// M4 extends M3
864-
int len = (info instanceof M4 ? 4 : 3);
865-
float[] x = new float[len];
866-
M34 m = (M34) info;
867-
sb.appendC('[');
868-
for (int i = 0; i < len; i++) {
869-
if (i > 0)
870-
sb.appendC(',');
871-
m.getRow(i, x);
872-
sb.append(toJSON(null, x));
873-
}
874-
sb.appendC(']');
875-
break;
876-
}
877-
s = nonArrayString(info);
878-
if (s == null) {
879-
sb.append("[");
880-
int n = AU.getLength(info);
881-
for (int i = 0; i < n; i++) {
882-
if (i > 0)
883-
sb.appendC(',');
884-
sb.append(toJSON(null, Array.get(info, i)));
885-
}
886-
sb.append("]");
887-
break;
888-
}
889-
info = info.toString();
890-
}
891-
return packageJSON(infoType, (s == null ? sb.toString() : s));
892-
}
819+
820+
if (s.indexOf("{\"") != 0) {
821+
// don't doubly fix JSON strings when retrieving status
822+
// what about \1 \2 \3 etc.?
823+
s = esc(s);
824+
}
825+
break;
826+
}
827+
if (info instanceof JSONEncodable) {
828+
// includes javajs.util.BS, org.jmol.script.SV
829+
if ((s = ((JSONEncodable) info).toJSON()) == null)
830+
s = "null"; // perhaps a list has a null value (group3List, for example)
831+
break;
832+
}
833+
sb = new SB();
834+
if (info instanceof Map) {
835+
sb.append("{ ");
836+
String sep = "";
837+
for (String key : ((Map<String, ?>) info).keySet()) {
838+
if (key == null)
839+
key = "null";
840+
sb.append(sep).append(packageJSON(key, toJSON(null, ((Map<?, ?>) info).get(key))));
841+
sep = ",";
842+
}
843+
sb.append(" }");
844+
break;
845+
}
846+
if (info instanceof Lst) {
847+
sb.append("[ ");
848+
int n = ((Lst<?>) info).size();
849+
for (int i = 0; i < n; i++) {
850+
if (i > 0)
851+
sb.appendC(',');
852+
sb.append(toJSON(null, ((Lst<?>) info).get(i)));
853+
}
854+
sb.append(" ]");
855+
break;
856+
}
857+
if (info instanceof M34) {
858+
// M4 extends M3
859+
int len = (info instanceof M4 ? 4 : 3);
860+
float[] x = new float[len];
861+
M34 m = (M34) info;
862+
sb.appendC('[');
863+
for (int i = 0; i < len; i++) {
864+
if (i > 0)
865+
sb.appendC(',');
866+
m.getRow(i, x);
867+
sb.append(toJSON(null, x));
868+
}
869+
sb.appendC(']');
870+
break;
871+
}
872+
s = nonArrayString(info);
873+
if (s == null) {
874+
sb.append("[");
875+
int n = AU.getLength(info);
876+
Object o = null;
877+
/** @j2sNative
878+
* o = info[0];
879+
* typeof o != "number" && typeof 0 != "boolean" && (o = null);
880+
*/
881+
{}
882+
if (o != null) {
883+
sb.appendO(info);
884+
} else {
885+
for (int i = 0; i < n; i++) {
886+
if (i > 0)
887+
sb.appendC(',');
888+
sb.append(toJSON(null, Array.get(info, i)));
889+
}
890+
}
891+
sb.append("]");
892+
break;
893+
}
894+
info = info.toString();
895+
}
896+
return packageJSON(infoType, (s == null ? sb.toString() : s));
897+
}
893898

894899
/**
895900
* Checks to see if an object is an array (including typed arrays), and if it is, returns null;

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,6 @@ public void playAudio(byte[] buffer, Object format) throws Exception {
12191219

12201220
@Override
12211221
public Promise importModule(String resource, Function<Object, Object> resolve, Function<Object,Object> reject) {
1222-
Object o = DOMNode.class;
12231222
loadStaticResource("/_ES6/jsutil.js");
12241223
if (resource.startsWith("$J2S$/"))
12251224
resource = getJ2SPath() + resource.substring(5);
@@ -1228,7 +1227,6 @@ public Promise importModule(String resource, Function<Object, Object> resolve, F
12281227
}
12291228

12301229
public Promise importModuleData(String resource, Function<Object, Object> resolve, Function<Object,Object> reject) {
1231-
Object o = DOMNode.class;
12321230
loadStaticResource("/_ES6/jsutil.js");
12331231
if (resource.startsWith("$J2S$/"))
12341232
resource = getJ2SPath() + resource.substring(5);

sources/net.sf.j2s.java.core/src/test/Test_JSON.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import java.net.MalformedURLException;
99
import java.net.URL;
1010
import java.util.ArrayList;
11+
import java.util.HashMap;
1112
import java.util.Iterator;
1213
import java.util.List;
1314
import java.util.Map;
1415

16+
import org.json.simple.JSONObject;
17+
18+
import javajs.util.PT;
1519
import javajs.util.Rdr;
1620
import swingjs.JSUtil;
1721

@@ -20,6 +24,30 @@ public class Test_JSON extends Test_ {
2024
@SuppressWarnings("unused")
2125
public static void main(String[] args) {
2226
try {
27+
28+
int[] test = new int[] {1, 2, 3};
29+
30+
String ss = PT.toJSON(null, test);
31+
System.out.println(ss);
32+
33+
Integer[] itest = new Integer[] {1, 2, 3};
34+
35+
ss = PT.toJSON(null, itest);
36+
System.out.println(ss);
37+
38+
String[] stest = new String[] {"1", null, "3"};
39+
40+
ss = PT.toJSON(null, stest);
41+
System.out.println(ss);
42+
43+
HashMap<String, String> m = new HashMap<String, String>();
44+
m.put(null, "a");
45+
System.out.println(m.get(null));
46+
System.out.println(PT.toJSON(null, m));
47+
48+
JSONObject jo = new JSONObject(m);
49+
System.out.println(jo.toJSONString());
50+
2351
//
2452
// System.out.println(Double.valueOf(Double.MIN_NORMAL + "1"));
2553
// System.out.println(Long.valueOf(Long.MAX_VALUE+"1"));

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14023,7 +14023,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1402314023

1402414024
// Google closure compiler cannot handle Clazz.new or Clazz.super
1402514025

14026-
// BH 2021.12.19 adds Double -0
14026+
// BH 2021.12.19 adds Double -0; fixes println(Double)
1402714027
// BH 2021.12.15 default encoding for String.getBytes() should be utf-8.
1402814028
// BH 2021.08.16 fix for Interface initializing its subclass with static initialization
1402914029
// BH 2021.07.28 String.instantialize upgraded to use TextDecoder() if possible (not in MSIE)

0 commit comments

Comments
 (0)