Avoided repeated calls to StringBuffer#toString from JSONObject#toJSONString(Map). This yields performance gains of up to two orders of magnitude on large objects in testing.

This commit is contained in:
jon.chambers@gmail.com
2013-08-10 04:37:10 +00:00
parent d4af019078
commit 8fafb8db6d

View File

@@ -5,6 +5,7 @@
package org.json.simple;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
@@ -83,33 +84,28 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @return JSON text, or "null" if map is null.
*/
public static String toJSONString(Map map){
if(map == null)
return "null";
final StringWriter writer = new StringWriter();
StringBuffer sb = new StringBuffer();
boolean first = true;
Iterator iter=map.entrySet().iterator();
sb.append('{');
while(iter.hasNext()){
if(first)
first = false;
else
sb.append(',');
Map.Entry entry=(Map.Entry)iter.next();
toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
try {
writeJSONString(map, writer);
return writer.toString();
} catch (IOException e) {
// This should never happen with a StringWriter
throw new RuntimeException(e);
}
sb.append('}');
return sb.toString();
}
public String toJSONString(){
return toJSONString(this);
}
private static String toJSONString(String key,Object value, StringBuffer sb){
sb.append('\"');
public String toString(){
return toJSONString();
}
public static String toString(String key,Object value){
StringBuffer sb = new StringBuffer();
sb.append('\"');
if(key == null)
sb.append("null");
else
@@ -121,16 +117,6 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
return sb.toString();
}
public String toString(){
return toJSONString();
}
public static String toString(String key,Object value){
StringBuffer sb = new StringBuffer();
toJSONString(key, value, sb);
return sb.toString();
}
/**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
* It's the same as JSONValue.escape() only for compatibility here.