Mercurial Hosting > luan
diff src/luan/modules/BasicLuan.java @ 1112:490f77bb2ad1
add JsonParser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Aug 2017 17:37:59 -0600 |
parents | 2443152dc2f1 |
children | 22652f4020fb |
line wrap: on
line diff
--- a/src/luan/modules/BasicLuan.java Wed Aug 02 15:19:47 2017 -0600 +++ b/src/luan/modules/BasicLuan.java Wed Aug 02 17:37:59 2017 -0600 @@ -7,6 +7,8 @@ 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; @@ -248,4 +250,47 @@ } } + 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; + } + }