mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-06 07:20:53 +03:00
Routed most toJSONString methods through writeJSONString with a StringWriter to reduce duplicated logic (and in some cases improve performance).
This commit is contained in:
@@ -90,12 +90,11 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
|
||||
try {
|
||||
writeJSONString(collection, writer);
|
||||
} catch (IOException e) {
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(byte[] array, Writer out) throws IOException{
|
||||
@@ -117,27 +116,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(byte[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(String.valueOf(array[0]));
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(String.valueOf(array[i]));
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(short[] array, Writer out) throws IOException{
|
||||
@@ -159,27 +146,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(short[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(int[] array, Writer out) throws IOException{
|
||||
@@ -201,27 +176,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(int[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(long[] array, Writer out) throws IOException{
|
||||
@@ -243,27 +206,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(long[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(float[] array, Writer out) throws IOException{
|
||||
@@ -285,27 +236,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(float[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(double[] array, Writer out) throws IOException{
|
||||
@@ -327,27 +266,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(double[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(boolean[] array, Writer out) throws IOException{
|
||||
@@ -369,27 +296,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(boolean[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(char[] array, Writer out) throws IOException{
|
||||
@@ -411,27 +326,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(char[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[\"");
|
||||
buffer.append(array[0]);
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append("\",\"");
|
||||
buffer.append(array[i]);
|
||||
}
|
||||
|
||||
buffer.append("\"]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static void writeJSONString(Object[] array, Writer out) throws IOException{
|
||||
@@ -453,27 +356,15 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||
}
|
||||
|
||||
public static String toJSONString(Object[] array){
|
||||
if(array == null){
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
try {
|
||||
writeJSONString(array, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(array.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
|
||||
buffer.append("[");
|
||||
buffer.append(JSONValue.toJSONString(array[0]));
|
||||
|
||||
for(int i = 1; i < array.length; i++){
|
||||
buffer.append(",");
|
||||
buffer.append(JSONValue.toJSONString(array[i]));
|
||||
}
|
||||
|
||||
buffer.append("]");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toJSONString(){
|
||||
|
||||
@@ -7,6 +7,7 @@ package org.json.simple;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Collection;
|
||||
// import java.util.List;
|
||||
@@ -238,69 +239,15 @@ public class JSONValue {
|
||||
* @return JSON text, or "null" if value is null or it's an NaN or an INF number.
|
||||
*/
|
||||
public static String toJSONString(Object value){
|
||||
if(value == null)
|
||||
return "null";
|
||||
final StringWriter writer = new StringWriter();
|
||||
|
||||
if(value instanceof String)
|
||||
return "\""+escape((String)value)+"\"";
|
||||
|
||||
if(value instanceof Double){
|
||||
if(((Double)value).isInfinite() || ((Double)value).isNaN())
|
||||
return "null";
|
||||
else
|
||||
return value.toString();
|
||||
try{
|
||||
writeJSONString(value, writer);
|
||||
return writer.toString();
|
||||
} catch(IOException e){
|
||||
// This should never happen for a StringWriter
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(value instanceof Float){
|
||||
if(((Float)value).isInfinite() || ((Float)value).isNaN())
|
||||
return "null";
|
||||
else
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
if(value instanceof Number)
|
||||
return value.toString();
|
||||
|
||||
if(value instanceof Boolean)
|
||||
return value.toString();
|
||||
|
||||
if((value instanceof JSONAware))
|
||||
return ((JSONAware)value).toJSONString();
|
||||
|
||||
if(value instanceof Map)
|
||||
return JSONObject.toJSONString((Map)value);
|
||||
|
||||
if(value instanceof Collection)
|
||||
return JSONArray.toJSONString((Collection)value);
|
||||
|
||||
if(value instanceof byte[])
|
||||
return JSONArray.toJSONString((byte[])value);
|
||||
|
||||
if(value instanceof short[])
|
||||
return JSONArray.toJSONString((short[])value);
|
||||
|
||||
if(value instanceof int[])
|
||||
return JSONArray.toJSONString((int[])value);
|
||||
|
||||
if(value instanceof long[])
|
||||
return JSONArray.toJSONString((long[])value);
|
||||
|
||||
if(value instanceof float[])
|
||||
return JSONArray.toJSONString((float[])value);
|
||||
|
||||
if(value instanceof double[])
|
||||
return JSONArray.toJSONString((double[])value);
|
||||
|
||||
if(value instanceof boolean[])
|
||||
return JSONArray.toJSONString((boolean[])value);
|
||||
|
||||
if(value instanceof char[])
|
||||
return JSONArray.toJSONString((char[])value);
|
||||
|
||||
if(value instanceof Object[])
|
||||
return JSONArray.toJSONString((Object[])value);
|
||||
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user