List of usage examples for org.json JSONTokener JSONTokener
public JSONTokener(String s)
From source file:com.petercassetta.utils.Bundle.java
public static Bundle read(InputStream stream) { try {/*w w w. ja v a2 s.c o m*/ BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder all = new StringBuilder(); String line = reader.readLine(); while (line != null) { all.append(line); line = reader.readLine(); } JSONObject json = (JSONObject) new JSONTokener(all.toString()).nextValue(); reader.close(); return new Bundle(json); } catch (Exception e) { return null; } }
From source file:org.sleeksnap.impl.Language.java
/** * Load and merge the tokens from a language * /*from ww w . ja v a 2 s. co m*/ * @param lang * The language to load * @throws IOException * If an error occurs, or if it's an invalid language file */ @SuppressWarnings("unchecked") public static void loadAndMerge(final String lang) throws IOException { final URL url = Util.getResourceByName("/languages/" + lang + ".json"); if (url != null) { final InputStream input = url.openStream(); try { final JSONObject obj = new JSONObject(new JSONTokener(input)); final JSONObject tokenObj = obj.getJSONObject("tokens"); for (final String s : (Set<String>) tokenObj.keySet()) { tokens.put(s, tokenObj.getString(s)); } } finally { input.close(); } } else { throw new IOException("Invalid language " + lang); } }
From source file:ai.susi.mind.SusiMind.java
public JSONObject readJsonLesson(File file) throws JSONException, FileNotFoundException { JSONObject json = new JSONObject(new JSONTokener(new FileReader(file))); //System.out.println(json.toString(2)); // debug return json;//from ww w. j av a2 s . c o m }
From source file:ai.susi.mind.SusiMind.java
/** * read an "EzD" ('Easy Dialog') file: this is just a text file. Read the docs/susi_skill_development_tutorial.md for an explanation * @param br//from w w w . ja v a2 s . com * @return * @throws JSONException * @throws FileNotFoundException */ public JSONObject readSkills(BufferedReader br) throws JSONException { // read the text file and turn it into a skill json; then learn that JSONObject json = new JSONObject(); JSONArray skills = new JSONArray(); json.put("skills", skills); String lastLine = "", line = ""; String bang_phrases = "", bang_type = "", bang_term = ""; StringBuilder bang_bag = new StringBuilder(); boolean prior = false; try { readloop: while ((line = br.readLine()) != null) { line = line.trim(); if (bang_type.length() > 0) { // collect a bang if (line.toLowerCase().equals("eol")) { // stop collection if (bang_type.equals("javascript")) { // create a javascript skill JSONObject skill = new JSONObject(true); JSONArray phrases = new JSONArray(); skill.put("phrases", phrases); for (String phrase : bang_phrases.split("\\|")) phrases.put(SusiPhrase.simplePhrase(phrase.trim(), prior)); // javascript process JSONObject process = new JSONObject(); process.put("type", Type.javascript.name()); process.put("expression", bang_bag.toString()); skill.put("process", new JSONArray().put(process)); // answers; must contain $!$ skill.put("actions", new JSONArray().put(SusiAction.answerAction(bang_term.split("\\|")))); skills.put(skill); } if (bang_type.equals("console")) { // create a console skill JSONObject skill = new JSONObject(true); JSONArray phrases = new JSONArray(); skill.put("phrases", phrases); for (String phrase : bang_phrases.split("\\|")) phrases.put(SusiPhrase.simplePhrase(phrase.trim(), prior)); // console process JSONObject process = new JSONObject(); process.put("type", Type.console.name()); process.put("definition", new JSONObject(new JSONTokener(bang_bag.toString()))); skill.put("process", new JSONArray().put(process)); // answers; must contain names from the console result array skill.put("actions", new JSONArray().put(SusiAction.answerAction(bang_term.split("\\|")))); skills.put(skill); } bang_phrases = ""; bang_type = ""; bang_term = ""; bang_bag.setLength(0); } bang_bag.append(line).append('\n'); continue readloop; } // read metadata if (line.startsWith("::")) { line = line.toLowerCase(); if (line.startsWith("::minor")) prior = false; if (line.startsWith("::prior")) prior = true; lastLine = ""; continue readloop; } if (line.startsWith("#")) { lastLine = ""; continue readloop; } // read content if (line.length() > 0 && lastLine.length() > 0) { // mid of conversation (last answer is query for next skill) String[] phrases = lastLine.split("\\|"); String condition = null; int thenpos = -1; if (line.startsWith("?") && (thenpos = line.indexOf(':')) > 0) { int elsepos = line.substring(thenpos + 1).indexOf(':') + thenpos + 1; condition = line.substring(1, thenpos).trim(); if (elsepos <= thenpos) { // only if, no else String ifsubstring = line.substring(thenpos + 1).trim(); if (ifsubstring.length() > 0) { String[] answers = ifsubstring.split("\\|"); JSONObject skill = SusiSkill.answerSkill(phrases, "IF " + condition, answers, prior); skills.put(skill); } } else { String ifsubstring = line.substring(thenpos + 1, elsepos).trim(); if (ifsubstring.length() > 0) { String[] ifanswers = ifsubstring.split("\\|"); JSONObject skillif = SusiSkill.answerSkill(phrases, "IF " + condition, ifanswers, prior); skills.put(skillif); } String elsesubstring = line.substring(elsepos + 1).trim(); if (elsesubstring.length() > 0) { String[] elseanswers = elsesubstring.split("\\|"); JSONObject skillelse = SusiSkill.answerSkill(phrases, "NOT " + condition, elseanswers, prior); skills.put(skillelse); } } } else if (line.startsWith("!") && (thenpos = line.indexOf(':')) > 0) { bang_phrases = lastLine; bang_type = line.substring(1, thenpos).trim().toLowerCase(); bang_term = line.substring(thenpos + 1).trim(); bang_bag.setLength(0); continue readloop; } else { String[] answers = line.split("\\|"); JSONObject skill = SusiSkill.answerSkill(phrases, condition, answers, prior); //System.out.println(skill.toString()); skills.put(skill); } } lastLine = line; } } catch (IOException e) { } return json; }