KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > json > client > JSONParser


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.json.client;
17
18 import com.google.gwt.core.client.JavaScriptException;
19 import com.google.gwt.core.client.JavaScriptObject;
20
21 /**
22  * Parses the string representation of a JSON object into a set of
23  * JSONValue-derived objects.
24  *
25  * @see com.google.gwt.json.client.JSONValue
26  */

27 public class JSONParser {
28
29   /**
30    * Given a jsonString, returns the JSONObject representation. For efficiency,
31    * parsing occurs lazily as the structure is requested.
32    *
33    * @param jsonString
34    * @return a JSONObject that has been built by parsing the JSON string
35    * @throws NullPointerException if <code>jsonString</code> is
36    * <code>null</code>
37    * @throws IllegalArgumentException if <code>jsonString</code> is empty
38    */

39   public static JSONValue parse(String JavaDoc jsonString) {
40     // Create a JavaScriptObject from the JSON string.
41
//
42
if (jsonString == null) {
43       throw new NullPointerException JavaDoc();
44     }
45     if (jsonString == "") {
46       throw new IllegalArgumentException JavaDoc("empty argument");
47     }
48     try {
49       JavaScriptObject jsonObject = evaluate(jsonString);
50       return buildValue(jsonObject);
51     } catch (JavaScriptException ex) {
52       throw new JSONException(ex);
53     }
54   }
55
56   /**
57    * Returns the {@link JSONValue} for a given {@link JavaScriptObject}.
58    *
59    * @param jsValue {@link JavaScriptObject} to build a {@link JSONValue} for,
60    * this object cannot be a primitive JavaScript type
61    * @return a {@link JSONValue} instance for the {@link JavaScriptObject}
62    */

63   static JSONValue buildValue(JavaScriptObject jsValue) throws JSONException {
64     
65     if (isNull(jsValue)) {
66       return JSONNull.getInstance();
67     }
68
69     if (isArray(jsValue)) {
70       return new JSONArray(jsValue);
71     }
72
73     if (isBoolean(jsValue)) {
74       return JSONBoolean.getInstance(asBoolean(jsValue));
75     }
76
77     if (isString(jsValue)) {
78       return new JSONString(asString(jsValue));
79     }
80
81     if (isDouble(jsValue)) {
82       return new JSONNumber(asDouble(jsValue));
83     }
84
85     if (isObject(jsValue)) {
86       return new JSONObject(jsValue);
87     }
88
89     /*
90      * In practice we should never reach this point. If we do, we cannot make
91      * any assumptions about the jsValue.
92      */

93     throw new JSONException("Unknown JavaScriptObject type");
94   }
95
96   /**
97    * Returns the boolean represented by the jsValue. This method
98    * assumes that {@link #isBoolean(JavaScriptObject)} returned
99    * <code>true</code>.
100    *
101    * @param jsValue JavaScript object to convert
102    * @return the boolean represented by the jsValue
103    */

104   private static native boolean asBoolean(JavaScriptObject jsValue) /*-{
105     return jsValue.valueOf();
106   }-*/
;
107
108   /**
109    * Returns the double represented by jsValue. This method assumes that
110    * {@link #isDouble(JavaScriptObject)} returned <code>true</code>.
111    *
112    * @param jsValue JavaScript object to convert
113    * @return the double represented by the jsValue
114    */

115   private static native double asDouble(JavaScriptObject jsValue) /*-{
116     return jsValue.valueOf();
117   }-*/
;
118
119   /**
120    * Returns the Javascript String as a Java String. This method assumes that
121    * {@link #isString(JavaScriptObject)} returned <code>true</code>.
122    *
123    * @param jsValue JavaScript object to convert
124    * @return the String represented by the jsValue
125    */

126   private static native String JavaDoc asString(JavaScriptObject jsValue) /*-{
127     return jsValue;
128   }-*/
;
129
130   /*
131    * This method converts the json string into a JavaScriptObject inside of JSNI
132    * method by simply evaluating the string in JavaScript.
133    */

134   private static native JavaScriptObject evaluate(String JavaDoc jsonString) /*-{
135     var x = eval('(' + jsonString + ')');
136     if (typeof x == 'number' || typeof x == 'string' || typeof x == 'array' || typeof x == 'boolean') {
137       x = (Object(x));
138     }
139     return x;
140   }-*/
;
141
142   /**
143    * Returns <code>true</code> if the {@link JavaScriptObject} is a wrapped
144    * JavaScript Array.
145    *
146    * @param jsValue JavaScript object to test
147    * @return <code>true</code> if jsValue is a wrapped JavaScript Array
148    */

149   private static native boolean isArray(JavaScriptObject jsValue) /*-{
150     return jsValue instanceof Array;
151   }-*/
;
152
153   /**
154    * Returns <code>true</code> if the {@link JavaScriptObject} is a wrapped
155    * JavaScript Boolean.
156    *
157    * @param jsValue JavaScript object to test
158    * @return <code>true</code> if jsValue is a wrapped JavaScript Boolean
159    */

160   private static native boolean isBoolean(JavaScriptObject jsValue) /*-{
161     return jsValue instanceof Boolean;
162   }-*/
;
163
164   /**
165    * Returns <code>true</code> if the {@link JavaScriptObject} is a wrapped
166    * JavaScript Double.
167    *
168    * @param jsValue JavaScript object to test
169    * @return <code>true</code> if jsValue is a wrapped JavaScript Double
170    */

171   private static native boolean isDouble(JavaScriptObject jsValue) /*-{
172     return jsValue instanceof Number;
173   }-*/
;
174
175   /**
176    * Returns <code>true</code> if the {@link JavaScriptObject} is <code>null</code>
177    * or <code>undefined</code>.
178    *
179    * @param jsValue JavaScript object to test
180    * @return <code>true</code> if jsValue is <code>null</code> or
181    * <code>undefined</code>
182    */

183   private static native boolean isNull(JavaScriptObject jsValue) /*-{
184     return jsValue == null;
185   }-*/
;
186
187   /**
188    * Returns <code>true</code> if the {@link JavaScriptObject} is a JavaScript
189    * Object.
190    *
191    * @param jsValue JavaScript object to test
192    * @return <code>true</code> if jsValue is a JavaScript Object
193    */

194   private static native boolean isObject(JavaScriptObject jsValue) /*-{
195     return jsValue instanceof Object;
196   }-*/
;
197
198   /**
199    * Returns <code>true</code> if the {@link JavaScriptObject} is a JavaScript
200    * String.
201    *
202    * @param jsValue JavaScript object to test
203    * @return <code>true</code> if jsValue is a JavaScript String
204    */

205   private static native boolean isString(JavaScriptObject jsValue) /*-{
206     return jsValue instanceof String;
207   }-*/
;
208
209   /**
210    * Not instantiable.
211    */

212   private JSONParser() {
213   }
214 }
Popular Tags