Test.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * $Id: Test.java,v 1.1 2006/04/15 14:40:06 platform Exp $
  3. * Created on 2006-4-15
  4. */
  5. package org.json.simple;
  6. import java.io.IOException;
  7. import java.io.StringWriter;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.LinkedHashMap;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14. import java.util.Map;
  15. import junit.framework.TestCase;
  16. import org.json.simple.parser.ContainerFactory;
  17. import org.json.simple.parser.ContentHandler;
  18. import org.json.simple.parser.JSONParser;
  19. import org.json.simple.parser.ParseException;
  20. /**
  21. * @author FangYidong<fangyidong@yahoo.com.cn>
  22. */
  23. public class Test extends TestCase{
  24. public void testDecode() throws Exception{
  25. System.out.println("=======decode=======");
  26. String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
  27. Object obj=JSONValue.parse(s);
  28. JSONArray array=(JSONArray)obj;
  29. System.out.println("======the 2nd element of array======");
  30. System.out.println(array.get(1));
  31. System.out.println();
  32. assertEquals("{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}",array.get(1).toString());
  33. JSONObject obj2=(JSONObject)array.get(1);
  34. System.out.println("======field \"1\"==========");
  35. System.out.println(obj2.get("1"));
  36. assertEquals("{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}",obj2.get("1").toString());
  37. s="{}";
  38. obj=JSONValue.parse(s);
  39. assertEquals("{}",obj.toString());
  40. s="[5,]";
  41. obj=JSONValue.parse(s);
  42. assertEquals("[5]",obj.toString());
  43. s="[5,,2]";
  44. obj=JSONValue.parse(s);
  45. assertEquals("[5,2]",obj.toString());
  46. s="[\"hello\\bworld\\\"abc\\tdef\\\\ghi\\rjkl\\n123\\u4e2d\"]";
  47. obj=JSONValue.parse(s);
  48. assertEquals("hello\bworld\"abc\tdef\\ghi\rjkl\n123中",((List)obj).get(0).toString());
  49. JSONParser parser = new JSONParser();
  50. s="{\"name\":";
  51. try{
  52. obj = parser.parse(s);
  53. }
  54. catch(ParseException pe){
  55. assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType());
  56. assertEquals(8, pe.getPosition());
  57. }
  58. s="{\"name\":}";
  59. try{
  60. obj = parser.parse(s);
  61. }
  62. catch(ParseException pe){
  63. assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType());
  64. assertEquals(8, pe.getPosition());
  65. }
  66. s="{\"name";
  67. try{
  68. obj = parser.parse(s);
  69. }
  70. catch(ParseException pe){
  71. assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType());
  72. assertEquals(6, pe.getPosition());
  73. }
  74. s = "[[null, 123.45, \"a\\\tb c\"}, true]";
  75. try{
  76. parser.parse(s);
  77. }
  78. catch(ParseException pe){
  79. assertEquals(24, pe.getPosition());
  80. System.out.println("Error at character position: " + pe.getPosition());
  81. switch(pe.getErrorType()){
  82. case ParseException.ERROR_UNEXPECTED_TOKEN:
  83. System.out.println("Unexpected token: " + pe.getUnexpectedObject());
  84. break;
  85. case ParseException.ERROR_UNEXPECTED_CHAR:
  86. System.out.println("Unexpected character: " + pe.getUnexpectedObject());
  87. break;
  88. case ParseException.ERROR_UNEXPECTED_EXCEPTION:
  89. ((Exception)pe.getUnexpectedObject()).printStackTrace();
  90. break;
  91. }
  92. }
  93. s = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}";
  94. ContainerFactory containerFactory = new ContainerFactory(){
  95. public List creatArrayContainer() {
  96. return new LinkedList();
  97. }
  98. public Map createObjectContainer() {
  99. return new LinkedHashMap();
  100. }
  101. };
  102. try{
  103. Map json = (Map)parser.parse(s, containerFactory);
  104. Iterator iter = json.entrySet().iterator();
  105. System.out.println("==iterate result==");
  106. while(iter.hasNext()){
  107. Map.Entry entry = (Map.Entry)iter.next();
  108. System.out.println(entry.getKey() + "=>" + entry.getValue());
  109. }
  110. System.out.println("==toJSONString()==");
  111. System.out.println(JSONValue.toJSONString(json));
  112. assertEquals("{\"first\":123,\"second\":[4,5,6],\"third\":789}", JSONValue.toJSONString(json));
  113. }
  114. catch(ParseException pe){
  115. pe.printStackTrace();
  116. }
  117. s = "{\"first\": 123, \"second\": [{\"s1\":{\"s11\":\"v11\"}}, 4, 5, 6], \"third\": 789}";
  118. ContentHandler myHandler = new ContentHandler() {
  119. public boolean endArray() throws ParseException {
  120. System.out.println("endArray()");
  121. return true;
  122. }
  123. public void endJSON() throws ParseException {
  124. System.out.println("endJSON()");
  125. }
  126. public boolean endObject() throws ParseException {
  127. System.out.println("endObject()");
  128. return true;
  129. }
  130. public boolean endObjectEntry() throws ParseException {
  131. System.out.println("endObjectEntry()");
  132. return true;
  133. }
  134. public boolean primitive(Object value) throws ParseException {
  135. System.out.println("primitive(): " + value);
  136. return true;
  137. }
  138. public boolean startArray() throws ParseException {
  139. System.out.println("startArray()");
  140. return true;
  141. }
  142. public void startJSON() throws ParseException {
  143. System.out.println("startJSON()");
  144. }
  145. public boolean startObject() throws ParseException {
  146. System.out.println("startObject()");
  147. return true;
  148. }
  149. public boolean startObjectEntry(String key) throws ParseException {
  150. System.out.println("startObjectEntry(), key:" + key);
  151. return true;
  152. }
  153. };
  154. try{
  155. parser.parse(s, myHandler);
  156. }
  157. catch(ParseException pe){
  158. pe.printStackTrace();
  159. }
  160. class KeyFinder implements ContentHandler{
  161. private Object value;
  162. private boolean found = false;
  163. private boolean end = false;
  164. private String key;
  165. private String matchKey;
  166. public void setMatchKey(String matchKey){
  167. this.matchKey = matchKey;
  168. }
  169. public Object getValue(){
  170. return value;
  171. }
  172. public boolean isEnd(){
  173. return end;
  174. }
  175. public void setFound(boolean found){
  176. this.found = found;
  177. }
  178. public boolean isFound(){
  179. return found;
  180. }
  181. public void startJSON() throws ParseException, IOException {
  182. found = false;
  183. end = false;
  184. }
  185. public void endJSON() throws ParseException, IOException {
  186. end = true;
  187. }
  188. public boolean primitive(Object value) throws ParseException, IOException {
  189. if(key != null){
  190. if(key.equals(matchKey)){
  191. found = true;
  192. this.value = value;
  193. key = null;
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. public boolean startArray() throws ParseException, IOException {
  200. return true;
  201. }
  202. public boolean startObject() throws ParseException, IOException {
  203. return true;
  204. }
  205. public boolean startObjectEntry(String key) throws ParseException, IOException {
  206. this.key = key;
  207. return true;
  208. }
  209. public boolean endArray() throws ParseException, IOException {
  210. return false;
  211. }
  212. public boolean endObject() throws ParseException, IOException {
  213. return true;
  214. }
  215. public boolean endObjectEntry() throws ParseException, IOException {
  216. return true;
  217. }
  218. };
  219. s = "{\"first\": 123, \"second\": [{\"k1\":{\"id\":\"id1\"}}, 4, 5, 6, {\"id\": 123}], \"third\": 789, \"id\": null}";
  220. parser.reset();
  221. KeyFinder keyFinder = new KeyFinder();
  222. keyFinder.setMatchKey("id");
  223. int i = 0;
  224. try{
  225. while(!keyFinder.isEnd()){
  226. parser.parse(s, keyFinder, true);
  227. if(keyFinder.isFound()){
  228. i++;
  229. keyFinder.setFound(false);
  230. System.out.println("found id:");
  231. System.out.println(keyFinder.getValue());
  232. if(i == 1)
  233. assertEquals("id1", keyFinder.getValue());
  234. if(i == 2){
  235. assertTrue(keyFinder.getValue() instanceof Number);
  236. assertEquals("123", String.valueOf(keyFinder.getValue()));
  237. }
  238. if(i == 3)
  239. assertTrue(null == keyFinder.getValue());
  240. }
  241. }
  242. }
  243. catch(ParseException pe){
  244. pe.printStackTrace();
  245. }
  246. }
  247. public void testEncode() throws Exception{
  248. System.out.println("=======encode=======");
  249. JSONArray array1=new JSONArray();
  250. array1.add("abc\u0010a/");
  251. array1.add(new Integer(123));
  252. array1.add(new Double(222.123));
  253. array1.add(new Boolean(true));
  254. System.out.println("======array1==========");
  255. System.out.println(array1);
  256. System.out.println();
  257. assertEquals("[\"abc\\u0010a\\/\",123,222.123,true]",array1.toString());
  258. JSONObject obj1=new JSONObject();
  259. obj1.put("name","fang");
  260. obj1.put("age",new Integer(27));
  261. obj1.put("is_developer",new Boolean(true));
  262. obj1.put("weight",new Double(60.21));
  263. obj1.put("array1",array1);
  264. System.out.println("======obj1 with array1===========");
  265. System.out.println(obj1);
  266. System.out.println();
  267. assertEquals("{\"array1\":[\"abc\\u0010a\\/\",123,222.123,true],\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}",obj1.toString());
  268. obj1.remove("array1");
  269. array1.add(obj1);
  270. System.out.println("======array1 with obj1========");
  271. System.out.println(array1);
  272. System.out.println();
  273. assertEquals("[\"abc\\u0010a\\/\",123,222.123,true,{\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}]",array1.toString());
  274. List list = new ArrayList();
  275. list.add("abc\u0010a/");
  276. list.add(new Integer(123));
  277. list.add(new Double(222.123));
  278. list.add(new Boolean(true));
  279. list.add(null);
  280. System.out.println("======list==========");
  281. System.out.println(JSONArray.toJSONString(list));
  282. System.out.println();
  283. assertEquals("[\"abc\\u0010a\\/\",123,222.123,true,null]",JSONArray.toJSONString(list));
  284. Map map = new HashMap();
  285. map.put("name","fang");
  286. map.put("age",new Integer(27));
  287. map.put("is_developer",new Boolean(true));
  288. map.put("weight",new Double(60.21));
  289. map.put("array1",list);
  290. System.out.println("======map with list===========");
  291. System.out.println(map);
  292. System.out.println();
  293. assertEquals("{\"array1\":[\"abc\\u0010a\\/\",123,222.123,true,null],\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}",JSONObject.toJSONString(map));
  294. Map m1 = new LinkedHashMap();
  295. Map m2 = new HashMap();
  296. List l1 = new LinkedList();
  297. m1.put("k11","v11");
  298. m1.put("k12","v12");
  299. m1.put("k13", "v13");
  300. m2.put("k21","v21");
  301. m2.put("k22","v22");
  302. m2.put("k23","v23");
  303. l1.add(m1);
  304. l1.add(m2);
  305. String jsonString = JSONValue.toJSONString(l1);
  306. System.out.println(jsonString);
  307. assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\"},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]", jsonString);
  308. StringWriter out = new StringWriter();
  309. JSONValue.writeJSONString(l1, out);
  310. jsonString = out.toString();
  311. System.out.println(jsonString);
  312. assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\"},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]", jsonString);
  313. List l2 = new LinkedList();
  314. Map m3 = new LinkedHashMap();
  315. m3.put("k31", "v3");
  316. m3.put("k32", new Double(123.45));
  317. m3.put("k33", new Boolean(false));
  318. m3.put("k34", null);
  319. l2.add("vvv");
  320. l2.add("1.23456789123456789");
  321. l2.add(new Boolean(true));
  322. l2.add(null);
  323. m3.put("k35", l2);
  324. m1.put("k14", m3);
  325. out = new StringWriter();
  326. JSONValue.writeJSONString(l1, out);
  327. jsonString = out.toString();
  328. System.out.println(jsonString);
  329. assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\",\"k14\":{\"k31\":\"v3\",\"k32\":123.45,\"k33\":false,\"k34\":null,\"k35\":[\"vvv\",\"1.23456789123456789\",true,null]}},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]",jsonString);
  330. }
  331. }