55import java .io .FileReader ;
66import java .math .BigDecimal ;
77import java .util .ArrayList ;
8+ import java .util .Arrays ;
89import java .util .Date ;
910import org .miktim .JSON ;
1011
@@ -24,19 +25,28 @@ public static void main(String[] args) throws Exception {
2425 path = args [0 ];
2526 }
2627
27- log ("JSON package test" );
28+ log ("JSON class test" );
2829
2930 log ("\n \r Test constructor:" );
30- log (new JSON ("One" , 1 , "Two" , 2 , 3 , "Three" ));
31-
31+ JSON json = new JSON ("One" , 1 , "Two" , 2 , 3 , "Three" ,
32+ "Nested" , new JSON ("Array" ,
33+ new float [][]{{1.1f , 2.2f }, {3.3f , 4.4f }}));
34+ log (json );
35+ log (json .getNumber ("One" ).floatValue ()); // Number(Integer)
36+ // java.lang.ClassCastException
37+ // log(json.getString("Two"));
38+ log (json .stringify ("Two" ));
39+ log (json .getString ("3" ));
40+ log (json .getJSON ("Nested" ).getNumber ("Array" , 1 , 1 ).intValue ());
41+
3242 log ("\n \r Test escape/unescape string:" );
3343 String unescaped = new String (new char []{0x8 , 0x9 , 0xA , 0xC , 0xD , 0x22 , 0x2F , 0x5C , 0 , '-' , 0x1F , 0xd834 , 0xdd1e });
3444 String escaped = JSON .escapeString (unescaped );
3545 log (escaped );
3646 logIsOK (unescaped .equals (JSON .unescapeString (escaped )));
3747
38- log ("\n \r Test create/stringify JSON object :" );
39- JSON json = (new JSON ())
48+ log ("\n \r Test JSON typecast. \r \n Before normalization :" );
49+ json = (new JSON ())
4050 .set ("Unescaped" , unescaped )
4151 .set ("EmptyJSON" , new JSON ())
4252 .set ("intArray" , new int [][]{{0 , 1 , 2 }, {3 , 4 , 5 , 6 }})
@@ -50,69 +60,67 @@ public static void main(String[] args) throws Exception {
5060 .set ("Int" , 14159265 )
5161 .set ("Byte" , (byte ) 0xFF );
5262 log (json );
53- log ("List members: " + JSON .stringify (json .list ()));
63+
64+ log ("List members: " + json .list ());
5465 for (String memberName : json .list ()) {
5566 if (json .get (memberName ) != null ) {
5667 log ("\" " + memberName + "\" is instance of: "
5768 + json .get (memberName ).getClass ().getSimpleName ());
5869 } else {
59- log ("\" " + memberName + "\" is " + json . get ( memberName ) );
70+ log ("\" " + memberName + "\" is null" );
6071 }
6172 }
6273
63- log ("\n \r Test stringify/parse JSON:" );
74+ log ("\n \r Normalized:" );
75+ json = json .normalize ();
6476 log (json );
65- log ("List members: " + JSON .stringify (json .list ()));
66- json = (JSON ) JSON .parse (json .toString ());
67- log (json );
68- log ("List members: " + JSON .stringify (json .list ()));
77+ log ("List members: " + json .list ());
6978 for (String memberName : json .list ()) {
7079 if (json .get (memberName ) != null ) {
7180 log ("\" " + memberName + "\" is instance of: "
7281 + json .get (memberName ).getClass ().getSimpleName ());
7382 } else {
74- log ("\" " + memberName + "\" is " + json . get ( memberName ) );
83+ log ("\" " + memberName + "\" is null" );
7584 }
7685 }
7786
78- log ("\n \r Test nullnamed/nonexistent member:" );
79- log ("Remove null/nonexistent member returns: "
80- + json .remove (null ) + "/" + json .remove ("nonexistent" ));
81- log ("Get null/nonexistent member returns: "
82- + json .get (null ) + "/" + json .get ("nonexistent" ));
83- log ("Exists null/nonexistent member returns: "
84- + json .exists (null ) + "/" + json .exists ("nonexistent" ));
85-
86- log ("\n \r Test JSON clone then remove \" BigDecimal\" :" );
87- JSON cloned = (JSON ) json .clone ();
88- cloned .remove ("BigDecimal" );
89- log (json .list ());
90- log (cloned .list ());
91-
9287 log ("\n \r Test JSON with Java arrays:" );
9388 int [][] intArray = new int [][]{{0 , 1 , 2 }, {3 , 4 , 5 , 6 }};
94- log (JSON .stringify (intArray ));
95- cloned .set ("Array" , intArray );
96- log (JSON .stringify (cloned .get ("Array" )));
97- intArray [0 ][1 ] = 6 ;
98- log (JSON .stringify (cloned .get ("Array" )));
99- Object [] array = intArray ;
100- array [1 ] = new int []{7 , 8 , 9 };
101- log (JSON .stringify (array ));
102- log (JSON .stringify (cloned .get ("Array" )));
103-
104- log ("\n \r Test generator with other Java objects (ArrayList with Date, File entries):" );
89+ json = new JSON ("Array" , intArray );
90+ log (json .get ("Array" ).getClass ().getSimpleName ());
91+ // array is instance of int[][]
92+ // java.lang.ClassCastException
93+ // log(json.getArray("Array", 1).getClass().getSimpleName());
94+ log (json .getNumber ("Array" , 1 , 1 ).floatValue ());
95+
96+ log (json = json .normalize ());
97+ log (json .get ("Array" ).getClass ().getSimpleName ());
98+ // array is instance of Object[]
99+ log (json .getNumber ("Array" , 1 , 1 ).floatValue ());
100+ Object [] array ;
101+ array = json .getArray ("Array" );
102+ log (array .length );
103+ array = json .getArray ("Array" ,1 );
104+ log (array .length );
105+ // cast array. doesn't make much sense
106+ Number [] na = Arrays .copyOf (array , array .length , Number [].class );
107+ log (JSON .stringify (na ));
108+
109+ log ("\n \r Test generator with other Java objects (ArrayList with Array, Date and File entries):" );
105110 ArrayList <Object > arrayList = new ArrayList <>();
111+ arrayList .add (new int [2 ]);
106112 arrayList .add (new Date ());
107113 arrayList .add (new File (path , "json.json" ));
108- log (JSON .stringify (arrayList ));
114+ String s = JSON .stringify (arrayList );
115+ log (s );
116+ Object o = JSON .parse (s );
117+ log ("is Array?: " + o .getClass ().isArray ());
109118
110119// examples from RFC 8259 https://datatracker.ietf.org/doc/rfc8259/?include_text=1
111120 log ("\n \r Test examples from RFC 8259:" );
112121 log (JSON .parse ("\" \\ uD834\\ uDD1E\" " )); // G-clef
113122 log (JSON .parse ("3.141592653589793238462643383279" ));
114123 log (JSON .parse ("null" ));
115- log (JSON .stringify (JSON .parse ("\t [1, null, 3.2, {}, \" some text\" ] " )));
116124
117125 String example1 = "{\n "
118126 + " \" Image\" : {\n "
@@ -129,11 +137,11 @@ public static void main(String[] args) throws Exception {
129137 + " \" IDs\" : [116, 943, 234, 38793]\n "
130138 + " }\n "
131139 + " } " ;
132- Object object = JSON .parse (example1 );
133- log ((( JSON ) object ). toString () );
134- log ((( JSON ) object ) .get ("Image" ));
135- log ((( JSON ) ((( JSON ) object ). get ("Image" ))) .set ("Thumbnail" , 256 ));
136- log ((( JSON ) ((( JSON ) object ). get ("Image" ))) .remove ("Thumbnail" ));
140+ json = ( JSON ) JSON .parse (example1 );
141+ log (json );
142+ log (json .get ("Image" ));
143+ log (json . getJSON ("Image" ).set ("Thumbnail" , 256 )); // replace JSON object with number
144+ log (json . getJSON ("Image" ).remove ("Thumbnail" )); // remove member
137145 String example2 = "[\n "
138146 + " {\n "
139147 + " \" precision\" : \" zip\" ,\n "
@@ -156,9 +164,9 @@ public static void main(String[] args) throws Exception {
156164 + " \" Country\" : \" US\" \n "
157165 + " }\n "
158166 + " ]" ;
159- object = JSON .parse (example2 );
160- log (JSON .stringify (object ));
161- log (JSON .stringify ((( Object []) object ) [1 ]));
167+ array = ( Object []) JSON .parse (example2 );
168+ log (JSON .stringify (array ));
169+ log (JSON .stringify (array [1 ]));
162170
163171// Example from https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/json-data.html#GUID-FBC22D72-AA64-4B0A-92A2-837B32902E2C
164172 log ("\n \r Test one more example:" );
0 commit comments