Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / org / json / JSONStringer.java @ 59

History | View | Annotate | Download (3.12 KB)

1
package org.json;
2

    
3
/*
4
Copyright (c) 2006 JSON.org
5

6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12

13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15

16
The Software shall be used for Good, not Evil.
17

18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
SOFTWARE.
25
*/
26

    
27
import java.io.StringWriter;
28

    
29
/**
30
 * JSONStringer provides a quick and convenient way of producing JSON text. The texts produced strictly conform to JSON syntax
31
 * rules. No whitespace is added, so the results are ready for transmission or storage. Each instance of JSONStringer can produce
32
 * one JSON text.
33
 * <p>
34
 * A JSONStringer instance provides a <code>value</code> method for appending values to the text, and a <code>key</code>
35
 * method for adding keys before values in objects. There are <code>array</code> and <code>endArray</code> methods that make
36
 * and bound array values, and <code>object</code> and <code>endObject</code> methods which make and bound object values. All
37
 * of these methods return the JSONWriter instance, permitting cascade style. For example,
38
 *
39
 * <pre>
40
 * myString = new JSONStringer().object().key(&quot;JSON&quot;).value(&quot;Hello, World!&quot;).endObject().toString();
41
 * </pre>
42
 *
43
 * which produces the string
44
 *
45
 * <pre>
46
 * {&quot;JSON&quot;:&quot;Hello, World!&quot;}
47
 * </pre>
48
 *
49
 * <p>
50
 * The first method called must be <code>array</code> or <code>object</code>. There are no methods for adding commas or
51
 * colons. JSONStringer adds them for you. Objects and arrays can be nested up to 20 levels deep.
52
 * <p>
53
 * This can sometimes be easier than using a JSONObject to build a string.
54
 *
55
 * @author JSON.org
56
 * @version 2
57
 */
58
public class JSONStringer
59
         extends
60
            JSONWriter {
61
   /**
62
    * Make a fresh JSONStringer. It can be used to build one JSON text.
63
    */
64
   public JSONStringer() {
65
      super(new StringWriter());
66
   }
67

    
68

    
69
   /**
70
    * Return the JSON text. This method is used to obtain the product of the JSONStringer instance. It will return
71
    * <code>null</code> if there was a problem in the construction of the JSON text (such as the calls to <code>array</code>
72
    * were not properly balanced with calls to <code>endArray</code>).
73
    *
74
    * @return The JSON text.
75
    */
76
   @Override
77
   public String toString() {
78
      return this.mode == 'd' ? this.writer.toString() : null;
79
   }
80
}