changeset 1116:633ca24d4d6e

fix JsonToString
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Aug 2017 11:06:08 -0600
parents 809d74db1415
children 9a1aa6fc0b4e
files src/luan/lib/json/JsonToString.java
diffstat 1 files changed, 30 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
diff -r 809d74db1415 -r 633ca24d4d6e src/luan/lib/json/JsonToString.java
--- a/src/luan/lib/json/JsonToString.java	Thu Aug 03 16:57:50 2017 -0600
+++ b/src/luan/lib/json/JsonToString.java	Fri Aug 04 11:06:08 2017 -0600
@@ -73,15 +73,13 @@
 	}
 
 	private static void toString(List list,StringBuilder sb) {
-		if( list.isEmpty() ) {
-			sb.append("{}");
-			return;
-		}
 		sb.append('[');
-		toString(list.get(0),sb);
-		for( int i=1; i<list.size(); i++ ) {
-			sb.append(',');
-			toString(list.get(i),sb);
+		if( !list.isEmpty() ) {
+			toString(list.get(0),sb);
+			for( int i=1; i<list.size(); i++ ) {
+				sb.append(',');
+				toString(list.get(i),sb);
+			}
 		}
 		sb.append(']');
 		return;
@@ -89,11 +87,13 @@
 
 	private static void toString(Map map,StringBuilder sb) throws JsonException {
 		sb.append('{');
-		Iterator<Map.Entry> i = map.entrySet().iterator();
-		toString(i.next(),sb);
-		while( i.hasNext() ) {
-			sb.append(',');
+		if( !map.isEmpty() ) {
+			Iterator<Map.Entry> i = map.entrySet().iterator();
 			toString(i.next(),sb);
+			while( i.hasNext() ) {
+				sb.append(',');
+				toString(i.next(),sb);
+			}
 		}
 		sb.append('}');
 	}
@@ -135,34 +135,34 @@
 	}
 
 	private static void toString(List list,StringBuilder sb,int indented) {
-		if( list.isEmpty() ) {
-			sb.append("{}");
-			return;
+		sb.append('[');
+		if( !list.isEmpty() ) {
+			indent(sb,indented+1);
+			toString(list.get(0),sb,indented+1);
+			for( int i=1; i<list.size(); i++ ) {
+				sb.append(',');
+				indent(sb,indented+1);
+				toString(list.get(i),sb,indented+1);
+			}
+			indent(sb,indented);
 		}
-		sb.append('[');
-		indent(sb,indented+1);
-		toString(list.get(0),sb,indented+1);
-		for( int i=1; i<list.size(); i++ ) {
-			sb.append(',');
-			indent(sb,indented+1);
-			toString(list.get(i),sb,indented+1);
-		}
-		indent(sb,indented);
 		sb.append(']');
 		return;
 	}
 
 	private static void toString(Map map,StringBuilder sb,int indented) throws JsonException {
 		sb.append('{');
-		Iterator<Map.Entry> i = map.entrySet().iterator();
-		indent(sb,indented+1);
-		toString(i.next(),sb,indented+1);
-		while( i.hasNext() ) {
-			sb.append(',');
+		if( !map.isEmpty() ) {
+			Iterator<Map.Entry> i = map.entrySet().iterator();
 			indent(sb,indented+1);
 			toString(i.next(),sb,indented+1);
+			while( i.hasNext() ) {
+				sb.append(',');
+				indent(sb,indented+1);
+				toString(i.next(),sb,indented+1);
+			}
+			indent(sb,indented);
 		}
-		indent(sb,indented);
 		sb.append('}');
 	}