package org.json;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.io.StringWriter;
/**
 * Test class. This file is not formally a member of the org.json library.
 * It is just a casual test tool.
 */
public class Test {
	
    /**
     * Entry point.
     * @param args
     */
    public static void main(String args[]) {
        Iterator it;
        JSONArray a;
        JSONObject j;
        JSONStringer jj;
        Object o;
        String s;
        
/** 
 *  Obj is a typical class that implements JSONString. It also
 *  provides some beanie methods that can be used to 
 *  construct a JSONObject. It also demonstrates constructing
 *  a JSONObject with an array of names.
 */
        class Obj implements JSONString {
        	public String aString;
        	public double aNumber;
        	public boolean aBoolean;
        	
            public Obj(String string, double n, boolean b) {
                this.aString = string;
                this.aNumber = n;
                this.aBoolean = b;
            }
            
            public double getNumber() {
            	return this.aNumber;
            }
            
            public String getString() {
            	return this.aString;
            }
            
            public boolean isBoolean() {
            	return this.aBoolean;
            }
            
            public String getBENT() {
            	return "All uppercase key";
            }
            
            public String getX() {
            	return "x";
            }
            
            public String toJSONString() {
            	return "{" + JSONObject.quote(this.aString) + ":" + 
            	JSONObject.doubleToString(this.aNumber) + "}";
            }            
            public String toString() {
            	return this.getString() + " " + this.getNumber() + " " + 
            			this.isBoolean() + "." + this.getBENT() + " " + this.getX();
            }
        }      
        
    	Obj obj = new Obj("A beany object", 42, true);
        
        try {     
            s = "[0.1]";
            a = new JSONArray(s);
            System.out.println(a.toString());
            System.out.println("");
            
            j = XML.toJSONObject("  Ignore the stuff past the end.  ");
            System.out.println(j.toString());
            System.out.println("");
            
            j = new JSONObject();
            o = null;
            j.put("booga", o);
            j.put("wooga", JSONObject.NULL);
            System.out.println(j.toString());
            System.out.println("");
           
            j = new JSONObject();
            j.increment("two");
            j.increment("two");
            System.out.println(j.toString());
            System.out.println("");
            
            
            s = "";
            j = XML.toJSONObject(s);
            System.out.println(j.toString(2));
            System.out.println(XML.toString(j));
            System.out.println("");
            s = "{     \"list of lists\" : [         [1, 2, 3],         [4, 5, 6],     ] }";
            j = new JSONObject(s);
            System.out.println(j.toString(4));
            System.out.println(XML.toString(j));
                    
            s = " Basic bread Flour Yeast Water Salt  Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes.   ";
            j = XML.toJSONObject(s);
            System.out.println(j.toString(4));
            System.out.println();
            
            j = JSONML.toJSONObject(s);
            System.out.println(j.toString());
            System.out.println(JSONML.toString(j));
            System.out.println();
            
            a = JSONML.toJSONArray(s);
            System.out.println(a.toString(4));
            System.out.println(JSONML.toString(a));
            System.out.println();
            
            s = "
JSONML is a transformation between JSON and XML that preserves ordering of document features.
JSONML can work with JSON arrays or JSON objects.
Three
little
words
Repository Address - Special Collections Library
- ABC University
- Main Library, 40 Circle Drive
- Ourtown, Pennsylvania
- 17654 USA
");
            System.out.println(j.toString());
            System.out.println(XML.toString(j));
            System.out.println("");
            j = XML.toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2");
            System.out.println(j.toString(2));
            System.out.println(XML.toString(j));
            System.out.println("");
            j = HTTP.toJSONObject("GET / HTTP/1.0\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\nAccept-Language: en-us\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\nHost: www.nokko.com\nConnection: keep-alive\nAccept-encoding: gzip, deflate\n");
            System.out.println(j.toString(2));
            System.out.println(HTTP.toString(j));
            System.out.println("");
            j = HTTP.toJSONObject("HTTP/1.1 200 Oki Doki\nDate: Sun, 26 May 2002 17:38:52 GMT\nServer: Apache/1.3.23 (Unix) mod_perl/1.26\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\nTransfer-Encoding: chunked\nContent-Type: text/html\n");
            System.out.println(j.toString(2));
            System.out.println(HTTP.toString(j));
            System.out.println("");
            j = new JSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
            System.out.println(j.toString(2));
            System.out.println("isNull: " + j.isNull("nix"));
            System.out.println("   has: " + j.has("nix"));
            System.out.println(XML.toString(j));
            System.out.println(HTTP.toString(j));
            System.out.println("");
            j = XML.toJSONObject(""+"\n\n"+""+
              ""+
              "GOOGLEKEY'+search+'
 0 10 true  false  latin1 latin1"+
              ""+
              "");
            System.out.println(j.toString(2));
            System.out.println(XML.toString(j));
            System.out.println("");
            j = new JSONObject("{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}");
            System.out.println(j.toString(2));
            System.out.println(XML.toString(j));
            System.out.println("");
            j = CookieList.toJSONObject("  f%oo = b+l=ah  ; o;n%40e = t.wo ");
            System.out.println(j.toString(2));
            System.out.println(CookieList.toString(j));
            System.out.println("");
            j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
            System.out.println(j.toString(2));
            System.out.println(Cookie.toString(j));
            System.out.println("");
            j = new JSONObject("{script: 'It is not allowed in HTML to send a close script tag in a stringso we insert a backslash before the /'}");
            System.out.println(j.toString());
            System.out.println("");
            JSONTokener jt = new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
            j = new JSONObject(jt);
            System.out.println(j.toString());
            System.out.println("pre: " + j.optInt("pre"));
            int i = jt.skipTo('{');
            System.out.println(i);
            j = new JSONObject(jt);
            System.out.println(j.toString());
            System.out.println("");
            a = CDL.toJSONArray("Comma delimited list test, '\"Strip\"Quotes', 'quote, comma', No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
            s = CDL.toString(a);
            System.out.println(s);
            System.out.println("");
            System.out.println(a.toString(4));
            System.out.println("");
            a = CDL.toJSONArray(s);
            System.out.println(a.toString(4));
            System.out.println("");
            a = new JSONArray(" [\"\", next is an implied null , , ok,] ");
            System.out.println(a.toString());
            System.out.println("");
            System.out.println(XML.toString(a));
            System.out.println("");
            j = new JSONObject("{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=>  o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
            System.out.println(j.toString(4));
            System.out.println("");
            if (j.getBoolean("true") && !j.getBoolean("false")) {
                System.out.println("It's all good");
            }
            System.out.println("");
            j = new JSONObject(j, new String[]{"dec", "oct", "hex", "missing"});
            System.out.println(j.toString(4));
            System.out.println("");
            System.out.println(new JSONStringer().array().value(a).value(j).endArray());
            j = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
            System.out.println(j.toString(4));
            System.out.println("\ngetInt");
            System.out.println("int    " + j.getInt("int"));
            System.out.println("long   " + j.getInt("long"));
            System.out.println("longer " + j.getInt("longer"));
            //System.out.println("double " + j.getInt("double"));
            //System.out.println("string " + j.getInt("string"));
            System.out.println("\ngetLong");
            System.out.println("int    " + j.getLong("int"));
            System.out.println("long   " + j.getLong("long"));
            System.out.println("longer " + j.getLong("longer"));
            //System.out.println("double " + j.getLong("double"));
            //System.out.println("string " + j.getLong("string"));
            System.out.println("\ngetDouble");
            System.out.println("int    " + j.getDouble("int"));
            System.out.println("long   " + j.getDouble("long"));
            System.out.println("longer " + j.getDouble("longer"));
            System.out.println("double " + j.getDouble("double"));
            System.out.println("string " + j.getDouble("string"));
            j.put("good sized", 9223372036854775807L);
            System.out.println(j.toString(4));
            a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
            System.out.println(a.toString(4));
            System.out.println("\nKeys: ");
            it = j.keys();
            while (it.hasNext()) {
                s = (String)it.next();
                System.out.println(s + ": " + j.getString(s));
            }
            System.out.println("\naccumulate: ");
            j = new JSONObject();
            j.accumulate("stooge", "Curly");
            j.accumulate("stooge", "Larry");
            j.accumulate("stooge", "Moe");
            a = j.getJSONArray("stooge");
            a.put(5, "Shemp");
            System.out.println(j.toString(4));
            System.out.println("\nwrite:");
            System.out.println(j.write(new StringWriter()));
            s = "122333";
            j = XML.toJSONObject(s);
            System.out.println(j.toString(4));
            System.out.println(XML.toString(j));
            
            s = "Content of the first chapterContent of the second chapter      Content of the first subchapter      Content of the second subchapterThird Chapter";
            j = XML.toJSONObject(s);
            System.out.println(j.toString(4));
            System.out.println(XML.toString(j));
            
            a = JSONML.toJSONArray(s);
            System.out.println(a.toString(4));
            System.out.println(JSONML.toString(a));
            
            Collection c = null;
            Map m = null;
            
            j = new JSONObject(m);
            a = new JSONArray(c);
            j.append("stooge", "Joe DeRita");
            j.append("stooge", "Shemp");
            j.accumulate("stooges", "Curly");
            j.accumulate("stooges", "Larry");
            j.accumulate("stooges", "Moe");
            j.accumulate("stoogearray", j.get("stooges"));
            j.put("map", m);
            j.put("collection", c);
            j.put("array", a);
            a.put(m);
            a.put(c);
            System.out.println(j.toString(4));
            
            s = "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa;  Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } "; 
            j = new JSONObject(s);
            System.out.println(j.toString(4));
            
            s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")";
            a = new JSONArray(s);
            System.out.println(a.toString());
            
            s = "The content of b and The content of cdoremi";
            j = XML.toJSONObject(s);
            System.out.println(j.toString(2));
            System.out.println(XML.toString(j));
            System.out.println("");
            ja = JSONML.toJSONArray(s);
            System.out.println(ja.toString(4));
            System.out.println(JSONML.toString(ja));
            System.out.println("");
            
            s = "111111111111111";
            j = JSONML.toJSONObject(s);
            System.out.println(j);
            ja = JSONML.toJSONArray(s);
            System.out.println(ja);
          
            
            System.out.println("\nTesting Exceptions: ");
            System.out.print("Exception: ");
            try {
                a = new JSONArray("[\n\r\n\r}");
                System.out.println(a.toString());
            } catch (Exception e) {
                System.out.println(e);
            }
            
            System.out.print("Exception: ");
            try {
                a = new JSONArray("<\n\r\n\r      ");
                System.out.println(a.toString());
            } catch (Exception e) {
                System.out.println(e);
            }
            
            System.out.print("Exception: ");
            try {
                a = new JSONArray();
                a.put(Double.NEGATIVE_INFINITY);
                a.put(Double.NaN);
                System.out.println(a.toString());
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(j.getDouble("stooge"));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(j.getDouble("howard"));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(j.put(null, "howard"));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(a.getDouble(0));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(a.get(-1));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
                System.out.println(a.put(Double.NaN));
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.print("Exception: ");
            try {
            	j = XML.toJSONObject("    ");
            } catch (Exception e) {
            	System.out.println(e);
            }            
            System.out.print("Exception: ");
            try {
            	j = XML.toJSONObject("    ");
            } catch (Exception e) {
            	System.out.println(e);
            }            
            System.out.print("Exception: ");
            try {
            	j = XML.toJSONObject("";
                ja = JSONML.toJSONArray(s);
                System.out.println(ja.toString(4));
            } catch (Exception e) {
            	System.out.println(e);
            }
            System.out.print("Exception: ");
            try {            	
                s = "{\"koda\": true, \"koda\": true}";
                j = new JSONObject(s);
                System.out.println(j.toString(4));
            } catch (Exception e) {
            	System.out.println(e);
            }
            System.out.print("Exception: ");
            try {            	
                jj = new JSONStringer();
                s = jj
    	            .object()
    	                .key("bosanda")
    	                .value("MARIE HAA'S")
    	                .key("bosanda")
    	                .value("MARIE HAA\\'S")
    	            .endObject()
    	            .toString();
                System.out.println(j.toString(4));
            } catch (Exception e) {
            	System.out.println(e);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}