@@ -94,11 +94,11 @@ public class JSONWriter {
9494 * Make a fresh JSONWriter. It can be used to build one JSON text.
9595 */
9696 public JSONWriter (Writer w ) {
97- this . comma = false ;
98- this . mode = 'i' ;
99- this . stack = new JSONObject [maxdepth ];
100- this . top = 0 ;
101- this . writer = w ;
97+ comma = false ;
98+ mode = 'i' ;
99+ stack = new JSONObject [maxdepth ];
100+ top = 0 ;
101+ writer = w ;
102102 }
103103
104104 /**
@@ -111,19 +111,19 @@ private JSONWriter append(String string) throws JSONException {
111111 if (string == null ) {
112112 throw new JSONException ("Null pointer" );
113113 }
114- if (this . mode == 'o' || this . mode == 'a' ) {
114+ if (mode == 'o' || mode == 'a' ) {
115115 try {
116- if (this . comma && this . mode == 'a' ) {
117- this . writer .write (',' );
116+ if (comma && mode == 'a' ) {
117+ writer .write (',' );
118118 }
119- this . writer .write (string );
119+ writer .write (string );
120120 } catch (IOException e ) {
121121 throw new JSONException (e );
122122 }
123- if (this . mode == 'o' ) {
124- this . mode = 'k' ;
123+ if (mode == 'o' ) {
124+ mode = 'k' ;
125125 }
126- this . comma = true ;
126+ comma = true ;
127127 return this ;
128128 }
129129 throw new JSONException ("Value out of sequence." );
@@ -139,10 +139,10 @@ private JSONWriter append(String string) throws JSONException {
139139 * outermost array or object).
140140 */
141141 public JSONWriter array () throws JSONException {
142- if (this . mode == 'i' || this . mode == 'o' || this . mode == 'a' ) {
143- this . push (null );
144- this . append ("[" );
145- this . comma = false ;
142+ if (mode == 'i' || mode == 'o' || mode == 'a' ) {
143+ push (null );
144+ append ("[" );
145+ comma = false ;
146146 return this ;
147147 }
148148 throw new JSONException ("Misplaced array." );
@@ -161,13 +161,13 @@ private JSONWriter end(char mode, char c) throws JSONException {
161161 ? "Misplaced endArray."
162162 : "Misplaced endObject." );
163163 }
164- this . pop (mode );
164+ pop (mode );
165165 try {
166- this . writer .write (c );
166+ writer .write (c );
167167 } catch (IOException e ) {
168168 throw new JSONException (e );
169169 }
170- this . comma = true ;
170+ comma = true ;
171171 return this ;
172172 }
173173
@@ -178,7 +178,7 @@ private JSONWriter end(char mode, char c) throws JSONException {
178178 * @throws JSONException If incorrectly nested.
179179 */
180180 public JSONWriter endArray () throws JSONException {
181- return this . end ('a' , ']' );
181+ return end ('a' , ']' );
182182 }
183183
184184 /**
@@ -188,9 +188,30 @@ public JSONWriter endArray() throws JSONException {
188188 * @throws JSONException If incorrectly nested.
189189 */
190190 public JSONWriter endObject () throws JSONException {
191- return this . end ('k' , '}' );
191+ return end ('k' , '}' );
192192 }
193193
194+ /**
195+ * End an object or array. This method most be called to balance calls to
196+ * <code>object</code> or <code>array</code>.
197+ * @return this
198+ * @throws JSONException If incorrectly nested.
199+ */
200+ public JSONWriter end () throws JSONException {
201+ char bracket ;
202+ switch (mode ) {
203+ case 'a' :
204+ bracket = ']' ;
205+ break ;
206+ case 'o' :
207+ bracket = '}' ;
208+ break ;
209+ default :
210+ throw new JSONException ("Misplaced end." );
211+ }
212+ return end (mode , bracket );
213+ }
214+
194215 /**
195216 * Append a key. The key will be associated with the next value. In an
196217 * object, every value must be preceded by a key.
@@ -203,16 +224,16 @@ public JSONWriter key(String string) throws JSONException {
203224 if (string == null ) {
204225 throw new JSONException ("Null key." );
205226 }
206- if (this . mode == 'k' ) {
227+ if (mode == 'k' ) {
207228 try {
208- this . stack [this . top - 1 ].putOnce (string , Boolean .TRUE );
209- if (this . comma ) {
210- this . writer .write (',' );
229+ stack [top - 1 ].putOnce (string , Boolean .TRUE );
230+ if (comma ) {
231+ writer .write (',' );
211232 }
212- this . writer .write (JSONObject .quote (string ));
213- this . writer .write (':' );
214- this . comma = false ;
215- this . mode = 'o' ;
233+ writer .write (JSONObject .quote (string ));
234+ writer .write (':' );
235+ comma = false ;
236+ mode = 'o' ;
216237 return this ;
217238 } catch (IOException e ) {
218239 throw new JSONException (e );
@@ -232,13 +253,13 @@ public JSONWriter key(String string) throws JSONException {
232253 * outermost array or object).
233254 */
234255 public JSONWriter object () throws JSONException {
235- if (this . mode == 'i' ) {
236- this . mode = 'o' ;
256+ if (mode == 'i' ) {
257+ mode = 'o' ;
237258 }
238- if (this . mode == 'o' || this . mode == 'a' ) {
239- this . append ("{" );
240- this . push (new JSONObject ());
241- this . comma = false ;
259+ if (mode == 'o' || mode == 'a' ) {
260+ append ("{" );
261+ push (new JSONObject ());
262+ comma = false ;
242263 return this ;
243264 }
244265 throw new JSONException ("Misplaced object." );
@@ -252,17 +273,17 @@ public JSONWriter object() throws JSONException {
252273 * @throws JSONException If nesting is wrong.
253274 */
254275 private void pop (char c ) throws JSONException {
255- if (this . top <= 0 ) {
276+ if (top <= 0 ) {
256277 throw new JSONException ("Nesting error." );
257278 }
258- char m = this . stack [this . top - 1 ] == null ? 'a' : 'k' ;
279+ char m = stack [top - 1 ] == null ? 'a' : 'k' ;
259280 if (m != c ) {
260281 throw new JSONException ("Nesting error." );
261282 }
262- this . top -= 1 ;
263- this . mode = this . top == 0
283+ top -= 1 ;
284+ mode = top == 0
264285 ? 'd'
265- : this . stack [this . top - 1 ] == null
286+ : stack [top - 1 ] == null
266287 ? 'a'
267288 : 'k' ;
268289 }
@@ -273,12 +294,12 @@ private void pop(char c) throws JSONException {
273294 * @throws JSONException If nesting is too deep.
274295 */
275296 private void push (JSONObject jo ) throws JSONException {
276- if (this . top >= maxdepth ) {
297+ if (top >= maxdepth ) {
277298 throw new JSONException ("Nesting too deep." );
278299 }
279- this . stack [this . top ] = jo ;
280- this . mode = jo == null ? 'a' : 'k' ;
281- this . top += 1 ;
300+ stack [top ] = jo ;
301+ mode = jo == null ? 'a' : 'k' ;
302+ top += 1 ;
282303 }
283304
284305
@@ -290,7 +311,7 @@ private void push(JSONObject jo) throws JSONException {
290311 * @throws JSONException
291312 */
292313 public JSONWriter value (boolean b ) throws JSONException {
293- return this . append (b ? "true" : "false" );
314+ return append (b ? "true" : "false" );
294315 }
295316
296317 /**
@@ -300,7 +321,7 @@ public JSONWriter value(boolean b) throws JSONException {
300321 * @throws JSONException If the number is not finite.
301322 */
302323 public JSONWriter value (double d ) throws JSONException {
303- return this . value (new Double (d ));
324+ return value (new Double (d ));
304325 }
305326
306327 /**
@@ -310,7 +331,7 @@ public JSONWriter value(double d) throws JSONException {
310331 * @throws JSONException
311332 */
312333 public JSONWriter value (long l ) throws JSONException {
313- return this . append (Long .toString (l ));
334+ return append (Long .toString (l ));
314335 }
315336
316337
@@ -322,6 +343,6 @@ public JSONWriter value(long l) throws JSONException {
322343 * @throws JSONException If the value is out of sequence.
323344 */
324345 public JSONWriter value (Object object ) throws JSONException {
325- return this . append (JSONObject .valueToString (object ));
346+ return append (JSONObject .valueToString (object ));
326347 }
327348}
0 commit comments