17c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetpackage com.google.polo.json;
27c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
37c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet/*
47c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetCopyright (c) 2006 JSON.org
57c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
67c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetPermission is hereby granted, free of charge, to any person obtaining a copy
77c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetof this software and associated documentation files (the "Software"), to deal
87c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetin the Software without restriction, including without limitation the rights
97c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetto use, copy, modify, merge, publish, distribute, sublicense, and/or sell
107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetcopies of the Software, and to permit persons to whom the Software is
117c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetfurnished to do so, subject to the following conditions:
127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
137c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetThe above copyright notice and this permission notice shall be included in all
147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetcopies or substantial portions of the Software.
157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
167c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetThe Software shall be used for Good, not Evil.
177c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
187c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
217c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
227c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
237c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
247c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetSOFTWARE.
257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet*/
267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetimport java.io.StringWriter;
287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet/**
307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * JSONStringer provides a quick and convenient way of producing JSON text.
317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * The texts produced strictly conform to JSON syntax rules. No whitespace is
327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * added, so the results are ready for transmission or storage. Each instance of
337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * JSONStringer can produce one JSON text.
347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * <p>
357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * A JSONStringer instance provides a <code>value</code> method for appending
367c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * values to the
377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * text, and a <code>key</code>
387c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * method for adding keys before values in objects. There are <code>array</code>
397c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * and <code>endArray</code> methods that make and bound array values, and
407c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * <code>object</code> and <code>endObject</code> methods which make and bound
417c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * object values. All of these methods return the JSONWriter instance,
427c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * permitting cascade style. For example, <pre>
437c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * myString = new JSONStringer()
447c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet *     .object()
457c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet *         .key("JSON")
467c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet *         .value("Hello, World!")
477c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet *     .endObject()
487c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet *     .toString();</pre> which produces the string <pre>
497c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * {"JSON":"Hello, World!"}</pre>
507c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * <p>
517c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * The first method called must be <code>array</code> or <code>object</code>.
527c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * There are no methods for adding commas or colons. JSONStringer adds them for
537c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * you. Objects and arrays can be nested up to 20 levels deep.
547c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * <p>
557c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * This can sometimes be easier than using a JSONObject to build a string.
567c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * @author JSON.org
577c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet * @version 2008-09-18
587c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet */
597c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetpublic class JSONStringer extends JSONWriter {
607c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    /**
617c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * Make a fresh JSONStringer. It can be used to build one JSON text.
627c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     */
637c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    public JSONStringer() {
647c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        super(new StringWriter());
657c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    }
667c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
677c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    /**
687c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * Return the JSON text. This method is used to obtain the product of the
697c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * JSONStringer instance. It will return <code>null</code> if there was a
707c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * problem in the construction of the JSON text (such as the calls to
717c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * <code>array</code> were not properly balanced with calls to
727c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * <code>endArray</code>).
737c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     * @return The JSON text.
747c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet     */
757c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    public String toString() {
767c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        return this.mode == 'd' ? this.writer.toString() : null;
777c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    }
787c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
79