KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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.JavaScriptObject;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * Represents a JSON object. A JSON object is a map of string-based keys onto a
25  * set of {@link com.google.gwt.json.client.JSONValue} objects.
26  */

27 public class JSONObject extends JSONValue {
28   private final JavaScriptObject backStore;
29
30   private final JavaScriptObject frontStore = createBlankObject();
31
32   public JSONObject() {
33     backStore = createBlankObject();
34   }
35
36   /**
37    * Creates a new JSONObject from the supplied JavaScript value.
38    */

39   public JSONObject(JavaScriptObject jsValue) {
40     backStore = jsValue;
41   }
42
43   /**
44    * Tests whether or not this JSONObject contains the specified key.
45    *
46    * We use Object.hasOwnProperty here to verify that a given key is
47    * specified on this object rather than a superclass (such as standard
48    * properties defined on Object).
49    *
50    * @param key the key to search for
51    * @return <code>true</code> if the JSONObject contains the specified key
52    */

53   public native boolean containsKey(String JavaDoc key) /*-{
54     return Object.prototype.hasOwnProperty.call(
55         this.@com.google.gwt.json.client.JSONObject::backStore, key)
56         || Object.prototype.hasOwnProperty.call(
57         this.@com.google.gwt.json.client.JSONObject::frontStore, key);
58   }-*/
;
59
60   /**
61    * Gets the JSONValue associated with the specified key.
62    *
63    * We use Object.hasOwnProperty here to verify that a given key is
64    * specified on this object rather than a superclass (such as standard
65    * properties defined on Object).
66    *
67    * @param key the key to search for
68    * @return if found, the value associated with the specified key, or
69    * <code>null</code> otherwise
70    */

71   public native JSONValue get(String JavaDoc key) /*-{
72     if (Object.prototype.hasOwnProperty.call(
73         this.@com.google.gwt.json.client.JSONObject::backStore, key))
74     {
75       var x = this.@com.google.gwt.json.client.JSONObject::backStore[key];
76       if (typeof x == 'number' || typeof x == 'string' || typeof x == 'array'
77           || typeof x == 'boolean')
78       {
79         x = Object(x);
80       }
81       this.@com.google.gwt.json.client.JSONObject::frontStore[key]=
82           // don't linebreak the next line
83           @com.google.gwt.json.client.JSONParser::buildValue(Lcom/google/gwt/core/client/JavaScriptObject;)(x);
84       delete this.@com.google.gwt.json.client.JSONObject::backStore[key];
85     }
86     if (Object.prototype.hasOwnProperty.call(
87         this.@com.google.gwt.json.client.JSONObject::frontStore, key))
88     {
89       return this.@com.google.gwt.json.client.JSONObject::frontStore[key];
90     }
91     return null;
92   }-*/
;
93
94   /**
95    * Returns <code>this</code>, as this is a JSONObject.
96    */

97   public JSONObject isObject() {
98     return this;
99   }
100
101   /**
102    * Returns keys for which this JSONObject has associations.
103    *
104    * @return array of keys for which there is a value
105    */

106   public Set JavaDoc keySet() {
107     Set JavaDoc keySet = new HashSet JavaDoc();
108     addAllKeysFromJavascriptObject(keySet, frontStore);
109     addAllKeysFromJavascriptObject(keySet, backStore);
110     return keySet;
111   }
112
113   /**
114    * Maps the specified key to the specified value in this JSONObject. If the
115    * specified key already has an associated value, it is overwritten.
116    *
117    * @param key the key to associate with the specified value
118    * @param jsonValue the value to assoociate with this key
119    * @return if one existed, the previous value associated with the key, or
120    * <code>null</code> otherwise
121    */

122   public native JSONValue put(String JavaDoc key, JSONValue jsonValue) /*-{
123     var out = // can't break next line
124       this.@com.google.gwt.json.client.JSONObject::get(Ljava/lang/String;)(key);
125     this.@com.google.gwt.json.client.JSONObject::frontStore[key] = jsonValue;
126     return out;
127   }-*/
;
128
129   /**
130    * Determines the number of keys on this object.
131    */

132   public int size() {
133     return keySet().size();
134   }
135
136   /**
137    * Converts a JSONObject into a JSON representation that can be used to
138    * communicate with a JSON service.
139    *
140    * @return a JSON string representation of this JSONObject instance
141    */

142   public native String JavaDoc toString() /*-{
143     for (var x in this.@com.google.gwt.json.client.JSONObject::backStore) {
144       // we are wrapping everything in backStore so that frontStore is canonical
145       this.@com.google.gwt.json.client.JSONObject::get(Ljava/lang/String;)(x);
146     }
147     var out = [];
148     out.push("{");
149     var first = true;
150     for (var x in this.@com.google.gwt.json.client.JSONObject::frontStore) {
151       if(first) {
152         first = false;
153       } else {
154         out.push(", ");
155       }
156       var subObj =
157         (this.@com.google.gwt.json.client.JSONObject::frontStore[x]).
158           @com.google.gwt.json.client.JSONValue::toString()();
159       out.push("\"");
160       out.push(x);
161       out.push("\":");
162       out.push(subObj);
163     }
164     out.push("}")
165     return out.join("");
166   }-*/
;
167
168   private native void addAllKeysFromJavascriptObject(Set JavaDoc s,
169       JavaScriptObject javaScriptObject) /*-{
170     for(var key in javaScriptObject) {
171      s.@java.util.Set::add(Ljava/lang/Object;)(key);
172     }
173   }-*/
;
174
175   private native JavaScriptObject createBlankObject() /*-{
176     return {};
177   }-*/
;
178 }
179
Popular Tags