4141public class JSONWriter {
4242 private boolean indentMode = false ;
4343 private int indentLevel = 0 ;
44- private StringBuffer buf = new StringBuffer ();
44+ private StringBuilder buf = new StringBuilder ();
4545
4646 public JSONWriter () {}
4747
@@ -86,6 +86,7 @@ public String write(boolean b) {
8686 return write (Boolean .valueOf (b ));
8787 }
8888
89+ @ SuppressWarnings ("unchecked" )
8990 private void value (Object object ) {
9091 if (object == null ) add ("null" );
9192 else if (object instanceof JSONSerializable ) {
@@ -97,7 +98,7 @@ else if (object instanceof JSONSerializable) {
9798 else if (object instanceof Character ) string (object );
9899 else if (object instanceof Map ) map ((Map <String , Object >) object );
99100 else if (object .getClass ().isArray ()) array (object );
100- else if (object instanceof Collection ) array (((Collection ) object ).iterator ());
101+ else if (object instanceof Collection ) array (((Collection <?> ) object ).iterator ());
101102 else bean (object );
102103 }
103104
@@ -111,7 +112,7 @@ private void bean(Object object) {
111112 * @param object the object
112113 * @param properties explicit list of property/field names to include - may be null for "all"
113114 */
114- public void writeLimited (Class klass , Object object , String [] properties ) {
115+ public void writeLimited (Class <?> klass , Object object , String [] properties ) {
115116 Set <String > propertiesSet = null ;
116117 if (properties != null ) {
117118 propertiesSet = new HashSet <String >();
@@ -120,7 +121,7 @@ public void writeLimited(Class klass, Object object, String[] properties) {
120121 }
121122 }
122123
123- add ("{" ); indentLevel += 2 ; newline ();
124+ add ('{' ); indentLevel += 2 ; newline ();
124125 boolean needComma = false ;
125126
126127 BeanInfo info ;
@@ -174,7 +175,7 @@ public void writeLimited(Class klass, Object object, String[] properties) {
174175 }
175176 }
176177
177- indentLevel -= 2 ; newline (); add ("}" );
178+ indentLevel -= 2 ; newline (); add ('}' );
178179 }
179180
180181 private void add (String name , Object value ) {
@@ -185,35 +186,45 @@ private void add(String name, Object value) {
185186 }
186187
187188 private void map (Map <String , Object > map ) {
188- add ("{" ); indentLevel += 2 ; newline ();
189+ add ('{' ); indentLevel += 2 ; newline ();
189190 Iterator <String > it = map .keySet ().iterator ();
191+ if (it .hasNext ()) {
192+ mapEntry (it .next (), map );
193+ }
190194 while (it .hasNext ()) {
195+ add (',' ); newline ();
191196 Object key = it .next ();
192197 value (key );
193- add (":" );
198+ add (':' );
194199 value (map .get (key ));
195- if (it .hasNext ()) { add ("," ); newline (); }
196200 }
197- indentLevel -= 2 ; newline (); add ("}" );
201+ indentLevel -= 2 ; newline (); add ('}' );
202+ }
203+ private void mapEntry (Object key , Map <String , Object > map ) {
204+ value (key );
205+ add (':' );
206+ value (map .get (key ));
198207 }
199208
200- private void array (Iterator it ) {
201- add ("[" );
209+ private void array (Iterator <?> it ) {
210+ add ('[' );
211+ if (it .hasNext ()) value (it .next ());
202212 while (it .hasNext ()) {
213+ add (',' );
203214 value (it .next ());
204- if (it .hasNext ()) add ("," );
205215 }
206- add ("]" );
216+ add (']' );
207217 }
208218
209219 private void array (Object object ) {
210- add ("[" );
220+ add ('[' );
211221 int length = Array .getLength (object );
212- for (int i = 0 ; i < length ; ++i ) {
222+ if (length > 0 ) value (Array .get (object , 0 ));
223+ for (int i = 1 ; i < length ; ++i ) {
224+ add (',' );
213225 value (Array .get (object , i ));
214- if (i < length - 1 ) add (',' );
215226 }
216- add ("]" );
227+ add (']' );
217228 }
218229
219230 private void bool (boolean b ) {
0 commit comments