diff src/luan/modules/parsers/LuanToString.java @ 1419:59fd2e8b1b9d

stringify and json_string
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 25 Oct 2019 22:12:06 -0600
parents 9103a61e64bc
children 6a24c8b33d6b
line wrap: on
line diff
--- a/src/luan/modules/parsers/LuanToString.java	Fri Oct 18 22:29:46 2019 -0600
+++ b/src/luan/modules/parsers/LuanToString.java	Fri Oct 25 22:12:06 2019 -0600
@@ -11,8 +11,14 @@
 public final class LuanToString {
 	public boolean strict = false;
 	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();
@@ -53,18 +59,25 @@
 		Map map = tbl.rawMap();
 		sb.append( '{' );
 		if( !list.isEmpty() ) {
-			indent(sb,indented+1);
+			if( !compressed )
+				indent(sb,indented+1);
 			for( Object obj : list ) {
 				toString(obj,sb,indented+1);
-				sb.append( ", " );
+				sb.append( comma );
 			}
+			if( compressed )
+				sb.setLength(sb.length()-1);
 		}
+		boolean first = true;
 		for( Object obj : map.entrySet() ) {
 			Map.Entry entry = (Map.Entry)obj;
-			indent(sb,indented+1);
+			if( compressed && first )
+				first = false;
+			else
+				indent(sb,indented+1);
 			toString(entry,sb,indented+1);
 		}
-		if( !list.isEmpty() || !map.isEmpty() )
+		if( !compressed && (!list.isEmpty() || !map.isEmpty()) )
 			indent(sb,indented);
 		sb.append( '}' );
 		return;
@@ -79,11 +92,15 @@
 			toString( key, sb, indented );
 			sb.append( ']' );
 		}
-		sb.append( " = " );
+		sb.append( equal );
 		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' );