changeset 1518:f989778ca9e1

stringify, json_string
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 19 Jun 2020 17:50:25 -0600
parents 04615093b19d
children 3ebf9781707c
files src/goodjava/json/JsonToString.java src/luan/modules/parsers/LuanToString.java
diffstat 2 files changed, 15 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
diff -r 04615093b19d -r f989778ca9e1 src/goodjava/json/JsonToString.java
--- a/src/goodjava/json/JsonToString.java	Tue Jun 09 12:36:16 2020 -0600
+++ b/src/goodjava/json/JsonToString.java	Fri Jun 19 17:50:25 2020 -0600
@@ -8,9 +8,6 @@
 public final class JsonToString {
 	public boolean compressed = false;
 
-	private String colon;
-	private String comma;
-
 	public static final class JsonException extends RuntimeException {
 		private JsonException(String msg) {
 			super(msg);
@@ -18,8 +15,6 @@
 	}
 
 	public String toString(Object obj) throws JsonException {
-		colon = compressed ? ":" : ": ";
-		comma = compressed ? "," : ", ";
 		StringBuilder sb = new StringBuilder();
 		toString(obj,sb,0);
 		if( !compressed )
@@ -123,7 +118,8 @@
 			toString(list.get(0),sb,indented+1);
 			final int n = list.size();
 			for( int i=1; i<n; i++ ) {
-				sb.append(comma);
+				sb.append(',');
+				indent(sb,indented+1);
 				toString(list.get(i),sb,indented+1);
 			}
 			indent(sb,indented);
@@ -153,7 +149,7 @@
 		if( !(key instanceof String) )
 			throw new JsonException("table keys must be strings but got "+key.getClass().getSimpleName()+" ("+key+"="+entry.getValue()+")");
 		toString((String)key,sb);
-		sb.append(colon);
+		sb.append( compressed ? ":" : ": " );
 		toString(entry.getValue(),sb,indented);
 	}
 
diff -r 04615093b19d -r f989778ca9e1 src/luan/modules/parsers/LuanToString.java
--- a/src/luan/modules/parsers/LuanToString.java	Tue Jun 09 12:36:16 2020 -0600
+++ b/src/luan/modules/parsers/LuanToString.java	Fri Jun 19 17:50:25 2020 -0600
@@ -13,12 +13,7 @@
 	public boolean numberTypes = false;
 	public boolean compressed = false;
 
-	private String equal;
-	private String comma;
-
 	public String toString(Object obj) throws LuanException {
-		equal = compressed ? "=" : " = ";
-		comma = compressed ? "," : ", ";
 		StringBuilder sb = new StringBuilder();
 		toString(obj,sb,0);
 		return sb.toString();
@@ -58,23 +53,24 @@
 		List list = tbl.asList();
 		Map map = tbl.rawMap();
 		sb.append( '{' );
-		if( !list.isEmpty() ) {
+		boolean first = true;
+		for( Object obj : list ) {
 			if( !compressed )
 				indent(sb,indented+1);
-			toString(list.get(0),sb,indented+1);
-			final int n = list.size();
-			for( int i=1; i<n; i++ ) {
-				sb.append(comma);
-				toString(list.get(i),sb,indented+1);
-			}
+			else if( first )
+				first = false;
+			else
+				sb.append( ',' );
+			toString(obj,sb,indented+1);
 		}
-		boolean first = true;
 		for( Object obj : map.entrySet() ) {
 			Map.Entry entry = (Map.Entry)obj;
-			if( compressed && first )
+			if( !compressed )
+				indent(sb,indented+1);
+			else if( first )
 				first = false;
 			else
-				indent(sb,indented+1);
+				sb.append( ',' );
 			toString(entry,sb,indented+1);
 		}
 		if( !compressed && (!list.isEmpty() || !map.isEmpty()) )
@@ -92,15 +88,11 @@
 			toString( key, sb, indented );
 			sb.append( ']' );
 		}
-		sb.append( equal );
+		sb.append( compressed ? "=" : " = " );
 		toString( entry.getValue(), sb, indented );
 	}
 
 	private void indent(StringBuilder sb,int indented) {
-		if( compressed ) {
-			sb.append( comma );
-			return;
-		}
 		sb.append( '\n' );
 		for( int i=0; i<indented; i++ ) {
 			sb.append( '\t' );