comparison core/src/luan/modules/RpcLuan.java @ 758:c29d11d675fd

added Json.toString() and rpc now sends tables as json
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 19 Jul 2016 00:57:37 -0600
parents 1a101ac9ea46
children ae612dfc57cb
comparison
equal deleted inserted replaced
757:e1dfeddfbc7b 758:c29d11d675fd
18 import luan.LuanState; 18 import luan.LuanState;
19 import luan.LuanTable; 19 import luan.LuanTable;
20 import luan.LuanFunction; 20 import luan.LuanFunction;
21 import luan.LuanException; 21 import luan.LuanException;
22 import luan.LuanMethod; 22 import luan.LuanMethod;
23 import luan.modules.parsers.Json;
24 import luan.modules.parsers.ParseException;
23 25
24 26
25 public final class RpcLuan { 27 public final class RpcLuan {
26 private static final int NIL = 0; 28 private static final int NIL = 0;
27 private static final int STRING = 1; 29 private static final int STRING = 1;
142 writeInt(out,a.length); 144 writeInt(out,a.length);
143 out.write(a); 145 out.write(a);
144 } 146 }
145 else if( obj instanceof LuanTable ) { 147 else if( obj instanceof LuanTable ) {
146 out.write(TABLE); 148 out.write(TABLE);
147 String s = pickle( luan, obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); 149 // String s = pickle( luan, obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) );
150 String s = Json.toString(obj);
148 writeString(out,s); 151 writeString(out,s);
149 } 152 }
150 else 153 else
151 throw new LuanException( "invalid type: " + obj.getClass() ); 154 throw new LuanException( "invalid type: " + obj.getClass() );
152 } 155 }
164 return Double.valueOf(readString(in)); 167 return Double.valueOf(readString(in));
165 case BINARY: 168 case BINARY:
166 return readBinary(in,readInt(in)); 169 return readBinary(in,readInt(in));
167 case TABLE: 170 case TABLE:
168 String s = readString(in); 171 String s = readString(in);
172 /*
169 LuanFunction fn = Luan.load("return "+s,"rpc-reader"); 173 LuanFunction fn = Luan.load("return "+s,"rpc-reader");
170 return fn.call(luan); 174 return fn.call(luan);
175 */
176 try {
177 return Json.parse(s);
178 } catch(ParseException e) {
179 throw new LuanException(e);
180 }
171 case IO: 181 case IO:
172 return new LuanInputStream(in,close).table(); 182 return new LuanInputStream(in,close).table();
173 default: 183 default:
174 throw new LuanException( "invalid type: " + type ); 184 throw new LuanException( "invalid type: " + type );
175 } 185 }
222 out.write((v >>> 16) & 0xFF); 232 out.write((v >>> 16) & 0xFF);
223 out.write((v >>> 8) & 0xFF); 233 out.write((v >>> 8) & 0xFF);
224 out.write((v >>> 0) & 0xFF); 234 out.write((v >>> 0) & 0xFF);
225 } 235 }
226 236
227 237 /*
228 private static String pickle(LuanState luan,Object obj,Set<LuanTable> set) throws LuanException { 238 private static String pickle(LuanState luan,Object obj,Set<LuanTable> set) throws LuanException {
229 if( obj == null ) 239 if( obj == null )
230 return "nil"; 240 return "nil";
231 if( obj instanceof Boolean ) 241 if( obj instanceof Boolean )
232 return obj.toString(); 242 return obj.toString();
251 sb.append( "}" ); 261 sb.append( "}" );
252 return sb.toString(); 262 return sb.toString();
253 } 263 }
254 throw new LuanException( "invalid type: " + obj.getClass() ); 264 throw new LuanException( "invalid type: " + obj.getClass() );
255 } 265 }
256 266 */
257 267
258 private static class Close { 268 private static class Close {
259 boolean b = true; 269 boolean b = true;
260 } 270 }
261 271