mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-08 16:28:04 +03:00
Avoided passing temporary strings when serializing a collection for a modest performance gain.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
package org.json.simple;
|
package org.json.simple;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -85,29 +86,16 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
|||||||
* @return JSON text, or "null" if list is null.
|
* @return JSON text, or "null" if list is null.
|
||||||
*/
|
*/
|
||||||
public static String toJSONString(Collection collection){
|
public static String toJSONString(Collection collection){
|
||||||
if(collection == null)
|
final StringWriter writer = new StringWriter();
|
||||||
return "null";
|
|
||||||
|
|
||||||
boolean first = true;
|
try {
|
||||||
StringBuffer sb = new StringBuffer();
|
writeJSONString(collection, writer);
|
||||||
Iterator iter=collection.iterator();
|
} catch (IOException e) {
|
||||||
|
// This should never happen for a StringWriter
|
||||||
sb.append('[');
|
throw new RuntimeException(e);
|
||||||
while(iter.hasNext()){
|
|
||||||
if(first)
|
|
||||||
first = false;
|
|
||||||
else
|
|
||||||
sb.append(',');
|
|
||||||
|
|
||||||
Object value=iter.next();
|
|
||||||
if(value == null){
|
|
||||||
sb.append("null");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sb.append(JSONValue.toJSONString(value));
|
|
||||||
}
|
}
|
||||||
sb.append(']');
|
|
||||||
return sb.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeJSONString(byte[] array, Writer out) throws IOException{
|
public static void writeJSONString(byte[] array, Writer out) throws IOException{
|
||||||
|
|||||||
Reference in New Issue
Block a user