static private Map<String, ?> parseDict(String string){ Parser parser = new Parser(); Ast ast = parser.parse(string); return (Map<String, ?>)ast.getData(); }
/** * get the actual data as Java objects. * @param dictConverter object to convert dicts to actual instances for a class, * instead of leaving them as dictionaries. Requires the __class__ key to be present * in the dict node. If it returns null, the normal processing is done. */ public Object getData(IDictToInstance dictConverter) { ObjectifyVisitor v = new ObjectifyVisitor(dictConverter); this.accept(v); return v.getObject(); }
/** * Parse from a string with the Python literal expression */ public Ast parse(String expression) { Ast ast=new Ast(); if(expression==null || expression.length()==0) return ast; SeekableStringReader sr = new SeekableStringReader(expression); if(sr.peek()=='#') sr.readUntil('\n'); // skip comment line try { ast.root = parseExpr(sr); sr.skipWhitespace(); if(sr.hasMore()) throw new ParseException("garbage at end of expression"); return ast; } catch (ParseException x) { String faultLocation = extractFaultLocation(sr); throw new ParseException(x.getMessage() + " (at position "+sr.bookmark()+"; '"+faultLocation+"')", x); } }
@Override public Object deserializeData(byte[] data) throws IOException { Parser p = new Parser(); Ast ast = p.parse(data); IDictToInstance dictConverter = new DictConverter(); return ast.getData(dictConverter); }
/** * get the actual data as Java objects. */ public Object getData() { ObjectifyVisitor v = new ObjectifyVisitor(); this.accept(v); return v.getObject(); }
@Override public Object deserializeData(byte[] data) throws IOException { Parser p = new Parser(); Ast ast = p.parse(data); IDictToInstance dictConverter = new DictConverter(); return ast.getData(dictConverter); }