Coverage Report - net.sf.json.util.JSONStringer
 
Classes in this File Line Coverage Branch Coverage Complexity
JSONStringer
0%
0/3
0%
0/2
1
 
 1  
 /*
 2  
  * Copyright 2002-2009 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package net.sf.json.util;
 17  
 
 18  
 import java.io.StringWriter;
 19  
 
 20  
 /**
 21  
  * JSONStringer provides a quick and convenient way of producing JSON text. The
 22  
  * texts produced strictly conform to JSON syntax rules. No whitespace is added,
 23  
  * so the results are ready for transmission or storage. Each instance of
 24  
  * JSONStringer can produce one JSON text.
 25  
  * <p>
 26  
  * A JSONStringer instance provides a <code>value</code> method for appending
 27  
  * values to the text, and a <code>key</code> method for adding keys before
 28  
  * values in objects. There are <code>array</code> and <code>endArray</code>
 29  
  * methods that make and bound array values, and <code>object</code> and
 30  
  * <code>endObject</code> methods which make and bound object values. All of
 31  
  * these methods return the JSONWriter instance, permitting cascade style. For
 32  
  * example,
 33  
  *
 34  
  * <pre>
 35  
  * myString = new JSONStringer()
 36  
  *     .object()
 37  
  *         .key("JSON")
 38  
  *         .value("Hello, World!")
 39  
  *     .endObject()
 40  
  *     .toString();</pre>
 41  
  *
 42  
  * which produces the string
 43  
  *
 44  
  * <pre>
 45  
  * {"JSON":"Hello, World!"}</pre>
 46  
  *
 47  
  * <p>
 48  
  * The first method called must be <code>array</code> or <code>object</code>.
 49  
  * There are no methods for adding commas or colons. JSONStringer adds them for
 50  
  * you. Objects and arrays can be nested up to 20 levels deep.
 51  
  * <p>
 52  
  * This can sometimes be easier than using a JSONObject to build a string.
 53  
  *
 54  
  * @author JSON.org
 55  
  * @version 2
 56  
  */
 57  
 public class JSONStringer extends JSONBuilder {
 58  
    /**
 59  
     * Make a fresh JSONStringer. It can be used to build one JSON text.
 60  
     */
 61  
    public JSONStringer() {
 62  0
       super( new StringWriter() );
 63  0
    }
 64  
 
 65  
    /**
 66  
     * Return the JSON text. This method is used to obtain the product of the
 67  
     * JSONStringer instance. It will return <code>null</code> if there was a
 68  
     * problem in the construction of the JSON text (such as the calls to
 69  
     * <code>array</code> were not properly balanced with calls to
 70  
     * <code>endArray</code>).
 71  
     *
 72  
     * @return The JSON text.
 73  
     */
 74  
    public String toString() {
 75  0
       return this.mode == 'd' ? this.writer.toString() : null;
 76  
    }
 77  
 }