changeset 1419:59fd2e8b1b9d

stringify and json_string
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 25 Oct 2019 22:12:06 -0600
parents 732b5de211fc
children 225808b90cee
files conv.txt src/goodjava/json/JsonToString.java src/goodjava/rpc/RpcCon.java src/luan/modules/BasicLuan.java src/luan/modules/Parsers.luan src/luan/modules/parsers/LuanToString.java
diffstat 6 files changed, 61 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Fri Oct 18 22:29:46 2019 -0600
+++ b/conv.txt	Fri Oct 25 22:12:06 2019 -0600
@@ -1,3 +1,5 @@
+JsonToString
+
 Hosting.send_mail
 Hosting.luan
 
--- a/src/goodjava/json/JsonToString.java	Fri Oct 18 22:29:46 2019 -0600
+++ b/src/goodjava/json/JsonToString.java	Fri Oct 25 22:12:06 2019 -0600
@@ -5,7 +5,11 @@
 import java.util.Iterator;
 
 
-public class JsonToString {
+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) {
@@ -13,19 +17,13 @@
 		}
 	}
 
-	public static String toString(Object obj) throws JsonException {
+	public String toString(Object obj) throws JsonException {
+		colon = compressed ? ":" : ": ";
+		comma = compressed ? "," : ", ";
 		StringBuilder sb = new StringBuilder();
-		new JsonToString().toString(obj,sb,0);
-		sb.append('\n');
-		return sb.toString();
-	}
-
-	public static String toCompressedString(Object obj) throws JsonException {
-		StringBuilder sb = new StringBuilder();
-		JsonToString jts = new JsonToString() {
-			void indent(StringBuilder sb,int indented) {}
-		};
-		jts.toString(obj,sb,0);
+		toString(obj,sb,0);
+		if( !compressed )
+			sb.append('\n');
 		return sb.toString();
 	}
 
@@ -124,7 +122,7 @@
 			indent(sb,indented+1);
 			toString(list.get(0),sb,indented+1);
 			for( int i=1; i<list.size(); i++ ) {
-				sb.append(", ");
+				sb.append(comma);
 				toString(list.get(i),sb,indented+1);
 			}
 			indent(sb,indented);
@@ -154,16 +152,17 @@
 		if( !(key instanceof String) )
 			throw new JsonException("table keys must be strings");
 		toString((String)key,sb);
-		sb.append(": ");
+		sb.append(colon);
 		toString(entry.getValue(),sb,indented);
 	}
 
-	void indent(StringBuilder sb,int indented) {
+	private void indent(StringBuilder sb,int indented) {
+		if( compressed )
+			return;
 		sb.append('\n');
 		for( int i=0; i<indented; i++ ) {
 			sb.append('\t');
 		}
 	}
 
-	private JsonToString() {}
 }
--- a/src/goodjava/rpc/RpcCon.java	Fri Oct 18 22:29:46 2019 -0600
+++ b/src/goodjava/rpc/RpcCon.java	Fri Oct 25 22:12:06 2019 -0600
@@ -52,7 +52,7 @@
 	{
 		if( in != null )
 			list.add(0,lenIn);
-		String json = JsonToString.toString(list);
+		String json = new JsonToString().toString(list);
 		byte[] aJson = json.getBytes(StandardCharsets.UTF_8);
 		int len = aJson.length;
 		byte[] a = new byte[4+len];
--- a/src/luan/modules/BasicLuan.java	Fri Oct 18 22:29:46 2019 -0600
+++ b/src/luan/modules/BasicLuan.java	Fri Oct 25 22:12:06 2019 -0600
@@ -8,6 +8,7 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Arrays;
+import goodjava.json.JsonToString;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanFunction;
@@ -265,10 +266,25 @@
 			Boolean numberTypes = Utils.removeBoolean(map,"number_types");
 			if( numberTypes != null )
 				lts.numberTypes = numberTypes;
+			Boolean compressed = Utils.removeBoolean(map,"compressed");
+			if( compressed != null )
+				lts.compressed = compressed;
 			Utils.checkEmpty(map);
 		}
 		return lts.toString(obj);
 	}
 
+	public static String json_string(Object obj,LuanTable options) throws LuanException {
+		JsonToString jts = new JsonToString();
+		if( options != null ) {
+			Map map = options.asMap();
+			Boolean compressed = Utils.removeBoolean(map,"compressed");
+			if( compressed != null )
+				jts.compressed = compressed;
+			Utils.checkEmpty(map);
+		}
+		return jts.toString(Luan.toJava(obj));
+	}
+
 	private void BasicLuan() {}  // never
 }
--- a/src/luan/modules/Parsers.luan	Fri Oct 18 22:29:46 2019 -0600
+++ b/src/luan/modules/Parsers.luan	Fri Oct 25 22:12:06 2019 -0600
@@ -2,6 +2,7 @@
 local BBCode = require "java:luan.modules.parsers.BBCode"
 local Csv = require "java:luan.modules.parsers.Csv"
 local Theme = require "java:luan.modules.parsers.Theme"
+local BasicLuan = require "java:luan.modules.BasicLuan"
 
 
 local Parsers = {}
@@ -9,6 +10,7 @@
 Parsers.bbcode_to_html = BBCode.toHtml
 Parsers.bbcode_to_text = BBCode.toText
 Parsers.csv_to_list = Csv.toList
+Parsers.json_string = BasicLuan.json_string
 Parsers.theme_to_luan = Theme.toLuan
 
 local Luan = require "luan:Luan.luan"
@@ -16,9 +18,7 @@
 local type = Luan.type or error()
 local Table = require "luan:Table.luan"
 local java_to_table_deep = Table.java_to_table_deep or error()
-local LuanJava = require "java:luan.Luan"
 local JsonParser = require "java:goodjava.json.JsonParser"
-local JsonToString = require "java:goodjava.json.JsonToString"
 
 -- converts json string to luan object
 function Parsers.json_parse(s)
@@ -26,13 +26,9 @@
 	return type(obj)=="java" and java_to_table_deep(obj) or obj
 end
 
--- converts luan object to json string
-function Parsers.json_string(obj)
-	return JsonToString.toString(LuanJava.toJava(obj))
-end
-
+-- tmp
 function Parsers.json_compressed_string(obj)
-	return JsonToString.toCompressedString(LuanJava.toJava(obj))
+	return Parsers.json_string(obj,{compressed=true})
 end
 
 return Parsers
--- 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' );