org.chromium.sdk/lib/json_simple/README.txt
author John Kern <johnk@symbian.org>
Mon, 01 Feb 2010 13:29:56 -0800
changeset 68 0f778d24a639
parent 2 e4420d2515f1
permissions -rw-r--r--
cron file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     1
Simple Java toolkit for JSON (JSON.simple)
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     2
==========================================
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     3
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     4
1.Why the Simple Java toolkit (also named as JSON.simple) for JSON?
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     5
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     6
  When I use JSON as the data exchange format between the AJAX client and JSP 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     7
  for the first time, what worry me mostly is how to encode Java strings and 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     8
  numbers correctly in the server side so the AJAX client will receive a well
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
     9
  formed JSON data. When I looked into the 'JSON in Java' directory in JSON
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    10
  website,I found that wrappers to JSONObject and JSONArray can be simpler, 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    11
  due to the simplicity of JSON itself. So I wrote the JSON.simple package.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    12
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    13
2.Is it simple,really?
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    14
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    15
  I think so. Take an example:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    16
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    17
  import org.json.simple.JSONObject;
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    18
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    19
  JSONObject obj=new JSONObject();
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    20
  obj.put("name","foo");
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    21
  obj.put("num",new Integer(100));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    22
  obj.put("balance",new Double(1000.21));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    23
  obj.put("is_vip",new Boolean(true));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    24
  obj.put("nickname",null);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    25
  System.out.print(obj);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    26
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    27
  Result:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    28
  {"nickname":null,"num":100,"balance":1000.21,"is_vip":true,"name":"foo"}
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    29
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    30
  The JSONObject.toString() will escape controls and specials correctly.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    31
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    32
3.How to use JSON.simple in JSP?
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    33
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    34
  Take an example in JSP:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    35
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    36
  <%@page contentType="text/html; charset=UTF-8"%>
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    37
  <%@page import="org.json.simple.JSONObject"%>
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    38
  <%
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    39
	JSONObject obj=new JSONObject();
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    40
  	obj.put("name","foo");
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    41
  	obj.put("num",new Integer(100));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    42
  	obj.put("balance",new Double(1000.21));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    43
  	obj.put("is_vip",new Boolean(true));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    44
  	obj.put("nickname",null);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    45
	out.print(obj);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    46
	out.flush();
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    47
  %>
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    48
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    49
  So the AJAX client will get the responseText.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    50
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    51
4.Some details about JSONObject?
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    52
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    53
  JSONObject inherits java.util.HashMap,so it don't have to worry about the 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    54
  mapping things between keys and values. Feel free to use the Map methods 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    55
  like get(), put(), and remove() and others. JSONObject.toString() will 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    56
  combine key value pairs to get the JSON data string. Values will be escaped
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    57
  into JSON quote string format if it's an instance of java.lang.String. Other
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    58
  type of instance like java.lang.Number,java.lang.Boolean,null,JSONObject and
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    59
  JSONArray will NOT escape, just take their java.lang.String.valueOf() result.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    60
  null value will be the JSON 'null' in the result.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    61
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    62
  It's still correct if you put an instance of JSONObject or JSONArray into an 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    63
  instance of JSONObject or JSONArray. Take the example about:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    64
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    65
  JSONObject obj2=new JSONObject();
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    66
  obj2.put("phone","123456");
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    67
  obj2.put("zip","7890");
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    68
  obj.put("contact",obj2);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    69
  System.out.print(obj);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    70
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    71
  Result:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    72
  {"nickname":null,"num":100,"contact":{"phone":"123456","zip":"7890"},"balance":1000.21,"is_vip":true,"name":"foo"}
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    73
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    74
  The method JSONObject.escape() is used to escape Java string into JSON quote 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    75
  string. Controls and specials will be escaped correctly into \b,\f,\r,\n,\t,
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    76
  \",\\,\/,\uhhhh.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    77
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    78
5.Some detail about JSONArray?
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    79
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    80
  org.json.simple.JSONArray inherits java.util.ArrayList. Feel free to use the
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    81
  List methods like get(),add(),remove(),iterator() and so on. The rules of 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    82
  JSONArray.toString() is similar to JSONObject.toString(). Here's the example:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    83
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    84
  import org.json.simple.JSONArray;
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    85
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    86
  JSONArray array=new JSONArray();
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    87
  array.add("hello");
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    88
  array.add(new Integer(123));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    89
  array.add(new Boolean(false));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    90
  array.add(null);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    91
  array.add(new Double(123.45));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    92
  array.add(obj2);//see above
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    93
  System.out.print(array);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    94
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    95
  Result:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    96
  ["hello",123,false,null,123.45,{"phone":"123456","zip":"7890"}]
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    97
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    98
6.What is JSONValue for?  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
    99
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   100
  org.json.simple.JSONValue is use to parse JSON data into Java Object. 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   101
  In JSON, the topmost entity is JSON value, not the JSON object. But
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   102
  it's not necessary to wrap JSON string,boolean,number and null again,
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   103
  for the Java has already had the according classes: java.lang.String,
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   104
  java.lang.Boolean,java.lang.Number and null. The mapping is:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   105
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   106
  JSON			Java
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   107
  ------------------------------------------------
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   108
  string      <=>  	java.lang.String 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   109
  number      <=>	java.lang.Number
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   110
  true|false  <=>	java.lang.Boolean
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   111
  null        <=>	null
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   112
  array	      <=>	org.json.simple.JSONArray
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   113
  object      <=>       org.json.simple.JSONObject
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   114
  ------------------------------------------------
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   115
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   116
  JSONValue has only one kind of method, JSONValue.parse(), which receives
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   117
  a java.io.Reader or java.lang.String. Return type of JSONValue.parse() 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   118
  is according to the mapping above. If the input is incorrect in syntax or
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   119
  there's exceptions during the parsing, I choose to return null, ignoring 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   120
  the exception: I have no idea if it's a serious implementaion, but I think
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   121
  it's convenient to the user.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   122
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   123
  Here's the example:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   124
  
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   125
  String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   126
  Object obj=JSONValue.parse(s);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   127
  JSONArray array=(JSONArray)obj;
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   128
  System.out.println(array.get(1));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   129
  JSONObject obj2=(JSONObject)array.get(1);
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   130
  System.out.println(obj2.get("1"));
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   131
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   132
  Result:
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   133
  {"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   134
  {"2":{"3":{"4":[5,{"6":7}]}}}
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   135
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   136
7.About the author.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   137
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   138
  I'm a Java EE developer on Linux. 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   139
  I'm working on web systems and information retrieval systems.
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   140
  I also develop 3D games and Flash games. 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   141
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   142
  You can contact me through: 
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   143
  Fang Yidong<fangyidong@yahoo.com.cn>
e4420d2515f1 Initial version of WRT Debugger.
TasneemS@US-TASNEEMS
parents:
diff changeset
   144
  Fang Yidong<fangyidng@gmail.com>