3 import java.io.IOException;
4 import java.math.BigDecimal;
5 import java.util.Collection;
9 Copyright (c) 2006 JSON.org
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
21 The Software shall be used for Good, not Evil.
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * JSONWriter provides a quick and convenient way of producing JSON text. The
34 * texts produced strictly conform to JSON syntax rules. No whitespace is added,
35 * so the results are ready for transmission or storage. Each instance of
36 * JSONWriter can produce one JSON text.
38 * A JSONWriter instance provides a <code>value</code> method for appending
39 * values to the text, and a <code>key</code> method for adding keys before
40 * values in objects. There are <code>array</code> and <code>endArray</code>
41 * methods that make and bound array values, and <code>object</code> and
42 * <code>endObject</code> methods which make and bound object values. All of
43 * these methods return the JSONWriter instance, permitting a cascade style. For
47 * new JSONWriter(myWriter).object().key("JSON").value("Hello, World!")
54 * {"JSON":"Hello, World!"}
57 * The first method called must be <code>array</code> or <code>object</code>.
58 * There are no methods for adding commas or colons. JSONWriter adds them for
59 * you. Objects and arrays can be nested up to 200 levels deep.
61 * This can sometimes be easier than using a JSONObject to build a string.
66 public class JSONWriter
68 private static final int maxdepth = 200;
71 * The comma flag determines if a comma should be output before the next
74 private boolean comma;
77 * The current mode. Values: 'a' (array), 'd' (done), 'i' (initial), 'k'
78 * (key), 'o' (object).
83 * The object/array stack.
85 private final JSONObject stack[];
88 * The stack top index. A value of 0 indicates that the stack is empty.
93 * The writer that will receive the output.
95 protected Appendable writer;
98 * Make a fresh JSONWriter. It can be used to build one JSON text.
100 public JSONWriter(Appendable w)
104 this.stack = new JSONObject[maxdepth];
115 * @throws JSONException
116 * If the value is out of sequence.
118 private JSONWriter append(String string) throws JSONException
122 throw new JSONException("Null pointer");
124 if (this.mode == 'o' || this.mode == 'a')
128 if (this.comma && this.mode == 'a')
130 this.writer.append(',');
132 this.writer.append(string);
133 } catch (IOException e)
135 // Android as of API 25 does not support this exception constructor
136 // however we won't worry about it. If an exception is happening here
137 // it will just throw a "Method not found" exception instead.
138 throw new JSONException(e);
140 if (this.mode == 'o')
147 throw new JSONException("Value out of sequence.");
151 * Begin appending a new array. All values until the balancing
152 * <code>endArray</code> will be appended to this array. The
153 * <code>endArray</code> method must be called to mark the array's end.
156 * @throws JSONException
157 * If the nesting is too deep, or if the object is started in the
158 * wrong place (for example as a key or after the end of the
159 * outermost array or object).
161 public JSONWriter array() throws JSONException
163 if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a')
170 throw new JSONException("Misplaced array.");
181 * @throws JSONException
184 private JSONWriter end(char m, char c) throws JSONException
188 throw new JSONException(
189 m == 'a' ? "Misplaced endArray." : "Misplaced endObject.");
194 this.writer.append(c);
195 } catch (IOException e)
197 // Android as of API 25 does not support this exception constructor
198 // however we won't worry about it. If an exception is happening here
199 // it will just throw a "Method not found" exception instead.
200 throw new JSONException(e);
207 * End an array. This method most be called to balance calls to
208 * <code>array</code>.
211 * @throws JSONException
212 * If incorrectly nested.
214 public JSONWriter endArray() throws JSONException
216 return this.end('a', ']');
220 * End an object. This method most be called to balance calls to
221 * <code>object</code>.
224 * @throws JSONException
225 * If incorrectly nested.
227 public JSONWriter endObject() throws JSONException
229 return this.end('k', '}');
233 * Append a key. The key will be associated with the next value. In an object,
234 * every value must be preceded by a key.
239 * @throws JSONException
240 * If the key is out of place. For example, keys do not belong in
241 * arrays or if the key is null.
243 public JSONWriter key(String string) throws JSONException
247 throw new JSONException("Null key.");
249 if (this.mode == 'k')
253 JSONObject topObject = this.stack[this.top - 1];
254 // don't use the built in putOnce method to maintain Android support
255 if (topObject.has(string))
257 throw new JSONException("Duplicate key \"" + string + "\"");
259 topObject.put(string, true);
262 this.writer.append(',');
264 this.writer.append(JSONObject.quote(string));
265 this.writer.append(':');
269 } catch (IOException e)
271 // Android as of API 25 does not support this exception constructor
272 // however we won't worry about it. If an exception is happening here
273 // it will just throw a "Method not found" exception instead.
274 throw new JSONException(e);
277 throw new JSONException("Misplaced key.");
281 * Begin appending a new object. All keys and values until the balancing
282 * <code>endObject</code> will be appended to this object. The
283 * <code>endObject</code> method must be called to mark the object's end.
286 * @throws JSONException
287 * If the nesting is too deep, or if the object is started in the
288 * wrong place (for example as a key or after the end of the
289 * outermost array or object).
291 public JSONWriter object() throws JSONException
293 if (this.mode == 'i')
297 if (this.mode == 'o' || this.mode == 'a')
300 this.push(new JSONObject());
304 throw new JSONException("Misplaced object.");
309 * Pop an array or object scope.
312 * The scope to close.
313 * @throws JSONException
314 * If nesting is wrong.
316 private void pop(char c) throws JSONException
320 throw new JSONException("Nesting error.");
322 char m = this.stack[this.top - 1] == null ? 'a' : 'k';
325 throw new JSONException("Nesting error.");
328 this.mode = this.top == 0 ? 'd'
329 : this.stack[this.top - 1] == null ? 'a' : 'k';
333 * Push an array or object scope.
337 * @throws JSONException
338 * If nesting is too deep.
340 private void push(JSONObject jo) throws JSONException
342 if (this.top >= maxdepth)
344 throw new JSONException("Nesting too deep.");
346 this.stack[this.top] = jo;
347 this.mode = jo == null ? 'a' : 'k';
352 * Make a JSON text of an Object value. If the object has an
353 * value.toJSONString() method, then that method will be used to produce the
354 * JSON text. The method is required to produce a strictly conforming text. If
355 * the object does not contain a toJSONString method (which is the most common
356 * case), then a text will be produced by other means. If the value is an
357 * array or Collection, then a JSONArray will be made from it and its
358 * toJSONString method will be called. If the value is a MAP, then a
359 * JSONObject will be made from it and its toJSONString method will be called.
360 * Otherwise, the value's toString method will be called, and the result will
364 * Warning: This method assumes that the data structure is acyclical.
367 * The value to be serialized.
368 * @return a printable, displayable, transmittable representation of the
369 * object, beginning with <code>{</code> <small>(left
370 * brace)</small> and ending with <code>}</code> <small>(right
372 * @throws JSONException
373 * If the value is or contains an invalid number.
375 public static String valueToString(Object value) throws JSONException
377 if (value == null || value.equals(null))
381 if (value instanceof JSONString)
386 object = ((JSONString) value).toJSONString();
387 } catch (Exception e)
389 throw new JSONException(e);
391 if (object instanceof String)
393 return (String) object;
395 throw new JSONException("Bad value from toJSONString: " + object);
397 if (value instanceof Number)
399 // not all Numbers may match actual JSON Numbers. i.e. Fractions or
401 final String numberAsString = JSONObject
402 .numberToString((Number) value);
405 // Use the BigDecimal constructor for it's parser to validate the
407 @SuppressWarnings("unused")
408 BigDecimal unused = new BigDecimal(numberAsString);
409 // Close enough to a JSON number that we will return it unquoted
410 return numberAsString;
411 } catch (NumberFormatException ex)
413 // The Number value is not a valid JSON number.
414 // Instead we will quote it as a string
415 return JSONObject.quote(numberAsString);
418 if (value instanceof Boolean || value instanceof JSONObject
419 || value instanceof JSONArray)
421 return value.toString();
423 if (value instanceof Map)
425 Map<?, ?> map = (Map<?, ?>) value;
426 return new JSONObject(map).toString();
428 if (value instanceof Collection)
430 Collection<?> coll = (Collection<?>) value;
431 return new JSONArray(coll).toString();
433 if (value.getClass().isArray())
435 return new JSONArray(value).toString();
437 if (value instanceof Enum<?>)
439 return JSONObject.quote(((Enum<?>) value).name());
441 return JSONObject.quote(value.toString());
445 * Append either the value <code>true</code> or the value <code>false</code>.
450 * @throws JSONException
452 public JSONWriter value(boolean b) throws JSONException
454 return this.append(b ? "true" : "false");
458 * Append a double value.
463 * @throws JSONException
464 * If the number is not finite.
466 public JSONWriter value(double d) throws JSONException
468 return this.value(Double.valueOf(d));
472 * Append a long value.
477 * @throws JSONException
479 public JSONWriter value(long l) throws JSONException
481 return this.append(Long.toString(l));
485 * Append an object value.
488 * The object to append. It can be null, or a Boolean, Number,
489 * String, JSONObject, or JSONArray, or an object that implements
492 * @throws JSONException
493 * If the value is out of sequence.
495 public JSONWriter value(Object object) throws JSONException
497 return this.append(valueToString(object));