@@ -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;
0 commit comments