changeset 1117:9a1aa6fc0b4e

remove json_pretty_string and make json_string somewhat pretty
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Aug 2017 11:15:53 -0600
parents 633ca24d4d6e
children e4710ddfd287
files src/luan/lib/json/JsonToString.java src/luan/modules/Parsers.luan
diffstat 2 files changed, 12 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/json/JsonToString.java	Fri Aug 04 11:06:08 2017 -0600
+++ b/src/luan/lib/json/JsonToString.java	Fri Aug 04 11:15:53 2017 -0600
@@ -15,11 +15,12 @@
 
 	public static String toString(Object obj) throws JsonException {
 		StringBuilder sb = new StringBuilder();
-		toString(obj,sb);
+		toString(obj,sb,0);
+		sb.append('\n');
 		return sb.toString();
 	}
 
-	private static void toString(Object obj,StringBuilder sb) throws JsonException {
+	private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException {
 		if( obj == null || obj instanceof Boolean || obj instanceof Number ) {
 			sb.append(obj);
 			return;
@@ -29,11 +30,11 @@
 			return;
 		}
 		if( obj instanceof List ) {
-			toString((List)obj,sb);
+			toString((List)obj,sb,indented);
 			return;
 		}
 		if( obj instanceof Map ) {
-			toString((Map)obj,sb);
+			toString((Map)obj,sb,indented);
 			return;
 		}
 		throw new JsonException("can't handle type "+obj.getClass().getName());
@@ -72,76 +73,13 @@
 		sb.append('"');
 	}
 
-	private static void toString(List list,StringBuilder sb) {
-		sb.append('[');
-		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;
-	}
-
-	private static void toString(Map map,StringBuilder sb) throws JsonException {
-		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('}');
-	}
-
-	private static void toString(Map.Entry entry,StringBuilder sb) throws JsonException {
-		Object key = entry.getKey();
-		if( !(key instanceof String) )
-			throw new JsonException("table keys must be strings");
-		toString((String)key,sb);
-		sb.append(':');
-		toString(entry.getValue(),sb);
-	}
-
-
-	public static String toPrettyString(Object obj) throws JsonException {
-		StringBuilder sb = new StringBuilder();
-		toString(obj,sb,0);
-		sb.append('\n');
-		return sb.toString();
-	}
-
-	private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException {
-		if( obj instanceof List ) {
-			toString((List)obj,sb,indented);
-			return;
-		}
-		if( obj instanceof Map ) {
-			toString((Map)obj,sb,indented);
-			return;
-		}
-		toString(obj,sb);
-	}
-
-	private static void indent(StringBuilder sb,int indented) {
-		sb.append('\n');
-		for( int i=0; i<indented; i++ ) {
-			sb.append('\t');
-		}
-	}
-
 	private static void toString(List list,StringBuilder sb,int indented) {
 		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);
+				sb.append(", ");
 				toString(list.get(i),sb,indented+1);
 			}
 			indent(sb,indented);
@@ -175,6 +113,12 @@
 		toString(entry.getValue(),sb,indented);
 	}
 
+	private static void indent(StringBuilder sb,int indented) {
+		sb.append('\n');
+		for( int i=0; i<indented; i++ ) {
+			sb.append('\t');
+		}
+	}
 
 	private JsonToString() {}  // never
 }
--- a/src/luan/modules/Parsers.luan	Fri Aug 04 11:06:08 2017 -0600
+++ b/src/luan/modules/Parsers.luan	Fri Aug 04 11:15:53 2017 -0600
@@ -25,8 +25,4 @@
 	return JsonToString.toString(Luan.toJava(obj))
 end
 
-function Parsers.json_pretty_string(obj)
-	return JsonToString.toPrettyString(Luan.toJava(obj))
-end
-
 return Parsers