001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.json.client;
017:
018: import com.google.gwt.core.client.JavaScriptObject;
019:
020: /**
021: * Represents an array of {@link com.google.gwt.json.client.JSONValue} objects.
022: */
023: public class JSONArray extends JSONValue {
024:
025: final JavaScriptObject javascriptArray;
026:
027: final JavaScriptObject wrappedArray;
028:
029: /**
030: * Creates an empty JSONArray.
031: */
032: public JSONArray() {
033: javascriptArray = createArray();
034: wrappedArray = createArray();
035: }
036:
037: /**
038: * Creates a new JSONArray from the supplied JavaScriptObject representing a
039: * JavaScript array.
040: *
041: * @param arr a JavaScript array
042: */
043: public JSONArray(JavaScriptObject arr) {
044: javascriptArray = arr;
045: wrappedArray = createArray();
046: }
047:
048: /**
049: * Returns the value at the specified index position.
050: *
051: * @param index the index of the array item to retrieve
052: * @return the value at this index, or <code>null</code> if this index is
053: * empty
054: */
055: public JSONValue get(int index) throws JSONException {
056: if (wrappedTest(index)) {
057: return wrappedGet(index);
058: }
059: JSONValue wrapped = null;
060: if (rawTest(index)) {
061: wrapped = JSONParser.buildValue(rawGet(index));
062: rawSet(index, null);
063: }
064: wrappedSet(index, wrapped);
065: return wrapped;
066: }
067:
068: /**
069: * Returns <code>this</code>, as this is a JSONArray.
070: */
071: @Override
072: public JSONArray isArray() {
073: return this ;
074: }
075:
076: /**
077: * Sets the specified index to the given value.
078: *
079: * @param index the index to set
080: * @param jsonValue the value to set
081: * @return the previous value at this index, or <code>null</code> if this
082: * index was empty
083: */
084: public JSONValue set(int index, JSONValue jsonValue) {
085: JSONValue out = get(index);
086: wrappedSet(index, jsonValue);
087: rawSet(index, null);
088: return out;
089: }
090:
091: /**
092: * Returns the number of elements in this array.
093: *
094: * @return size of this array
095: */
096: public native int size() /*-{
097: return this.@com.google.gwt.json.client.JSONArray::javascriptArray.length;
098: }-*/;
099:
100: /**
101: * Create the JSON encoded string representation of this JSONArray instance.
102: * This method may take a long time to execute if the underlying array is
103: * large.
104: */
105: @Override
106: public String toString() throws JSONException {
107: StringBuffer sb = new StringBuffer();
108: sb.append("[");
109: for (int i = 0, c = size(); i < c; i++) {
110: JSONValue value = get(i);
111: sb.append(value.toString());
112:
113: if (i < c - 1) {
114: sb.append(",");
115: }
116: }
117: sb.append("]");
118: return sb.toString();
119: }
120:
121: private native JavaScriptObject createArray() /*-{
122: return [];
123: }-*/;
124:
125: private native JavaScriptObject rawGet(int index) /*-{
126: var x = this.@com.google.gwt.json.client.JSONArray::javascriptArray[index];
127: if (typeof x == 'number' || typeof x == 'string' || typeof x == 'array' || typeof x == 'boolean') {
128: x = (Object(x));
129: }
130: return x;
131: }-*/;
132:
133: private native void rawSet(int index, JavaScriptObject jsObject) /*-{
134: this.@com.google.gwt.json.client.JSONArray::javascriptArray[index] = jsObject;
135: }-*/;
136:
137: private native boolean rawTest(int index) /*-{
138: var x = this.@com.google.gwt.json.client.JSONArray::javascriptArray[index];
139: return x !== undefined;
140: }-*/;
141:
142: private native JSONValue wrappedGet(int index) /*-{
143: return this.@com.google.gwt.json.client.JSONArray::wrappedArray[index];
144: }-*/;
145:
146: private native void wrappedSet(int index, JSONValue jsonValue) /*-{
147: this.@com.google.gwt.json.client.JSONArray::wrappedArray[index] = jsonValue;
148: }-*/;
149:
150: private native boolean wrappedTest(int index) /*-{
151: var x = this.@com.google.gwt.json.client.JSONArray::wrappedArray[index];
152: return x !== undefined;
153: }-*/;
154: }
|