comparison 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
comparison
equal deleted inserted replaced
1111:88b5b81cad4a 1112:490f77bb2ad1
5 import java.lang.reflect.Method; 5 import java.lang.reflect.Method;
6 import java.util.Iterator; 6 import java.util.Iterator;
7 import java.util.Map; 7 import java.util.Map;
8 import java.util.List; 8 import java.util.List;
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Set;
11 import java.util.Arrays;
10 import luan.Luan; 12 import luan.Luan;
11 import luan.LuanState; 13 import luan.LuanState;
12 import luan.LuanTable; 14 import luan.LuanTable;
13 import luan.LuanFunction; 15 import luan.LuanFunction;
14 import luan.LuanException; 16 import luan.LuanException;
246 } else { 248 } else {
247 return obj.hashCode(); 249 return obj.hashCode();
248 } 250 }
249 } 251 }
250 252
253 public static Object to_luan(Object obj) throws LuanException {
254 if( !type(obj).equals("java") )
255 return obj;
256 LuanTable tbl = new LuanTable();
257 if( obj instanceof Map ) {
258 Map map = (Map)obj;
259 for( Object stupid : map.entrySet() ) {
260 Map.Entry entry = (Map.Entry)stupid;
261 Object key = entry.getKey();
262 Object value = entry.getValue();
263 if( key != null && value != null )
264 tbl.rawPut(to_luan(key),to_luan(value));
265 }
266 return tbl;
267 }
268 if( obj instanceof Set ) {
269 Set set = (Set)obj;
270 for( Object el : set ) {
271 if( el != null )
272 tbl.rawPut(to_luan(el),Boolean.TRUE);
273 }
274 return tbl;
275 }
276 List list;
277 if( obj instanceof List ) {
278 list = (List)obj;
279 } else {
280 Class cls = obj.getClass();
281 if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
282 Object[] a = (Object[])obj;
283 list = Arrays.asList(a);
284 } else
285 throw new LuanException("can't convert type "+obj.getClass().getName()+" to luan");
286 }
287 int n = list.size();
288 for( int i=0; i<n; i++ ) {
289 Object val = list.get(i);
290 if( val != null )
291 tbl.rawPut(i+1,to_luan(val));
292 }
293 return tbl;
294 }
295
251 } 296 }