annotate lib/org.json_2.0/src/org/json/JSONObject.java @ 0:db87c1b7eb6d

initial
author dwinter
date Wed, 03 Nov 2010 12:18:46 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1 package org.json;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
2
db87c1b7eb6d initial
dwinter
parents:
diff changeset
3 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
4 Copyright (c) 2002 JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
5
db87c1b7eb6d initial
dwinter
parents:
diff changeset
6 Permission is hereby granted, free of charge, to any person obtaining a copy
db87c1b7eb6d initial
dwinter
parents:
diff changeset
7 of this software and associated documentation files (the "Software"), to deal
db87c1b7eb6d initial
dwinter
parents:
diff changeset
8 in the Software without restriction, including without limitation the rights
db87c1b7eb6d initial
dwinter
parents:
diff changeset
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
db87c1b7eb6d initial
dwinter
parents:
diff changeset
10 copies of the Software, and to permit persons to whom the Software is
db87c1b7eb6d initial
dwinter
parents:
diff changeset
11 furnished to do so, subject to the following conditions:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
12
db87c1b7eb6d initial
dwinter
parents:
diff changeset
13 The above copyright notice and this permission notice shall be included in all
db87c1b7eb6d initial
dwinter
parents:
diff changeset
14 copies or substantial portions of the Software.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
15
db87c1b7eb6d initial
dwinter
parents:
diff changeset
16 The Software shall be used for Good, not Evil.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
17
db87c1b7eb6d initial
dwinter
parents:
diff changeset
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
db87c1b7eb6d initial
dwinter
parents:
diff changeset
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
db87c1b7eb6d initial
dwinter
parents:
diff changeset
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
24 SOFTWARE.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
25 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
26
db87c1b7eb6d initial
dwinter
parents:
diff changeset
27 import java.io.IOException;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
28 import java.io.Writer;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
29 import java.lang.reflect.Field;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
30 import java.lang.reflect.Modifier;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
31 import java.lang.reflect.Method;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
32 import java.util.Collection;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
33 import java.util.HashMap;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
34 import java.util.Iterator;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
35 import java.util.Map;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
36 import java.util.TreeSet;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
37
db87c1b7eb6d initial
dwinter
parents:
diff changeset
38 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
39 * A JSONObject is an unordered collection of name/value pairs. Its
db87c1b7eb6d initial
dwinter
parents:
diff changeset
40 * external form is a string wrapped in curly braces with colons between the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
41 * names and values, and commas between the values and names. The internal form
db87c1b7eb6d initial
dwinter
parents:
diff changeset
42 * is an object having <code>get</code> and <code>opt</code> methods for
db87c1b7eb6d initial
dwinter
parents:
diff changeset
43 * accessing the values by name, and <code>put</code> methods for adding or
db87c1b7eb6d initial
dwinter
parents:
diff changeset
44 * replacing values by name. The values can be any of these types:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
45 * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
46 * <code>Number</code>, <code>String</code>, or the <code>JSONObject.NULL</code>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
47 * object. A JSONObject constructor can be used to convert an external form
db87c1b7eb6d initial
dwinter
parents:
diff changeset
48 * JSON text into an internal form whose values can be retrieved with the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
49 * <code>get</code> and <code>opt</code> methods, or to convert values into a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
50 * JSON text using the <code>put</code> and <code>toString</code> methods.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
51 * A <code>get</code> method returns a value if one can be found, and throws an
db87c1b7eb6d initial
dwinter
parents:
diff changeset
52 * exception if one cannot be found. An <code>opt</code> method returns a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
53 * default value instead of throwing an exception, and so is useful for
db87c1b7eb6d initial
dwinter
parents:
diff changeset
54 * obtaining optional values.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
55 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
56 * The generic <code>get()</code> and <code>opt()</code> methods return an
db87c1b7eb6d initial
dwinter
parents:
diff changeset
57 * object, which you can cast or query for type. There are also typed
db87c1b7eb6d initial
dwinter
parents:
diff changeset
58 * <code>get</code> and <code>opt</code> methods that do type checking and type
db87c1b7eb6d initial
dwinter
parents:
diff changeset
59 * coercion for you.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
60 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
61 * The <code>put</code> methods adds values to an object. For example, <pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
62 * myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
63 * produces the string <code>{"JSON": "Hello, World"}</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
64 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
65 * The texts produced by the <code>toString</code> methods strictly conform to
db87c1b7eb6d initial
dwinter
parents:
diff changeset
66 * the JSON syntax rules.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
67 * The constructors are more forgiving in the texts they will accept:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
68 * <ul>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
69 * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
db87c1b7eb6d initial
dwinter
parents:
diff changeset
70 * before the closing brace.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
71 * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
db87c1b7eb6d initial
dwinter
parents:
diff changeset
72 * quote)</small>.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
73 * <li>Strings do not need to be quoted at all if they do not begin with a quote
db87c1b7eb6d initial
dwinter
parents:
diff changeset
74 * or single quote, and if they do not contain leading or trailing spaces,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
75 * and if they do not contain any of these characters:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
76 * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
db87c1b7eb6d initial
dwinter
parents:
diff changeset
77 * and if they are not the reserved words <code>true</code>,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
78 * <code>false</code>, or <code>null</code>.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
79 * <li>Keys can be followed by <code>=</code> or <code>=></code> as well as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
80 * by <code>:</code>.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
81 * <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
82 * well as by <code>,</code> <small>(comma)</small>.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
83 * <li>Numbers may have the <code>0-</code> <small>(octal)</small> or
db87c1b7eb6d initial
dwinter
parents:
diff changeset
84 * <code>0x-</code> <small>(hex)</small> prefix.</li>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
85 * </ul>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
86 * @author JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
87 * @version 2009-03-06
db87c1b7eb6d initial
dwinter
parents:
diff changeset
88 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
89 public class JSONObject {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
90
db87c1b7eb6d initial
dwinter
parents:
diff changeset
91 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
92 * JSONObject.NULL is equivalent to the value that JavaScript calls null,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
93 * whilst Java's null is equivalent to the value that JavaScript calls
db87c1b7eb6d initial
dwinter
parents:
diff changeset
94 * undefined.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
95 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
96 private static final class Null {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
97
db87c1b7eb6d initial
dwinter
parents:
diff changeset
98 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
99 * There is only intended to be a single instance of the NULL object,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
100 * so the clone method returns itself.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
101 * @return NULL.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
102 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
103 protected final Object clone() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
104 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
105 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
106
db87c1b7eb6d initial
dwinter
parents:
diff changeset
107
db87c1b7eb6d initial
dwinter
parents:
diff changeset
108 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
109 * A Null object is equal to the null value and to itself.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
110 * @param object An object to test for nullness.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
111 * @return true if the object parameter is the JSONObject.NULL object
db87c1b7eb6d initial
dwinter
parents:
diff changeset
112 * or null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
113 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
114 public boolean equals(Object object) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
115 return object == null || object == this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
116 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
117
db87c1b7eb6d initial
dwinter
parents:
diff changeset
118
db87c1b7eb6d initial
dwinter
parents:
diff changeset
119 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
120 * Get the "null" string value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
121 * @return The string "null".
db87c1b7eb6d initial
dwinter
parents:
diff changeset
122 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
123 public String toString() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
124 return "null";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
125 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
126 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
127
db87c1b7eb6d initial
dwinter
parents:
diff changeset
128
db87c1b7eb6d initial
dwinter
parents:
diff changeset
129 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
130 * The map where the JSONObject's properties are kept.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
131 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
132 private Map map;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
133
db87c1b7eb6d initial
dwinter
parents:
diff changeset
134
db87c1b7eb6d initial
dwinter
parents:
diff changeset
135 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
136 * It is sometimes more convenient and less ambiguous to have a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
137 * <code>NULL</code> object than to use Java's <code>null</code> value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
138 * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
139 * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
140 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
141 public static final Object NULL = new Null();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
142
db87c1b7eb6d initial
dwinter
parents:
diff changeset
143
db87c1b7eb6d initial
dwinter
parents:
diff changeset
144 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
145 * Construct an empty JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
146 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
147 public JSONObject() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
148 this.map = new HashMap();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
149 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
150
db87c1b7eb6d initial
dwinter
parents:
diff changeset
151
db87c1b7eb6d initial
dwinter
parents:
diff changeset
152 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
153 * Construct a JSONObject from a subset of another JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
154 * An array of strings is used to identify the keys that should be copied.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
155 * Missing keys are ignored.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
156 * @param jo A JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
157 * @param names An array of strings.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
158 * @exception JSONException If a value is a non-finite number or if a name is duplicated.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
159 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
160 public JSONObject(JSONObject jo, String[] names) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
161 this();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
162 for (int i = 0; i < names.length; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
163 putOnce(names[i], jo.opt(names[i]));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
164 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
165 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
166
db87c1b7eb6d initial
dwinter
parents:
diff changeset
167
db87c1b7eb6d initial
dwinter
parents:
diff changeset
168 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
169 * Construct a JSONObject from a JSONTokener.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
170 * @param x A JSONTokener object containing the source string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
171 * @throws JSONException If there is a syntax error in the source string
db87c1b7eb6d initial
dwinter
parents:
diff changeset
172 * or a duplicated key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
173 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
174 public JSONObject(JSONTokener x) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
175 this();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
176 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
177 String key;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
178
db87c1b7eb6d initial
dwinter
parents:
diff changeset
179 if (x.nextClean() != '{') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
180 throw x.syntaxError("A JSONObject text must begin with '{'");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
181 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
182 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
183 c = x.nextClean();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
184 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
185 case 0:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
186 throw x.syntaxError("A JSONObject text must end with '}'");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
187 case '}':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
188 return;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
189 default:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
190 x.back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
191 key = x.nextValue().toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
192 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
193
db87c1b7eb6d initial
dwinter
parents:
diff changeset
194 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
195 * The key is followed by ':'. We will also tolerate '=' or '=>'.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
196 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
197
db87c1b7eb6d initial
dwinter
parents:
diff changeset
198 c = x.nextClean();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
199 if (c == '=') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
200 if (x.next() != '>') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
201 x.back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
202 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
203 } else if (c != ':') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
204 throw x.syntaxError("Expected a ':' after a key");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
205 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
206 putOnce(key, x.nextValue());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
207
db87c1b7eb6d initial
dwinter
parents:
diff changeset
208 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
209 * Pairs are separated by ','. We will also tolerate ';'.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
210 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
211
db87c1b7eb6d initial
dwinter
parents:
diff changeset
212 switch (x.nextClean()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
213 case ';':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
214 case ',':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
215 if (x.nextClean() == '}') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
216 return;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
217 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
218 x.back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
219 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
220 case '}':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
221 return;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
222 default:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
223 throw x.syntaxError("Expected a ',' or '}'");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
224 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
225 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
226 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
227
db87c1b7eb6d initial
dwinter
parents:
diff changeset
228
db87c1b7eb6d initial
dwinter
parents:
diff changeset
229 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
230 * Construct a JSONObject from a Map.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
231 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
232 * @param map A map object that can be used to initialize the contents of
db87c1b7eb6d initial
dwinter
parents:
diff changeset
233 * the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
234 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
235 public JSONObject(Map map) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
236 this.map = (map == null) ? new HashMap() : map;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
237 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
238
db87c1b7eb6d initial
dwinter
parents:
diff changeset
239
db87c1b7eb6d initial
dwinter
parents:
diff changeset
240 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
241 * Construct a JSONObject from a Map.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
242 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
243 * Note: Use this constructor when the map contains <key,bean>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
244 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
245 * @param map - A map with Key-Bean data.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
246 * @param includeSuperClass - Tell whether to include the super class properties.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
247 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
248 public JSONObject(Map map, boolean includeSuperClass) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
249 this.map = new HashMap();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
250 if (map != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
251 Iterator i = map.entrySet().iterator();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
252 while (i.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
253 Map.Entry e = (Map.Entry)i.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
254 if (isStandardProperty(e.getValue().getClass())) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
255 this.map.put(e.getKey(), e.getValue());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
256 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
257 this.map.put(e.getKey(), new JSONObject(e.getValue(),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
258 includeSuperClass));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
259 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
260 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
261 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
262 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
263
db87c1b7eb6d initial
dwinter
parents:
diff changeset
264
db87c1b7eb6d initial
dwinter
parents:
diff changeset
265 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
266 * Construct a JSONObject from an Object using bean getters.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
267 * It reflects on all of the public methods of the object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
268 * For each of the methods with no parameters and a name starting
db87c1b7eb6d initial
dwinter
parents:
diff changeset
269 * with <code>"get"</code> or <code>"is"</code> followed by an uppercase letter,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
270 * the method is invoked, and a key and the value returned from the getter method
db87c1b7eb6d initial
dwinter
parents:
diff changeset
271 * are put into the new JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
272 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
273 * The key is formed by removing the <code>"get"</code> or <code>"is"</code> prefix.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
274 * If the second remaining character is not upper case, then the first
db87c1b7eb6d initial
dwinter
parents:
diff changeset
275 * character is converted to lower case.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
276 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
277 * For example, if an object has a method named <code>"getName"</code>, and
db87c1b7eb6d initial
dwinter
parents:
diff changeset
278 * if the result of calling <code>object.getName()</code> is <code>"Larry Fine"</code>,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
279 * then the JSONObject will contain <code>"name": "Larry Fine"</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
280 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
281 * @param bean An object that has getter methods that should be used
db87c1b7eb6d initial
dwinter
parents:
diff changeset
282 * to make a JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
283 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
284 public JSONObject(Object bean) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
285 this();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
286 populateInternalMap(bean, false);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
287 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
288
db87c1b7eb6d initial
dwinter
parents:
diff changeset
289
db87c1b7eb6d initial
dwinter
parents:
diff changeset
290 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
291 * Construct a JSONObject from an Object using bean getters.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
292 * It reflects on all of the public methods of the object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
293 * For each of the methods with no parameters and a name starting
db87c1b7eb6d initial
dwinter
parents:
diff changeset
294 * with <code>"get"</code> or <code>"is"</code> followed by an uppercase letter,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
295 * the method is invoked, and a key and the value returned from the getter method
db87c1b7eb6d initial
dwinter
parents:
diff changeset
296 * are put into the new JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
297 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
298 * The key is formed by removing the <code>"get"</code> or <code>"is"</code> prefix.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
299 * If the second remaining character is not upper case, then the first
db87c1b7eb6d initial
dwinter
parents:
diff changeset
300 * character is converted to lower case.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
301 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
302 * @param bean An object that has getter methods that should be used
db87c1b7eb6d initial
dwinter
parents:
diff changeset
303 * to make a JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
304 * @param includeSuperClass If true, include the super class properties.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
305 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
306 public JSONObject(Object bean, boolean includeSuperClass) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
307 this();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
308 populateInternalMap(bean, includeSuperClass);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
309 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
310
db87c1b7eb6d initial
dwinter
parents:
diff changeset
311 private void populateInternalMap(Object bean, boolean includeSuperClass){
db87c1b7eb6d initial
dwinter
parents:
diff changeset
312 Class klass = bean.getClass();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
313
db87c1b7eb6d initial
dwinter
parents:
diff changeset
314 /* If klass.getSuperClass is System class then force includeSuperClass to false. */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
315
db87c1b7eb6d initial
dwinter
parents:
diff changeset
316 if (klass.getClassLoader() == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
317 includeSuperClass = false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
318 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
319
db87c1b7eb6d initial
dwinter
parents:
diff changeset
320 Method[] methods = (includeSuperClass) ?
db87c1b7eb6d initial
dwinter
parents:
diff changeset
321 klass.getMethods() : klass.getDeclaredMethods();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
322 for (int i = 0; i < methods.length; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
323 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
324 Method method = methods[i];
db87c1b7eb6d initial
dwinter
parents:
diff changeset
325 if (Modifier.isPublic(method.getModifiers())) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
326 String name = method.getName();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
327 String key = "";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
328 if (name.startsWith("get")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
329 key = name.substring(3);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
330 } else if (name.startsWith("is")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
331 key = name.substring(2);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
332 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
333 if (key.length() > 0 &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
334 Character.isUpperCase(key.charAt(0)) &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
335 method.getParameterTypes().length == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
336 if (key.length() == 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
337 key = key.toLowerCase();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
338 } else if (!Character.isUpperCase(key.charAt(1))) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
339 key = key.substring(0, 1).toLowerCase() +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
340 key.substring(1);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
341 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
342
db87c1b7eb6d initial
dwinter
parents:
diff changeset
343 Object result = method.invoke(bean, (Object[])null);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
344 if (result == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
345 map.put(key, NULL);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
346 } else if (result.getClass().isArray()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
347 map.put(key, new JSONArray(result, includeSuperClass));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
348 } else if (result instanceof Collection) { // List or Set
db87c1b7eb6d initial
dwinter
parents:
diff changeset
349 map.put(key, new JSONArray((Collection)result, includeSuperClass));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
350 } else if (result instanceof Map) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
351 map.put(key, new JSONObject((Map)result, includeSuperClass));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
352 } else if (isStandardProperty(result.getClass())) { // Primitives, String and Wrapper
db87c1b7eb6d initial
dwinter
parents:
diff changeset
353 map.put(key, result);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
354 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
355 if (result.getClass().getPackage().getName().startsWith("java") ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
356 result.getClass().getClassLoader() == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
357 map.put(key, result.toString());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
358 } else { // User defined Objects
db87c1b7eb6d initial
dwinter
parents:
diff changeset
359 map.put(key, new JSONObject(result, includeSuperClass));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
360 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
361 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
362 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
363 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
364 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
365 throw new RuntimeException(e);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
366 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
367 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
368 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
369
db87c1b7eb6d initial
dwinter
parents:
diff changeset
370
db87c1b7eb6d initial
dwinter
parents:
diff changeset
371 static boolean isStandardProperty(Class clazz) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
372 return clazz.isPrimitive() ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
373 clazz.isAssignableFrom(Byte.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
374 clazz.isAssignableFrom(Short.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
375 clazz.isAssignableFrom(Integer.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
376 clazz.isAssignableFrom(Long.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
377 clazz.isAssignableFrom(Float.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
378 clazz.isAssignableFrom(Double.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
379 clazz.isAssignableFrom(Character.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
380 clazz.isAssignableFrom(String.class) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
381 clazz.isAssignableFrom(Boolean.class);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
382 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
383
db87c1b7eb6d initial
dwinter
parents:
diff changeset
384
db87c1b7eb6d initial
dwinter
parents:
diff changeset
385 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
386 * Construct a JSONObject from an Object, using reflection to find the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
387 * public members. The resulting JSONObject's keys will be the strings
db87c1b7eb6d initial
dwinter
parents:
diff changeset
388 * from the names array, and the values will be the field values associated
db87c1b7eb6d initial
dwinter
parents:
diff changeset
389 * with those keys in the object. If a key is not found or not visible,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
390 * then it will not be copied into the new JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
391 * @param object An object that has fields that should be used to make a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
392 * JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
393 * @param names An array of strings, the names of the fields to be obtained
db87c1b7eb6d initial
dwinter
parents:
diff changeset
394 * from the object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
395 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
396 public JSONObject(Object object, String names[]) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
397 this();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
398 Class c = object.getClass();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
399 for (int i = 0; i < names.length; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
400 String name = names[i];
db87c1b7eb6d initial
dwinter
parents:
diff changeset
401 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
402 putOpt(name, c.getField(name).get(object));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
403 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
404 /* forget about it */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
405 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
406 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
407 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
408
db87c1b7eb6d initial
dwinter
parents:
diff changeset
409
db87c1b7eb6d initial
dwinter
parents:
diff changeset
410 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
411 * Construct a JSONObject from a source JSON text string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
412 * This is the most commonly used JSONObject constructor.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
413 * @param source A string beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
414 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
415 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
416 * @exception JSONException If there is a syntax error in the source
db87c1b7eb6d initial
dwinter
parents:
diff changeset
417 * string or a duplicated key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
418 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
419 public JSONObject(String source) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
420 this(new JSONTokener(source));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
421 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
422
db87c1b7eb6d initial
dwinter
parents:
diff changeset
423
db87c1b7eb6d initial
dwinter
parents:
diff changeset
424 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
425 * Accumulate values under a key. It is similar to the put method except
db87c1b7eb6d initial
dwinter
parents:
diff changeset
426 * that if there is already an object stored under the key then a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
427 * JSONArray is stored under the key to hold all of the accumulated values.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
428 * If there is already a JSONArray, then the new value is appended to it.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
429 * In contrast, the put method replaces the previous value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
430 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
431 * @param value An object to be accumulated under the key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
432 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
433 * @throws JSONException If the value is an invalid number
db87c1b7eb6d initial
dwinter
parents:
diff changeset
434 * or if the key is null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
435 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
436 public JSONObject accumulate(String key, Object value)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
437 throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
438 testValidity(value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
439 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
440 if (o == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
441 put(key, value instanceof JSONArray ?
db87c1b7eb6d initial
dwinter
parents:
diff changeset
442 new JSONArray().put(value) :
db87c1b7eb6d initial
dwinter
parents:
diff changeset
443 value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
444 } else if (o instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
445 ((JSONArray)o).put(value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
446 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
447 put(key, new JSONArray().put(o).put(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
448 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
449 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
450 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
451
db87c1b7eb6d initial
dwinter
parents:
diff changeset
452
db87c1b7eb6d initial
dwinter
parents:
diff changeset
453 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
454 * Append values to the array under a key. If the key does not exist in the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
455 * JSONObject, then the key is put in the JSONObject with its value being a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
456 * JSONArray containing the value parameter. If the key was already
db87c1b7eb6d initial
dwinter
parents:
diff changeset
457 * associated with a JSONArray, then the value parameter is appended to it.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
458 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
459 * @param value An object to be accumulated under the key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
460 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
461 * @throws JSONException If the key is null or if the current value
db87c1b7eb6d initial
dwinter
parents:
diff changeset
462 * associated with the key is not a JSONArray.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
463 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
464 public JSONObject append(String key, Object value)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
465 throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
466 testValidity(value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
467 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
468 if (o == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
469 put(key, new JSONArray().put(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
470 } else if (o instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
471 put(key, ((JSONArray)o).put(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
472 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
473 throw new JSONException("JSONObject[" + key +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
474 "] is not a JSONArray.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
475 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
476 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
477 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
478
db87c1b7eb6d initial
dwinter
parents:
diff changeset
479
db87c1b7eb6d initial
dwinter
parents:
diff changeset
480 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
481 * Produce a string from a double. The string "null" will be returned if
db87c1b7eb6d initial
dwinter
parents:
diff changeset
482 * the number is not finite.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
483 * @param d A double.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
484 * @return A String.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
485 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
486 static public String doubleToString(double d) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
487 if (Double.isInfinite(d) || Double.isNaN(d)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
488 return "null";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
489 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
490
db87c1b7eb6d initial
dwinter
parents:
diff changeset
491 // Shave off trailing zeros and decimal point, if possible.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
492
db87c1b7eb6d initial
dwinter
parents:
diff changeset
493 String s = Double.toString(d);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
494 if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
495 while (s.endsWith("0")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
496 s = s.substring(0, s.length() - 1);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
497 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
498 if (s.endsWith(".")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
499 s = s.substring(0, s.length() - 1);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
500 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
501 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
502 return s;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
503 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
504
db87c1b7eb6d initial
dwinter
parents:
diff changeset
505
db87c1b7eb6d initial
dwinter
parents:
diff changeset
506 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
507 * Get the value object associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
508 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
509 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
510 * @return The object associated with the key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
511 * @throws JSONException if the key is not found.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
512 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
513 public Object get(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
514 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
515 if (o == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
516 throw new JSONException("JSONObject[" + quote(key) +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
517 "] not found.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
518 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
519 return o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
520 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
521
db87c1b7eb6d initial
dwinter
parents:
diff changeset
522
db87c1b7eb6d initial
dwinter
parents:
diff changeset
523 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
524 * Get the boolean value associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
525 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
526 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
527 * @return The truth.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
528 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
529 * if the value is not a Boolean or the String "true" or "false".
db87c1b7eb6d initial
dwinter
parents:
diff changeset
530 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
531 public boolean getBoolean(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
532 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
533 if (o.equals(Boolean.FALSE) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
534 (o instanceof String &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
535 ((String)o).equalsIgnoreCase("false"))) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
536 return false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
537 } else if (o.equals(Boolean.TRUE) ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
538 (o instanceof String &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
539 ((String)o).equalsIgnoreCase("true"))) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
540 return true;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
541 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
542 throw new JSONException("JSONObject[" + quote(key) +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
543 "] is not a Boolean.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
544 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
545
db87c1b7eb6d initial
dwinter
parents:
diff changeset
546
db87c1b7eb6d initial
dwinter
parents:
diff changeset
547 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
548 * Get the double value associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
549 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
550 * @return The numeric value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
551 * @throws JSONException if the key is not found or
db87c1b7eb6d initial
dwinter
parents:
diff changeset
552 * if the value is not a Number object and cannot be converted to a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
553 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
554 public double getDouble(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
555 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
556 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
557 return o instanceof Number ?
db87c1b7eb6d initial
dwinter
parents:
diff changeset
558 ((Number)o).doubleValue() :
db87c1b7eb6d initial
dwinter
parents:
diff changeset
559 Double.valueOf((String)o).doubleValue();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
560 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
561 throw new JSONException("JSONObject[" + quote(key) +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
562 "] is not a number.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
563 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
564 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
565
db87c1b7eb6d initial
dwinter
parents:
diff changeset
566
db87c1b7eb6d initial
dwinter
parents:
diff changeset
567 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
568 * Get the int value associated with a key. If the number value is too
db87c1b7eb6d initial
dwinter
parents:
diff changeset
569 * large for an int, it will be clipped.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
570 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
571 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
572 * @return The integer value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
573 * @throws JSONException if the key is not found or if the value cannot
db87c1b7eb6d initial
dwinter
parents:
diff changeset
574 * be converted to an integer.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
575 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
576 public int getInt(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
577 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
578 return o instanceof Number ?
db87c1b7eb6d initial
dwinter
parents:
diff changeset
579 ((Number)o).intValue() : (int)getDouble(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
580 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
581
db87c1b7eb6d initial
dwinter
parents:
diff changeset
582
db87c1b7eb6d initial
dwinter
parents:
diff changeset
583 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
584 * Get the JSONArray value associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
585 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
586 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
587 * @return A JSONArray which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
588 * @throws JSONException if the key is not found or
db87c1b7eb6d initial
dwinter
parents:
diff changeset
589 * if the value is not a JSONArray.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
590 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
591 public JSONArray getJSONArray(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
592 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
593 if (o instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
594 return (JSONArray)o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
595 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
596 throw new JSONException("JSONObject[" + quote(key) +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
597 "] is not a JSONArray.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
598 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
599
db87c1b7eb6d initial
dwinter
parents:
diff changeset
600
db87c1b7eb6d initial
dwinter
parents:
diff changeset
601 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
602 * Get the JSONObject value associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
603 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
604 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
605 * @return A JSONObject which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
606 * @throws JSONException if the key is not found or
db87c1b7eb6d initial
dwinter
parents:
diff changeset
607 * if the value is not a JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
608 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
609 public JSONObject getJSONObject(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
610 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
611 if (o instanceof JSONObject) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
612 return (JSONObject)o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
613 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
614 throw new JSONException("JSONObject[" + quote(key) +
db87c1b7eb6d initial
dwinter
parents:
diff changeset
615 "] is not a JSONObject.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
616 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
617
db87c1b7eb6d initial
dwinter
parents:
diff changeset
618
db87c1b7eb6d initial
dwinter
parents:
diff changeset
619 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
620 * Get the long value associated with a key. If the number value is too
db87c1b7eb6d initial
dwinter
parents:
diff changeset
621 * long for a long, it will be clipped.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
622 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
623 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
624 * @return The long value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
625 * @throws JSONException if the key is not found or if the value cannot
db87c1b7eb6d initial
dwinter
parents:
diff changeset
626 * be converted to a long.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
627 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
628 public long getLong(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
629 Object o = get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
630 return o instanceof Number ?
db87c1b7eb6d initial
dwinter
parents:
diff changeset
631 ((Number)o).longValue() : (long)getDouble(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
632 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
633
db87c1b7eb6d initial
dwinter
parents:
diff changeset
634
db87c1b7eb6d initial
dwinter
parents:
diff changeset
635 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
636 * Get an array of field names from a JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
637 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
638 * @return An array of field names, or null if there are no names.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
639 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
640 public static String[] getNames(JSONObject jo) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
641 int length = jo.length();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
642 if (length == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
643 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
644 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
645 Iterator i = jo.keys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
646 String[] names = new String[length];
db87c1b7eb6d initial
dwinter
parents:
diff changeset
647 int j = 0;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
648 while (i.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
649 names[j] = (String)i.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
650 j += 1;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
651 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
652 return names;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
653 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
654
db87c1b7eb6d initial
dwinter
parents:
diff changeset
655
db87c1b7eb6d initial
dwinter
parents:
diff changeset
656 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
657 * Get an array of field names from an Object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
658 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
659 * @return An array of field names, or null if there are no names.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
660 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
661 public static String[] getNames(Object object) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
662 if (object == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
663 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
664 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
665 Class klass = object.getClass();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
666 Field[] fields = klass.getFields();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
667 int length = fields.length;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
668 if (length == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
669 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
670 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
671 String[] names = new String[length];
db87c1b7eb6d initial
dwinter
parents:
diff changeset
672 for (int i = 0; i < length; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
673 names[i] = fields[i].getName();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
674 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
675 return names;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
676 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
677
db87c1b7eb6d initial
dwinter
parents:
diff changeset
678
db87c1b7eb6d initial
dwinter
parents:
diff changeset
679 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
680 * Get the string associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
681 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
682 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
683 * @return A string which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
684 * @throws JSONException if the key is not found.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
685 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
686 public String getString(String key) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
687 return get(key).toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
688 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
689
db87c1b7eb6d initial
dwinter
parents:
diff changeset
690
db87c1b7eb6d initial
dwinter
parents:
diff changeset
691 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
692 * Determine if the JSONObject contains a specific key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
693 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
694 * @return true if the key exists in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
695 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
696 public boolean has(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
697 return this.map.containsKey(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
698 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
699
db87c1b7eb6d initial
dwinter
parents:
diff changeset
700
db87c1b7eb6d initial
dwinter
parents:
diff changeset
701 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
702 * Determine if the value associated with the key is null or if there is
db87c1b7eb6d initial
dwinter
parents:
diff changeset
703 * no value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
704 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
705 * @return true if there is no value associated with the key or if
db87c1b7eb6d initial
dwinter
parents:
diff changeset
706 * the value is the JSONObject.NULL object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
707 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
708 public boolean isNull(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
709 return JSONObject.NULL.equals(opt(key));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
710 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
711
db87c1b7eb6d initial
dwinter
parents:
diff changeset
712
db87c1b7eb6d initial
dwinter
parents:
diff changeset
713 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
714 * Get an enumeration of the keys of the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
715 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
716 * @return An iterator of the keys.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
717 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
718 public Iterator keys() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
719 return this.map.keySet().iterator();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
720 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
721
db87c1b7eb6d initial
dwinter
parents:
diff changeset
722
db87c1b7eb6d initial
dwinter
parents:
diff changeset
723 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
724 * Get the number of keys stored in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
725 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
726 * @return The number of keys in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
727 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
728 public int length() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
729 return this.map.size();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
730 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
731
db87c1b7eb6d initial
dwinter
parents:
diff changeset
732
db87c1b7eb6d initial
dwinter
parents:
diff changeset
733 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
734 * Produce a JSONArray containing the names of the elements of this
db87c1b7eb6d initial
dwinter
parents:
diff changeset
735 * JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
736 * @return A JSONArray containing the key strings, or null if the JSONObject
db87c1b7eb6d initial
dwinter
parents:
diff changeset
737 * is empty.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
738 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
739 public JSONArray names() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
740 JSONArray ja = new JSONArray();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
741 Iterator keys = keys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
742 while (keys.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
743 ja.put(keys.next());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
744 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
745 return ja.length() == 0 ? null : ja;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
746 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
747
db87c1b7eb6d initial
dwinter
parents:
diff changeset
748 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
749 * Produce a string from a Number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
750 * @param n A Number
db87c1b7eb6d initial
dwinter
parents:
diff changeset
751 * @return A String.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
752 * @throws JSONException If n is a non-finite number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
753 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
754 static public String numberToString(Number n)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
755 throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
756 if (n == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
757 throw new JSONException("Null pointer");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
758 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
759 testValidity(n);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
760
db87c1b7eb6d initial
dwinter
parents:
diff changeset
761 // Shave off trailing zeros and decimal point, if possible.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
762
db87c1b7eb6d initial
dwinter
parents:
diff changeset
763 String s = n.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
764 if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
765 while (s.endsWith("0")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
766 s = s.substring(0, s.length() - 1);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
767 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
768 if (s.endsWith(".")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
769 s = s.substring(0, s.length() - 1);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
770 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
771 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
772 return s;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
773 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
774
db87c1b7eb6d initial
dwinter
parents:
diff changeset
775
db87c1b7eb6d initial
dwinter
parents:
diff changeset
776 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
777 * Get an optional value associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
778 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
779 * @return An object which is the value, or null if there is no value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
780 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
781 public Object opt(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
782 return key == null ? null : this.map.get(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
783 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
784
db87c1b7eb6d initial
dwinter
parents:
diff changeset
785
db87c1b7eb6d initial
dwinter
parents:
diff changeset
786 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
787 * Get an optional boolean associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
788 * It returns false if there is no such key, or if the value is not
db87c1b7eb6d initial
dwinter
parents:
diff changeset
789 * Boolean.TRUE or the String "true".
db87c1b7eb6d initial
dwinter
parents:
diff changeset
790 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
791 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
792 * @return The truth.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
793 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
794 public boolean optBoolean(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
795 return optBoolean(key, false);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
796 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
797
db87c1b7eb6d initial
dwinter
parents:
diff changeset
798
db87c1b7eb6d initial
dwinter
parents:
diff changeset
799 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
800 * Get an optional boolean associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
801 * It returns the defaultValue if there is no such key, or if it is not
db87c1b7eb6d initial
dwinter
parents:
diff changeset
802 * a Boolean or the String "true" or "false" (case insensitive).
db87c1b7eb6d initial
dwinter
parents:
diff changeset
803 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
804 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
805 * @param defaultValue The default.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
806 * @return The truth.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
807 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
808 public boolean optBoolean(String key, boolean defaultValue) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
809 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
810 return getBoolean(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
811 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
812 return defaultValue;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
813 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
814 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
815
db87c1b7eb6d initial
dwinter
parents:
diff changeset
816
db87c1b7eb6d initial
dwinter
parents:
diff changeset
817 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
818 * Put a key/value pair in the JSONObject, where the value will be a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
819 * JSONArray which is produced from a Collection.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
820 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
821 * @param value A Collection value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
822 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
823 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
824 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
825 public JSONObject put(String key, Collection value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
826 put(key, new JSONArray(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
827 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
828 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
829
db87c1b7eb6d initial
dwinter
parents:
diff changeset
830
db87c1b7eb6d initial
dwinter
parents:
diff changeset
831 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
832 * Get an optional double associated with a key,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
833 * or NaN if there is no such key or if its value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
834 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
835 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
836 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
837 * @param key A string which is the key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
838 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
839 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
840 public double optDouble(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
841 return optDouble(key, Double.NaN);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
842 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
843
db87c1b7eb6d initial
dwinter
parents:
diff changeset
844
db87c1b7eb6d initial
dwinter
parents:
diff changeset
845 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
846 * Get an optional double associated with a key, or the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
847 * defaultValue if there is no such key or if its value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
848 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
849 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
850 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
851 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
852 * @param defaultValue The default.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
853 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
854 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
855 public double optDouble(String key, double defaultValue) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
856 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
857 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
858 return o instanceof Number ? ((Number)o).doubleValue() :
db87c1b7eb6d initial
dwinter
parents:
diff changeset
859 new Double((String)o).doubleValue();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
860 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
861 return defaultValue;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
862 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
863 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
864
db87c1b7eb6d initial
dwinter
parents:
diff changeset
865
db87c1b7eb6d initial
dwinter
parents:
diff changeset
866 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
867 * Get an optional int value associated with a key,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
868 * or zero if there is no such key or if the value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
869 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
870 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
871 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
872 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
873 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
874 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
875 public int optInt(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
876 return optInt(key, 0);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
877 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
878
db87c1b7eb6d initial
dwinter
parents:
diff changeset
879
db87c1b7eb6d initial
dwinter
parents:
diff changeset
880 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
881 * Get an optional int value associated with a key,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
882 * or the default if there is no such key or if the value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
883 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
884 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
885 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
886 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
887 * @param defaultValue The default.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
888 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
889 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
890 public int optInt(String key, int defaultValue) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
891 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
892 return getInt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
893 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
894 return defaultValue;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
895 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
896 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
897
db87c1b7eb6d initial
dwinter
parents:
diff changeset
898
db87c1b7eb6d initial
dwinter
parents:
diff changeset
899 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
900 * Get an optional JSONArray associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
901 * It returns null if there is no such key, or if its value is not a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
902 * JSONArray.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
903 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
904 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
905 * @return A JSONArray which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
906 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
907 public JSONArray optJSONArray(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
908 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
909 return o instanceof JSONArray ? (JSONArray)o : null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
910 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
911
db87c1b7eb6d initial
dwinter
parents:
diff changeset
912
db87c1b7eb6d initial
dwinter
parents:
diff changeset
913 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
914 * Get an optional JSONObject associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
915 * It returns null if there is no such key, or if its value is not a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
916 * JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
917 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
918 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
919 * @return A JSONObject which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
920 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
921 public JSONObject optJSONObject(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
922 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
923 return o instanceof JSONObject ? (JSONObject)o : null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
924 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
925
db87c1b7eb6d initial
dwinter
parents:
diff changeset
926
db87c1b7eb6d initial
dwinter
parents:
diff changeset
927 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
928 * Get an optional long value associated with a key,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
929 * or zero if there is no such key or if the value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
930 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
931 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
932 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
933 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
934 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
935 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
936 public long optLong(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
937 return optLong(key, 0);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
938 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
939
db87c1b7eb6d initial
dwinter
parents:
diff changeset
940
db87c1b7eb6d initial
dwinter
parents:
diff changeset
941 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
942 * Get an optional long value associated with a key,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
943 * or the default if there is no such key or if the value is not a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
944 * If the value is a string, an attempt will be made to evaluate it as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
945 * a number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
946 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
947 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
948 * @param defaultValue The default.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
949 * @return An object which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
950 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
951 public long optLong(String key, long defaultValue) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
952 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
953 return getLong(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
954 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
955 return defaultValue;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
956 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
957 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
958
db87c1b7eb6d initial
dwinter
parents:
diff changeset
959
db87c1b7eb6d initial
dwinter
parents:
diff changeset
960 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
961 * Get an optional string associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
962 * It returns an empty string if there is no such key. If the value is not
db87c1b7eb6d initial
dwinter
parents:
diff changeset
963 * a string and is not null, then it is coverted to a string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
964 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
965 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
966 * @return A string which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
967 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
968 public String optString(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
969 return optString(key, "");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
970 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
971
db87c1b7eb6d initial
dwinter
parents:
diff changeset
972
db87c1b7eb6d initial
dwinter
parents:
diff changeset
973 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
974 * Get an optional string associated with a key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
975 * It returns the defaultValue if there is no such key.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
976 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
977 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
978 * @param defaultValue The default.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
979 * @return A string which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
980 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
981 public String optString(String key, String defaultValue) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
982 Object o = opt(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
983 return o != null ? o.toString() : defaultValue;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
984 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
985
db87c1b7eb6d initial
dwinter
parents:
diff changeset
986
db87c1b7eb6d initial
dwinter
parents:
diff changeset
987 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
988 * Put a key/boolean pair in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
989 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
990 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
991 * @param value A boolean which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
992 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
993 * @throws JSONException If the key is null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
994 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
995 public JSONObject put(String key, boolean value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
996 put(key, value ? Boolean.TRUE : Boolean.FALSE);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
997 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
998 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
999
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1000
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1001 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1002 * Put a key/double pair in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1003 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1004 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1005 * @param value A double which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1006 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1007 * @throws JSONException If the key is null or if the number is invalid.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1008 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1009 public JSONObject put(String key, double value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1010 put(key, new Double(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1011 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1012 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1013
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1014
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1015 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1016 * Put a key/int pair in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1017 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1018 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1019 * @param value An int which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1020 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1021 * @throws JSONException If the key is null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1022 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1023 public JSONObject put(String key, int value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1024 put(key, new Integer(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1025 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1026 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1027
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1028
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1029 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1030 * Put a key/long pair in the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1031 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1032 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1033 * @param value A long which is the value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1034 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1035 * @throws JSONException If the key is null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1036 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1037 public JSONObject put(String key, long value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1038 put(key, new Long(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1039 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1040 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1041
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1042
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1043 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1044 * Put a key/value pair in the JSONObject, where the value will be a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1045 * JSONObject which is produced from a Map.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1046 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1047 * @param value A Map value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1048 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1049 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1050 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1051 public JSONObject put(String key, Map value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1052 put(key, new JSONObject(value));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1053 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1054 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1055
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1056
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1057 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1058 * Put a key/value pair in the JSONObject. If the value is null,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1059 * then the key will be removed from the JSONObject if it is present.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1060 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1061 * @param value An object which is the value. It should be of one of these
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1062 * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1063 * or the JSONObject.NULL object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1064 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1065 * @throws JSONException If the value is non-finite number
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1066 * or if the key is null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1067 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1068 public JSONObject put(String key, Object value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1069 if (key == null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1070 throw new JSONException("Null key.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1071 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1072 if (value != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1073 testValidity(value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1074 this.map.put(key, value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1075 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1076 remove(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1077 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1078 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1079 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1080
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1081
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1082 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1083 * Put a key/value pair in the JSONObject, but only if the key and the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1084 * value are both non-null, and only if there is not already a member
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1085 * with that name.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1086 * @param key
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1087 * @param value
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1088 * @return his.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1089 * @throws JSONException if the key is a duplicate
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1090 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1091 public JSONObject putOnce(String key, Object value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1092 if (key != null && value != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1093 if (opt(key) != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1094 throw new JSONException("Duplicate key \"" + key + "\"");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1095 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1096 put(key, value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1097 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1098 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1099 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1100
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1101
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1102 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1103 * Put a key/value pair in the JSONObject, but only if the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1104 * key and the value are both non-null.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1105 * @param key A key string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1106 * @param value An object which is the value. It should be of one of these
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1107 * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1108 * or the JSONObject.NULL object.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1109 * @return this.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1110 * @throws JSONException If the value is a non-finite number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1111 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1112 public JSONObject putOpt(String key, Object value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1113 if (key != null && value != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1114 put(key, value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1115 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1116 return this;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1117 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1118
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1119
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1120 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1121 * Produce a string in double quotes with backslash sequences in all the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1122 * right places. A backslash will be inserted within </, allowing JSON
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1123 * text to be delivered in HTML. In JSON text, a string cannot contain a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1124 * control character or an unescaped quote or backslash.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1125 * @param string A String
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1126 * @return A String correctly formatted for insertion in a JSON text.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1127 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1128 public static String quote(String string) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1129 if (string == null || string.length() == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1130 return "\"\"";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1131 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1132
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1133 char b;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1134 char c = 0;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1135 int i;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1136 int len = string.length();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1137 StringBuffer sb = new StringBuffer(len + 4);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1138 String t;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1139
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1140 sb.append('"');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1141 for (i = 0; i < len; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1142 b = c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1143 c = string.charAt(i);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1144 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1145 case '\\':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1146 case '"':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1147 sb.append('\\');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1148 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1149 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1150 case '/':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1151 if (b == '<') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1152 sb.append('\\');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1153 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1154 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1155 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1156 case '\b':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1157 sb.append("\\b");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1158 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1159 case '\t':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1160 sb.append("\\t");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1161 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1162 case '\n':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1163 sb.append("\\n");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1164 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1165 case '\f':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1166 sb.append("\\f");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1167 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1168 case '\r':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1169 sb.append("\\r");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1170 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1171 default:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1172 if (c < ' ' || (c >= '\u0080' && c < '\u00a0') ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1173 (c >= '\u2000' && c < '\u2100')) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1174 t = "000" + Integer.toHexString(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1175 sb.append("\\u" + t.substring(t.length() - 4));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1176 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1177 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1178 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1179 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1180 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1181 sb.append('"');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1182 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1183 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1184
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1185 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1186 * Remove a name and its value, if present.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1187 * @param key The name to be removed.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1188 * @return The value that was associated with the name,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1189 * or null if there was no value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1190 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1191 public Object remove(String key) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1192 return this.map.remove(key);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1193 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1194
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1195 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1196 * Get an enumeration of the keys of the JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1197 * The keys will be sorted alphabetically.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1198 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1199 * @return An iterator of the keys.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1200 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1201 public Iterator sortedKeys() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1202 return new TreeSet(this.map.keySet()).iterator();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1203 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1204
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1205 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1206 * Try to convert a string into a number, boolean, or null. If the string
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1207 * can't be converted, return the string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1208 * @param s A String.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1209 * @return A simple JSON value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1210 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1211 static public Object stringToValue(String s) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1212 if (s.equals("")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1213 return s;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1214 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1215 if (s.equalsIgnoreCase("true")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1216 return Boolean.TRUE;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1217 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1218 if (s.equalsIgnoreCase("false")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1219 return Boolean.FALSE;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1220 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1221 if (s.equalsIgnoreCase("null")) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1222 return JSONObject.NULL;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1223 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1224
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1225 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1226 * If it might be a number, try converting it. We support the 0- and 0x-
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1227 * conventions. If a number cannot be produced, then the value will just
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1228 * be a string. Note that the 0-, 0x-, plus, and implied string
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1229 * conventions are non-standard. A JSON parser is free to accept
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1230 * non-JSON forms as long as it accepts all correct JSON forms.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1231 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1232
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1233 char b = s.charAt(0);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1234 if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1235 if (b == '0') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1236 if (s.length() > 2 &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1237 (s.charAt(1) == 'x' || s.charAt(1) == 'X')) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1238 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1239 return new Integer(Integer.parseInt(s.substring(2),
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1240 16));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1241 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1242 /* Ignore the error */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1243 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1244 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1245 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1246 return new Integer(Integer.parseInt(s, 8));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1247 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1248 /* Ignore the error */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1249 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1250 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1251 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1252 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1253 if (s.indexOf('.') > -1 || s.indexOf('e') > -1 || s.indexOf('E') > -1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1254 return Double.valueOf(s);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1255 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1256 Long myLong = new Long(s);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1257 if (myLong.longValue() == myLong.intValue()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1258 return new Integer(myLong.intValue());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1259 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1260 return myLong;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1261 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1262 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1263 } catch (Exception f) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1264 /* Ignore the error */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1265 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1266 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1267 return s;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1268 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1269
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1270
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1271 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1272 * Throw an exception if the object is an NaN or infinite number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1273 * @param o The object to test.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1274 * @throws JSONException If o is a non-finite number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1275 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1276 static void testValidity(Object o) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1277 if (o != null) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1278 if (o instanceof Double) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1279 if (((Double)o).isInfinite() || ((Double)o).isNaN()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1280 throw new JSONException(
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1281 "JSON does not allow non-finite numbers.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1282 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1283 } else if (o instanceof Float) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1284 if (((Float)o).isInfinite() || ((Float)o).isNaN()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1285 throw new JSONException(
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1286 "JSON does not allow non-finite numbers.");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1287 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1288 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1289 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1290 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1291
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1292
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1293 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1294 * Produce a JSONArray containing the values of the members of this
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1295 * JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1296 * @param names A JSONArray containing a list of key strings. This
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1297 * determines the sequence of the values in the result.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1298 * @return A JSONArray of values.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1299 * @throws JSONException If any of the values are non-finite numbers.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1300 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1301 public JSONArray toJSONArray(JSONArray names) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1302 if (names == null || names.length() == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1303 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1304 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1305 JSONArray ja = new JSONArray();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1306 for (int i = 0; i < names.length(); i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1307 ja.put(this.opt(names.getString(i)));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1308 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1309 return ja;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1310 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1311
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1312 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1313 * Make a JSON text of this JSONObject. For compactness, no whitespace
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1314 * is added. If this would not result in a syntactically correct JSON text,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1315 * then null will be returned instead.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1316 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1317 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1318 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1319 * @return a printable, displayable, portable, transmittable
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1320 * representation of the object, beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1321 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1322 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1323 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1324 public String toString() {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1325 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1326 Iterator keys = keys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1327 StringBuffer sb = new StringBuffer("{");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1328
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1329 while (keys.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1330 if (sb.length() > 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1331 sb.append(',');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1332 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1333 Object o = keys.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1334 sb.append(quote(o.toString()));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1335 sb.append(':');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1336 sb.append(valueToString(this.map.get(o)));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1337 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1338 sb.append('}');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1339 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1340 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1341 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1342 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1343 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1344
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1345
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1346 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1347 * Make a prettyprinted JSON text of this JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1348 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1349 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1350 * @param indentFactor The number of spaces to add to each level of
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1351 * indentation.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1352 * @return a printable, displayable, portable, transmittable
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1353 * representation of the object, beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1354 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1355 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1356 * @throws JSONException If the object contains an invalid number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1357 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1358 public String toString(int indentFactor) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1359 return toString(indentFactor, 0);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1360 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1361
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1362
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1363 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1364 * Make a prettyprinted JSON text of this JSONObject.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1365 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1366 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1367 * @param indentFactor The number of spaces to add to each level of
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1368 * indentation.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1369 * @param indent The indentation of the top level.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1370 * @return a printable, displayable, transmittable
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1371 * representation of the object, beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1372 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1373 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1374 * @throws JSONException If the object contains an invalid number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1375 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1376 String toString(int indentFactor, int indent) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1377 int j;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1378 int n = length();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1379 if (n == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1380 return "{}";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1381 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1382 Iterator keys = sortedKeys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1383 StringBuffer sb = new StringBuffer("{");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1384 int newindent = indent + indentFactor;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1385 Object o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1386 if (n == 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1387 o = keys.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1388 sb.append(quote(o.toString()));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1389 sb.append(": ");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1390 sb.append(valueToString(this.map.get(o), indentFactor,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1391 indent));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1392 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1393 while (keys.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1394 o = keys.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1395 if (sb.length() > 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1396 sb.append(",\n");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1397 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1398 sb.append('\n');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1399 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1400 for (j = 0; j < newindent; j += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1401 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1402 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1403 sb.append(quote(o.toString()));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1404 sb.append(": ");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1405 sb.append(valueToString(this.map.get(o), indentFactor,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1406 newindent));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1407 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1408 if (sb.length() > 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1409 sb.append('\n');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1410 for (j = 0; j < indent; j += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1411 sb.append(' ');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1412 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1413 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1414 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1415 sb.append('}');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1416 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1417 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1418
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1419
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1420 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1421 * Make a JSON text of an Object value. If the object has an
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1422 * value.toJSONString() method, then that method will be used to produce
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1423 * the JSON text. The method is required to produce a strictly
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1424 * conforming text. If the object does not contain a toJSONString
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1425 * method (which is the most common case), then a text will be
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1426 * produced by other means. If the value is an array or Collection,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1427 * then a JSONArray will be made from it and its toJSONString method
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1428 * will be called. If the value is a MAP, then a JSONObject will be made
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1429 * from it and its toJSONString method will be called. Otherwise, the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1430 * value's toString method will be called, and the result will be quoted.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1431 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1432 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1433 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1434 * @param value The value to be serialized.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1435 * @return a printable, displayable, transmittable
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1436 * representation of the object, beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1437 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1438 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1439 * @throws JSONException If the value is or contains an invalid number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1440 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1441 static String valueToString(Object value) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1442 if (value == null || value.equals(null)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1443 return "null";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1444 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1445 if (value instanceof JSONString) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1446 Object o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1447 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1448 o = ((JSONString)value).toJSONString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1449 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1450 throw new JSONException(e);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1451 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1452 if (o instanceof String) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1453 return (String)o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1454 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1455 throw new JSONException("Bad value from toJSONString: " + o);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1456 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1457 if (value instanceof Number) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1458 return numberToString((Number) value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1459 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1460 if (value instanceof Boolean || value instanceof JSONObject ||
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1461 value instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1462 return value.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1463 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1464 if (value instanceof Map) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1465 return new JSONObject((Map)value).toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1466 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1467 if (value instanceof Collection) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1468 return new JSONArray((Collection)value).toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1469 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1470 if (value.getClass().isArray()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1471 return new JSONArray(value).toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1472 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1473 return quote(value.toString());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1474 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1475
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1476
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1477 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1478 * Make a prettyprinted JSON text of an object value.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1479 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1480 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1481 * @param value The value to be serialized.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1482 * @param indentFactor The number of spaces to add to each level of
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1483 * indentation.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1484 * @param indent The indentation of the top level.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1485 * @return a printable, displayable, transmittable
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1486 * representation of the object, beginning
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1487 * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1488 * with <code>}</code>&nbsp;<small>(right brace)</small>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1489 * @throws JSONException If the object contains an invalid number.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1490 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1491 static String valueToString(Object value, int indentFactor, int indent)
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1492 throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1493 if (value == null || value.equals(null)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1494 return "null";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1495 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1496 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1497 if (value instanceof JSONString) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1498 Object o = ((JSONString)value).toJSONString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1499 if (o instanceof String) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1500 return (String)o;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1501 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1502 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1503 } catch (Exception e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1504 /* forget about it */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1505 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1506 if (value instanceof Number) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1507 return numberToString((Number) value);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1508 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1509 if (value instanceof Boolean) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1510 return value.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1511 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1512 if (value instanceof JSONObject) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1513 return ((JSONObject)value).toString(indentFactor, indent);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1514 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1515 if (value instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1516 return ((JSONArray)value).toString(indentFactor, indent);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1517 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1518 if (value instanceof Map) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1519 return new JSONObject((Map)value).toString(indentFactor, indent);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1520 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1521 if (value instanceof Collection) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1522 return new JSONArray((Collection)value).toString(indentFactor, indent);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1523 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1524 if (value.getClass().isArray()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1525 return new JSONArray(value).toString(indentFactor, indent);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1526 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1527 return quote(value.toString());
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1528 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1529
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1530
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1531 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1532 * Write the contents of the JSONObject as JSON text to a writer.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1533 * For compactness, no whitespace is added.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1534 * <p>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1535 * Warning: This method assumes that the data structure is acyclical.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1536 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1537 * @return The writer.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1538 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1539 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1540 public Writer write(Writer writer) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1541 try {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1542 boolean b = false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1543 Iterator keys = keys();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1544 writer.write('{');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1545
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1546 while (keys.hasNext()) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1547 if (b) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1548 writer.write(',');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1549 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1550 Object k = keys.next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1551 writer.write(quote(k.toString()));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1552 writer.write(':');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1553 Object v = this.map.get(k);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1554 if (v instanceof JSONObject) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1555 ((JSONObject)v).write(writer);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1556 } else if (v instanceof JSONArray) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1557 ((JSONArray)v).write(writer);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1558 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1559 writer.write(valueToString(v));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1560 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1561 b = true;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1562 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1563 writer.write('}');
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1564 return writer;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1565 } catch (IOException e) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1566 throw new JSONException(e);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1567 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1568 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1569 }