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.
34 * The texts produced strictly conform to JSON syntax rules. No whitespace is
35 * added, 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
40 * text, and a <code>key</code>
41 * method for adding keys before values in objects. There are <code>array</code>
42 * and <code>endArray</code> methods that make and bound array values, and
43 * <code>object</code> and <code>endObject</code> methods which make and bound
44 * object values. All of these methods return the JSONWriter instance,
45 * permitting a cascade style. For example, <pre>
46 * new JSONWriter(myWriter)
49 * .value("Hello, World!")
50 * .endObject();</pre> which writes <pre>
51 * {"JSON":"Hello, World!"}</pre>
53 * The first method called must be <code>array</code> or <code>object</code>.
54 * There are no methods for adding commas or colons. JSONWriter adds them for
55 * you. Objects and arrays can be nested up to 200 levels deep.
57 * This can sometimes be easier than using a JSONObject to build a string.
61 public class JSONWriter {
62 private static final int maxdepth = 200;
65 * The comma flag determines if a comma should be output before the next
68 private boolean comma;
71 * The current mode. Values:
81 * The object/array stack.
83 private final JSONObject stack[];
86 * The stack top index. A value of 0 indicates that the stack is empty.
91 * The writer that will receive the output.
93 protected Appendable writer;
96 * Make a fresh JSONWriter. It can be used to build one JSON text.
98 public JSONWriter(Appendable w) {
101 this.stack = new JSONObject[maxdepth];
108 * @param string A string value.
110 * @throws JSONException If the value is out of sequence.
112 private JSONWriter append(String string) throws JSONException {
113 if (string == null) {
114 throw new JSONException("Null pointer");
116 if (this.mode == 'o' || this.mode == 'a') {
118 if (this.comma && this.mode == 'a') {
119 this.writer.append(',');
121 this.writer.append(string);
122 } catch (IOException e) {
123 // Android as of API 25 does not support this exception constructor
124 // however we won't worry about it. If an exception is happening here
125 // it will just throw a "Method not found" exception instead.
126 throw new JSONException(e);
128 if (this.mode == 'o') {
134 throw new JSONException("Value out of sequence.");
138 * Begin appending a new array. All values until the balancing
139 * <code>endArray</code> will be appended to this array. The
140 * <code>endArray</code> method must be called to mark the array's end.
142 * @throws JSONException If the nesting is too deep, or if the object is
143 * started in the wrong place (for example as a key or after the end of the
144 * outermost array or object).
146 public JSONWriter array() throws JSONException {
147 if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
153 throw new JSONException("Misplaced array.");
159 * @param c Closing character
161 * @throws JSONException If unbalanced.
163 private JSONWriter end(char m, char c) throws JSONException {
164 if (this.mode != m) {
165 throw new JSONException(m == 'a'
166 ? "Misplaced endArray."
167 : "Misplaced endObject.");
171 this.writer.append(c);
172 } catch (IOException e) {
173 // Android as of API 25 does not support this exception constructor
174 // however we won't worry about it. If an exception is happening here
175 // it will just throw a "Method not found" exception instead.
176 throw new JSONException(e);
183 * End an array. This method most be called to balance calls to
184 * <code>array</code>.
186 * @throws JSONException If incorrectly nested.
188 public JSONWriter endArray() throws JSONException {
189 return this.end('a', ']');
193 * End an object. This method most be called to balance calls to
194 * <code>object</code>.
196 * @throws JSONException If incorrectly nested.
198 public JSONWriter endObject() throws JSONException {
199 return this.end('k', '}');
203 * Append a key. The key will be associated with the next value. In an
204 * object, every value must be preceded by a key.
205 * @param string A key string.
207 * @throws JSONException If the key is out of place. For example, keys
208 * do not belong in arrays or if the key is null.
210 public JSONWriter key(String string) throws JSONException {
211 if (string == null) {
212 throw new JSONException("Null key.");
214 if (this.mode == 'k') {
216 JSONObject topObject = this.stack[this.top - 1];
217 // don't use the built in putOnce method to maintain Android support
218 if(topObject.has(string)) {
219 throw new JSONException("Duplicate key \"" + string + "\"");
221 topObject.put(string, true);
223 this.writer.append(',');
225 this.writer.append(JSONObject.quote(string));
226 this.writer.append(':');
230 } catch (IOException e) {
231 // Android as of API 25 does not support this exception constructor
232 // however we won't worry about it. If an exception is happening here
233 // it will just throw a "Method not found" exception instead.
234 throw new JSONException(e);
237 throw new JSONException("Misplaced key.");
242 * Begin appending a new object. All keys and values until the balancing
243 * <code>endObject</code> will be appended to this object. The
244 * <code>endObject</code> method must be called to mark the object's end.
246 * @throws JSONException If the nesting is too deep, or if the object is
247 * started in the wrong place (for example as a key or after the end of the
248 * outermost array or object).
250 public JSONWriter object() throws JSONException {
251 if (this.mode == 'i') {
254 if (this.mode == 'o' || this.mode == 'a') {
256 this.push(new JSONObject());
260 throw new JSONException("Misplaced object.");
266 * Pop an array or object scope.
267 * @param c The scope to close.
268 * @throws JSONException If nesting is wrong.
270 private void pop(char c) throws JSONException {
272 throw new JSONException("Nesting error.");
274 char m = this.stack[this.top - 1] == null ? 'a' : 'k';
276 throw new JSONException("Nesting error.");
279 this.mode = this.top == 0
281 : this.stack[this.top - 1] == null
287 * Push an array or object scope.
288 * @param jo The scope to open.
289 * @throws JSONException If nesting is too deep.
291 private void push(JSONObject jo) throws JSONException {
292 if (this.top >= maxdepth) {
293 throw new JSONException("Nesting too deep.");
295 this.stack[this.top] = jo;
296 this.mode = jo == null ? 'a' : 'k';
301 * Make a JSON text of an Object value. If the object has an
302 * value.toJSONString() method, then that method will be used to produce the
303 * JSON text. The method is required to produce a strictly conforming text.
304 * If the object does not contain a toJSONString method (which is the most
305 * common case), then a text will be produced by other means. If the value
306 * is an array or Collection, then a JSONArray will be made from it and its
307 * toJSONString method will be called. If the value is a MAP, then a
308 * JSONObject will be made from it and its toJSONString method will be
309 * called. Otherwise, the value's toString method will be called, and the
310 * result will be quoted.
313 * Warning: This method assumes that the data structure is acyclical.
316 * The value to be serialized.
317 * @return a printable, displayable, transmittable representation of the
318 * object, beginning with <code>{</code> <small>(left
319 * brace)</small> and ending with <code>}</code> <small>(right
321 * @throws JSONException
322 * If the value is or contains an invalid number.
324 public static String valueToString(Object value) throws JSONException {
325 if (value == null || value.equals(null)) {
328 if (value instanceof JSONString) {
331 object = ((JSONString) value).toJSONString();
332 } catch (Exception e) {
333 throw new JSONException(e);
335 if (object instanceof String) {
336 return (String) object;
338 throw new JSONException("Bad value from toJSONString: " + object);
340 if (value instanceof Number) {
341 // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
342 final String numberAsString = JSONObject.numberToString((Number) value);
344 // Use the BigDecimal constructor for it's parser to validate the format.
345 @SuppressWarnings("unused")
346 BigDecimal unused = new BigDecimal(numberAsString);
347 // Close enough to a JSON number that we will return it unquoted
348 return numberAsString;
349 } catch (NumberFormatException ex){
350 // The Number value is not a valid JSON number.
351 // Instead we will quote it as a string
352 return JSONObject.quote(numberAsString);
355 if (value instanceof Boolean || value instanceof JSONObject
356 || value instanceof JSONArray) {
357 return value.toString();
359 if (value instanceof Map) {
360 Map<?, ?> map = (Map<?, ?>) value;
361 return new JSONObject(map).toString();
363 if (value instanceof Collection) {
364 Collection<?> coll = (Collection<?>) value;
365 return new JSONArray(coll).toString();
367 if (value.getClass().isArray()) {
368 return new JSONArray(value).toString();
370 if(value instanceof Enum<?>){
371 return JSONObject.quote(((Enum<?>)value).name());
373 return JSONObject.quote(value.toString());
377 * Append either the value <code>true</code> or the value
378 * <code>false</code>.
379 * @param b A boolean.
381 * @throws JSONException
383 public JSONWriter value(boolean b) throws JSONException {
384 return this.append(b ? "true" : "false");
388 * Append a double value.
391 * @throws JSONException If the number is not finite.
393 public JSONWriter value(double d) throws JSONException {
394 return this.value(Double.valueOf(d));
398 * Append a long value.
401 * @throws JSONException
403 public JSONWriter value(long l) throws JSONException {
404 return this.append(Long.toString(l));
409 * Append an object value.
410 * @param object The object to append. It can be null, or a Boolean, Number,
411 * String, JSONObject, or JSONArray, or an object that implements JSONString.
413 * @throws JSONException If the value is out of sequence.
415 public JSONWriter value(Object object) throws JSONException {
416 return this.append(valueToString(object));