mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-06 07:20:53 +03:00
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:
@@ -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.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
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.
|
* @return JSON text, or "null" if map is null.
|
||||||
*/
|
*/
|
||||||
public static String toJSONString(Map map){
|
public static String toJSONString(Map map){
|
||||||
if(map == null)
|
final StringWriter writer = new StringWriter();
|
||||||
return "null";
|
|
||||||
|
|
||||||
StringBuffer sb = new StringBuffer();
|
try {
|
||||||
boolean first = true;
|
writeJSONString(map, writer);
|
||||||
Iterator iter=map.entrySet().iterator();
|
return writer.toString();
|
||||||
|
} catch (IOException e) {
|
||||||
sb.append('{');
|
// This should never happen with a StringWriter
|
||||||
while(iter.hasNext()){
|
throw new RuntimeException(e);
|
||||||
if(first)
|
|
||||||
first = false;
|
|
||||||
else
|
|
||||||
sb.append(',');
|
|
||||||
|
|
||||||
Map.Entry entry=(Map.Entry)iter.next();
|
|
||||||
toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
|
|
||||||
}
|
}
|
||||||
sb.append('}');
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toJSONString(){
|
public String toJSONString(){
|
||||||
return toJSONString(this);
|
return toJSONString(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String toJSONString(String key,Object value, StringBuffer sb){
|
public String toString(){
|
||||||
sb.append('\"');
|
return toJSONString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toString(String key,Object value){
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
sb.append('\"');
|
||||||
if(key == null)
|
if(key == null)
|
||||||
sb.append("null");
|
sb.append("null");
|
||||||
else
|
else
|
||||||
@@ -121,16 +117,6 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
|
|||||||
return sb.toString();
|
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).
|
* 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.
|
* It's the same as JSONValue.escape() only for compatibility here.
|
||||||
|
|||||||
Reference in New Issue
Block a user