1 16 package com.google.gwt.json.client; 17 18 import com.google.gwt.core.client.JavaScriptObject; 19 20 23 public class JSONArray extends JSONValue { 24 25 final JavaScriptObject javascriptArray; 26 27 final JavaScriptObject wrappedArray; 28 29 32 public JSONArray() { 33 javascriptArray = createArray(); 34 wrappedArray = createArray(); 35 } 36 37 43 public JSONArray(JavaScriptObject arr) { 44 javascriptArray = arr; 45 wrappedArray = createArray(); 46 } 47 48 55 public JSONValue get(int index) throws JSONException { 56 if (wrappedTest(index)) { 57 return wrappedGet(index); 58 } 59 JSONValue wrapped = null; 60 if (rawTest(index)) { 61 wrapped = JSONParser.buildValue(rawGet(index)); 62 rawSet(index, null); 63 } 64 wrappedSet(index, wrapped); 65 return wrapped; 66 } 67 68 71 public JSONArray isArray() { 72 return this; 73 } 74 75 83 public JSONValue set(int index, JSONValue jsonValue) { 84 JSONValue out = get(index); 85 wrappedSet(index, jsonValue); 86 rawSet(index, null); 87 return out; 88 } 89 90 95 public native int size() ; 98 99 104 public String toString() throws JSONException { 105 StringBuffer sb = new StringBuffer (); 106 sb.append("["); 107 for (int i = 0, c = size(); i < c; i++) { 108 JSONValue value = get(i); 109 sb.append(value.toString()); 110 111 if (i < c - 1) { 112 sb.append(","); 113 } 114 } 115 sb.append("]"); 116 return sb.toString(); 117 } 118 119 private native JavaScriptObject createArray() ; 122 123 private native JavaScriptObject rawGet(int index) ; 130 131 private native void rawSet(int index, JavaScriptObject jsObject) ; 134 135 private native boolean rawTest(int index) ; 139 140 private native JSONValue wrappedGet(int index) ; 143 144 private native void wrappedSet(int index, JSONValue jsonValue) ; 147 148 private native boolean wrappedTest(int index) ; 152 } 153 | Popular Tags |