changeset 1113:22652f4020fb

add JsonToString
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 02 Aug 2017 19:00:24 -0600
parents 490f77bb2ad1
children 540e1940170f
files src/luan/Luan.java src/luan/LuanTable.java src/luan/lib/json/JsonToString.java src/luan/modules/BasicLuan.java src/luan/modules/Parsers.luan src/luan/modules/RpcLuan.java src/luan/modules/parsers/Json.java
diffstat 7 files changed, 210 insertions(+), 176 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/Luan.java	Wed Aug 02 17:37:59 2017 -0600
+++ b/src/luan/Luan.java	Wed Aug 02 19:00:24 2017 -0600
@@ -1,6 +1,12 @@
 package luan;
 
 import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.LinkedHashMap;
+import java.util.Set;
+import java.util.Arrays;
+import java.util.Iterator;
 import luan.modules.BasicLuan;
 import luan.impl.LuanCompiler;
 
@@ -153,5 +159,70 @@
 	}
 
 
+	public static Object toLuan(Object obj) throws LuanException {
+		if( !type(obj).equals("java") )
+			return obj;
+		LuanTable tbl = new LuanTable();
+		if( obj instanceof Map ) {
+			Map map = (Map)obj;
+			for( Object stupid : map.entrySet() ) {
+				Map.Entry entry = (Map.Entry)stupid;
+				Object key = entry.getKey();
+				Object value = entry.getValue();
+				if( key != null && value != null )
+					tbl.rawPut(toLuan(key),toLuan(value));
+			}
+			return tbl;
+		}
+		if( obj instanceof Set ) {
+			Set set = (Set)obj;
+			for( Object el : set ) {
+				if( el != null )
+					tbl.rawPut(toLuan(el),Boolean.TRUE);
+			}
+			return tbl;
+		}
+		List list;
+		if( obj instanceof List ) {
+			list = (List)obj;
+		} else {
+			Class cls = obj.getClass();
+			if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
+				Object[] a = (Object[])obj;
+				list = Arrays.asList(a);
+			} else
+				throw new LuanException("can't convert type "+obj.getClass().getName()+" to luan");
+		}
+		int n = list.size();
+		for( int i=0; i<n; i++ ) {
+			Object val = list.get(i);
+			if( val != null )
+				tbl.rawPut(i+1,toLuan(val));
+		}
+		return tbl;
+	}
+
+	public static Object toJava(Object obj) throws LuanException {
+		if( !(obj instanceof LuanTable) )
+			return obj;
+		LuanTable tbl = (LuanTable)obj;
+		if( tbl.isList() ) {
+			List list = new ArrayList();
+			for( Object el : tbl.asList() ) {
+				list.add( toJava(el) );
+			}
+			return list;
+		} else {
+			Map map = new LinkedHashMap();
+			Iterator<Map.Entry> iter = tbl.rawIterator();
+			while( iter.hasNext() ) {
+				Map.Entry entry = iter.next();
+				map.put( toJava(entry.getKey()), toJava(entry.getValue()) );
+			}
+			return map;
+		}
+	}
+
+
 	private Luan() {}  // never
 }
--- a/src/luan/LuanTable.java	Wed Aug 02 17:37:59 2017 -0600
+++ b/src/luan/LuanTable.java	Wed Aug 02 19:00:24 2017 -0600
@@ -276,29 +276,29 @@
 		return list==null ? 0 : list.size();
 	}
 
-	public Iterable<Map.Entry<Object,Object>> iterable(LuanState luan) throws LuanException {
-		final Iterator<Map.Entry<Object,Object>> iter = iterator(luan);
-		return new Iterable<Map.Entry<Object,Object>>() {
-			public Iterator<Map.Entry<Object,Object>> iterator() {
+	public Iterable<Map.Entry> iterable(LuanState luan) throws LuanException {
+		final Iterator<Map.Entry> iter = iterator(luan);
+		return new Iterable<Map.Entry>() {
+			public Iterator<Map.Entry> iterator() {
 				return iter;
 			}
 		};
 	}
 
-	public Iterable<Map.Entry<Object,Object>> rawIterable() throws LuanException {
-		final Iterator<Map.Entry<Object,Object>> iter = rawIterator();
-		return new Iterable<Map.Entry<Object,Object>>() {
-			public Iterator<Map.Entry<Object,Object>> iterator() {
+	public Iterable<Map.Entry> rawIterable() throws LuanException {
+		final Iterator<Map.Entry> iter = rawIterator();
+		return new Iterable<Map.Entry>() {
+			public Iterator<Map.Entry> iterator() {
 				return iter;
 			}
 		};
 	}
 
-	public Iterator<Map.Entry<Object,Object>> iterator(final LuanState luan) throws LuanException {
+	public Iterator<Map.Entry> iterator(final LuanState luan) throws LuanException {
 		if( getHandler("__pairs") == null )
 			return rawIterator();
 		final LuanFunction fn = pairs(luan);
-		return new Iterator<Map.Entry<Object,Object>>() {
+		return new Iterator<Map.Entry>() {
 			private Map.Entry<Object,Object> next = getNext();
 
 			private Map.Entry<Object,Object> getNext() {
@@ -348,7 +348,7 @@
 
 	private LuanFunction rawPairs() {
 		return new LuanFunction() {
-			final Iterator<Map.Entry<Object,Object>> iter = rawIterator();
+			final Iterator<Map.Entry> iter = rawIterator();
 
 			@Override public Object[] call(LuanState luan,Object[] args) {
 				if( !iter.hasNext() )
@@ -359,17 +359,17 @@
 		};
 	}
 
-	public Iterator<Map.Entry<Object,Object>> rawIterator() {
+	public Iterator<Map.Entry> rawIterator() {
 		check();
 		if( list == null ) {
 			if( map == null )
-				return Collections.<Map.Entry<Object,Object>>emptyList().iterator();
+				return Collections.<Map.Entry>emptyList().iterator();
 			return map.entrySet().iterator();
 		}
 		if( map == null )
 			return listIterator();
-		return new Iterator<Map.Entry<Object,Object>>() {
-			Iterator<Map.Entry<Object,Object>> iter = listIterator();
+		return new Iterator<Map.Entry>() {
+			Iterator<Map.Entry> iter = listIterator();
 			boolean isList = true;
 
 			public boolean hasNext() {
@@ -392,11 +392,11 @@
 		};
 	}
 
-	private Iterator<Map.Entry<Object,Object>> listIterator() {
+	private Iterator<Map.Entry> listIterator() {
 		if( list == null )
-			return Collections.<Map.Entry<Object,Object>>emptyList().iterator();
+			return Collections.<Map.Entry>emptyList().iterator();
 		final ListIterator iter = list.listIterator();
-		return new Iterator<Map.Entry<Object,Object>>() {
+		return new Iterator<Map.Entry>() {
 
 			public boolean hasNext() {
 				return iter.hasNext();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/json/JsonToString.java	Wed Aug 02 19:00:24 2017 -0600
@@ -0,0 +1,110 @@
+package luan.lib.json;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Iterator;
+
+
+public final class JsonToString {
+
+	public static final class JsonException extends RuntimeException {
+		private JsonException(String msg) {
+			super(msg);
+		}
+	}
+
+	public static String toString(Object obj) throws JsonException {
+		StringBuilder sb = new StringBuilder();
+		toString(obj,sb);
+		return sb.toString();
+	}
+
+	private static void toString(Object obj,StringBuilder sb) throws JsonException {
+		if( obj == null || obj instanceof Boolean || obj instanceof Number ) {
+			sb.append(obj);
+			return;
+		}
+		if( obj instanceof String ) {
+			toString((String)obj,sb);
+			return;
+		}
+		if( obj instanceof List ) {
+			toString((List)obj,sb);
+			return;
+		}
+		if( obj instanceof Map ) {
+			toString((Map)obj,sb);
+			return;
+		}
+		throw new JsonException("can't handle type "+obj.getClass().getName());
+	}
+
+	private static void toString(final String s,StringBuilder sb) {
+		sb.append('"');
+		for( int i=0; i<s.length(); i++ ) {
+			char c = s.charAt(i);
+			switch(c) {
+			case '"':
+				sb.append("\\\"");
+				break;
+			case '\\':
+				sb.append("\\\\");
+				break;
+			case '\b':
+				sb.append("\\b");
+				break;
+			case '\f':
+				sb.append("\\f");
+				break;
+			case '\n':
+				sb.append("\\n");
+				break;
+			case '\r':
+				sb.append("\\r");
+				break;
+			case '\t':
+				sb.append("\\t");
+				break;
+			default:
+				sb.append(c);
+			}
+		}
+		sb.append('"');
+	}
+
+	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);
+		}
+		sb.append(']');
+		return;
+	}
+
+	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(',');
+			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);
+	}
+
+}
--- a/src/luan/modules/BasicLuan.java	Wed Aug 02 17:37:59 2017 -0600
+++ b/src/luan/modules/BasicLuan.java	Wed Aug 02 19:00:24 2017 -0600
@@ -7,8 +7,6 @@
 import java.util.Map;
 import java.util.List;
 import java.util.ArrayList;
-import java.util.Set;
-import java.util.Arrays;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -250,47 +248,4 @@
 		}
 	}
 
-	public static Object to_luan(Object obj) throws LuanException {
-		if( !type(obj).equals("java") )
-			return obj;
-		LuanTable tbl = new LuanTable();
-		if( obj instanceof Map ) {
-			Map map = (Map)obj;
-			for( Object stupid : map.entrySet() ) {
-				Map.Entry entry = (Map.Entry)stupid;
-				Object key = entry.getKey();
-				Object value = entry.getValue();
-				if( key != null && value != null )
-					tbl.rawPut(to_luan(key),to_luan(value));
-			}
-			return tbl;
-		}
-		if( obj instanceof Set ) {
-			Set set = (Set)obj;
-			for( Object el : set ) {
-				if( el != null )
-					tbl.rawPut(to_luan(el),Boolean.TRUE);
-			}
-			return tbl;
-		}
-		List list;
-		if( obj instanceof List ) {
-			list = (List)obj;
-		} else {
-			Class cls = obj.getClass();
-			if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
-				Object[] a = (Object[])obj;
-				list = Arrays.asList(a);
-			} else
-				throw new LuanException("can't convert type "+obj.getClass().getName()+" to luan");
-		}
-		int n = list.size();
-		for( int i=0; i<n; i++ ) {
-			Object val = list.get(i);
-			if( val != null )
-				tbl.rawPut(i+1,to_luan(val));
-		}
-		return tbl;
-	}
-
 }
--- a/src/luan/modules/Parsers.luan	Wed Aug 02 17:37:59 2017 -0600
+++ b/src/luan/modules/Parsers.luan	Wed Aug 02 19:00:24 2017 -0600
@@ -2,7 +2,6 @@
 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 Json = require "java:luan.modules.parsers.Json"
 
 
 local Parsers = {}
@@ -10,15 +9,20 @@
 Parsers.bbcode_to_html = BBCode.toHtml
 Parsers.bbcode_to_text = BBCode.toText
 Parsers.csv_to_list = Csv.toList
-Parsers.json_string = Json.toString  -- converts luan object to json string
 Parsers.theme_to_luan = Theme.toLuan
 
+local Luan = require "java:luan.Luan"
 local JsonParser = require "java:luan.lib.json.JsonParser"
-local BasicLuan = require "java:luan.modules.BasicLuan"
+local JsonToString = require "java:luan.lib.json.JsonToString"
 
 -- converts json string to luan object
 function Parsers.json_parse(obj)
-	return BasicLuan.to_luan(JsonParser.parse(obj))
+	return Luan.toLuan(JsonParser.parse(obj))
+end
+
+-- converts luan object to json string
+function Parsers.json_string(obj)
+	return JsonToString.toString(Luan.toJava(obj))
 end
 
 return Parsers
--- a/src/luan/modules/RpcLuan.java	Wed Aug 02 17:37:59 2017 -0600
+++ b/src/luan/modules/RpcLuan.java	Wed Aug 02 19:00:24 2017 -0600
@@ -20,7 +20,7 @@
 import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanMethod;
-import luan.modules.parsers.Json;
+import luan.lib.json.JsonToString;
 import luan.lib.json.JsonParser;
 import luan.lib.parser.ParseException;
 
@@ -153,7 +153,7 @@
 		else if( obj instanceof LuanTable ) {
 			out.write(TABLE);
 //			String s = pickle( luan, obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) );
-			String s = Json.toString(obj);
+			String s = JsonToString.toString(Luan.toJava(obj));
 			writeString(out,s);
 		}
 		else
@@ -182,7 +182,7 @@
 			return fn.call(luan);
 */
 			try {
-				return BasicLuan.to_luan(JsonParser.parse(s));
+				return Luan.toLuan(JsonParser.parse(s));
 			} catch(ParseException e) {
 				throw new LuanException(e);
 			}
--- a/src/luan/modules/parsers/Json.java	Wed Aug 02 17:37:59 2017 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,106 +0,0 @@
-package luan.modules.parsers;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.LinkedHashMap;
-import java.util.Iterator;
-import luan.LuanTable;
-import luan.LuanException;
-import luan.lib.parser.Parser;
-import luan.lib.parser.ParseException;
-
-
-public final class Json {
-
-	public static String toString(Object obj) throws LuanException {
-		StringBuilder sb = new StringBuilder();
-		toString(obj,sb);
-		return sb.toString();
-	}
-
-	private static void toString(Object obj,StringBuilder sb) throws LuanException {
-		if( obj == null || obj instanceof Boolean || obj instanceof Number ) {
-			sb.append(obj);
-			return;
-		}
-		if( obj instanceof String ) {
-			toString((String)obj,sb);
-			return;
-		}
-		if( obj instanceof LuanTable ) {
-			toString((LuanTable)obj,sb);
-			return;
-		}
-		throw new LuanException("can't handle type "+obj.getClass().getName());
-	}
-
-	private static void toString(final String s,StringBuilder sb) {
-		sb.append('"');
-		for( int i=0; i<s.length(); i++ ) {
-			char c = s.charAt(i);
-			switch(c) {
-			case '"':
-				sb.append("\\\"");
-				break;
-			case '\\':
-				sb.append("\\\\");
-				break;
-			case '\b':
-				sb.append("\\b");
-				break;
-			case '\f':
-				sb.append("\\f");
-				break;
-			case '\n':
-				sb.append("\\n");
-				break;
-			case '\r':
-				sb.append("\\r");
-				break;
-			case '\t':
-				sb.append("\\t");
-				break;
-			default:
-				sb.append(c);
-			}
-		}
-		sb.append('"');
-	}
-
-	private static void toString(LuanTable t,StringBuilder sb) throws LuanException {
-		if( t.isList() ) {
-			final List list = t.asList();
-			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);
-			}
-			sb.append(']');
-			return;
-		}
-		sb.append('{');
-		Iterator<Map.Entry<Object,Object>> i = t.rawIterator();
-		toString(i.next(),sb);
-		while( i.hasNext() ) {
-			sb.append(',');
-			toString(i.next(),sb);
-		}
-		sb.append('}');
-	}
-
-	private static void toString(Map.Entry<Object,Object> entry,StringBuilder sb) throws LuanException {
-		Object key = entry.getKey();
-		if( !(key instanceof String) )
-			throw new LuanException("table keys must be strings");
-		toString((String)key,sb);
-		sb.append(':');
-		toString(entry.getValue(),sb);
-	}
-
-}