List of usage examples for org.json.simple JSONObject writeJSONString
public void writeJSONString(Writer out) throws IOException
From source file:naftoreiclag.villagefive.SaveLoad.java
public static void save(World data) throws IOException { JSONObject obj = new JSONObject(); obj.put("world", data); FileWriter fw = new FileWriter(new File("saves/save.json")); obj.writeJSONString(fw); fw.flush();/*from w ww . ja v a 2 s.co m*/ }
From source file:com.raphfrk.craftproxyclient.json.JSONManager.java
public static byte[] JSONToBytes(JSONObject obj) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter wos = new OutputStreamWriter(bos, StandardCharsets.UTF_8); BufferedWriter br = new BufferedWriter(wos); try {/*from w w w. j a v a 2s. c om*/ obj.writeJSONString(br); br.close(); } catch (IOException e) { return null; } return bos.toByteArray(); }
From source file:component.Configuration.java
public static void createConfigFile() throws IOException { JSONObject config = new JSONObject(); List<String> queryList = new ArrayList<>(); Map<String, List<String>> queries = new HashMap<>(); try (Writer writer = new FileWriter("config.json", false)) { queryList.add("full_message:(Exception)"); queryList.add("full_message:(Error)"); config.put("queries", queryList); config.put("range", 3600); config.writeJSONString(writer); writer.flush();/*from w ww. j av a2 s . com*/ } }
From source file:com.github.lgi2p.obirs.utils.JSONConverter.java
public static String buildJSONString(Set<Autocompletion_Trie.AutocompleteElement> autocomplete) throws IOException { JSONArray jsonResults = new JSONArray(); StringWriter json = new StringWriter(); for (Autocompletion_Trie.AutocompleteElement result : autocomplete) { JSONObject jsonResult = new JSONObject(); jsonResult.put("uri", result.id); jsonResult.put("label", result.label); jsonResults.add(jsonResult);//ww w . java 2 s . c o m } JSONObject finalJSONresult = new JSONObject(); finalJSONresult.put("results", jsonResults); finalJSONresult.writeJSONString(json); return json.toString(); }
From source file:com.github.lgi2p.obirs.utils.JSONConverter.java
public static RefinedObirsQuery parseRefinedObirsQuery(SM_Engine engine, String jsonQuery) throws ParseException, IOException, Exception { logger.info("parsing json refined query"); logger.info("query: " + jsonQuery); jsonQuery = jsonQuery.replace("\n", ""); jsonQuery = jsonQuery.replace("\r", ""); JSONParser parser = new JSONParser(); Object obj = parser.parse(jsonQuery); JSONObject jsonObject = (JSONObject) obj; JSONObject _query = (JSONObject) jsonObject.get("query"); StringWriter jsQuery = new StringWriter(); _query.writeJSONString(jsQuery); ObirsQuery obirsQuery = parseObirsJSONQuery(engine, jsQuery.toString()); List<URI> selectedItemURIs = toURIs((List<String>) jsonObject.get("selectedItemURIs")); List<URI> rejectedItemURIs = toURIs((List<String>) jsonObject.get("rejectedItemURIs")); return new RefinedObirsQuery(obirsQuery, selectedItemURIs, rejectedItemURIs); }
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
private static void writeAccessToken(JSONObject obj) { Writer writer = null;//from w w w . j ava 2 s .c o m try { writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(tokenFilename), StandardCharsets.UTF_8)); obj.writeJSONString(writer); } catch (IOException e) { } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { } } } }
From source file:com.github.lgi2p.obirs.utils.JSONConverter.java
public static String jsonifyObirsResults(List<ObirsResult> results, IndexerJSON indexer, Map<URI, String> indexURI2Label) throws IOException { Map<String, String> namespace2prefix = new HashMap<String, String>(); JSONArray jsonResults = new JSONArray(); Set<URI> infoConcepts = new HashSet(); StringWriter json = new StringWriter(); for (ObirsResult result : results) { JSONObject jsonResult = new JSONObject(); URI itemResultURI = result.getItemURI(); ItemMetadata metadata = indexer.getMetadata(itemResultURI); jsonResult.put("itemTitle", metadata.title); jsonResult.put("href", metadata.href); jsonResult.put("itemId", metadata.idLiteral); jsonResult.put("itemURI", buildShortURI(result.getItemURI(), namespace2prefix)); jsonResult.put("score", result.getScore()); JSONArray jsonConcepts = new JSONArray(); if (result.getConcepts() != null) { for (ConceptMatch concept : result.getConcepts()) { JSONObject jsonConcept = new JSONObject(); jsonConcept.put("queryConceptURI", buildShortURI(concept.getQueryConceptURI(), namespace2prefix)); jsonConcept.put("matchingConceptURI", buildShortURI(concept.getMatchingConceptURI(), namespace2prefix)); infoConcepts.add(concept.getQueryConceptURI()); infoConcepts.add(concept.getMatchingConceptURI()); jsonConcept.put("relationType", buildShortURI(concept.getRelationType(), namespace2prefix)); jsonConcept.put("score", concept.getScore()); jsonConcepts.add(jsonConcept); }/* w ww . ja v a2 s . c o m*/ } jsonResult.put("concepts", jsonConcepts); jsonResults.add(jsonResult); } JSONArray jsonInfoConcepts = new JSONArray(); for (URI uri : infoConcepts) { JSONObject jsonQueryConcept = new JSONObject(); jsonQueryConcept.put("uri", buildShortURI(uri, namespace2prefix)); jsonQueryConcept.put("label", indexURI2Label.get(uri)); jsonInfoConcepts.add(jsonQueryConcept); } JSONArray jsonInfoNamespace = new JSONArray(); for (Map.Entry<String, String> e : namespace2prefix.entrySet()) { JSONObject jsonQueryConcept = new JSONObject(); jsonQueryConcept.put("ns", e.getKey()); jsonQueryConcept.put("prefix", e.getValue()); jsonInfoNamespace.add(jsonQueryConcept); } JSONObject finalJSONresult = new JSONObject(); finalJSONresult.put("prefixes", jsonInfoNamespace); finalJSONresult.put("results", jsonResults); finalJSONresult.put("infoConcepts", jsonInfoConcepts); finalJSONresult.writeJSONString(json); return json.toString(); }
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
@SuppressWarnings("unchecked") public static void authServer17(String hash) throws IOException { URL url;//from ww w.j ava 2s . c o m String username; String accessToken; try { if (loginDetails == null) { throw new IOException("Not logged in"); } try { username = URLEncoder.encode(getUsername(), "UTF-8"); accessToken = URLEncoder.encode(getAccessToken(), "UTF-8"); hash = URLEncoder.encode(hash, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IOException("Username/password encoding error", e); } String urlString; urlString = sessionServer17; url = new URL(urlString); } catch (MalformedURLException e) { throw new IOException("Auth server URL error", e); } HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setDoOutput(true); con.setInstanceFollowRedirects(false); con.setReadTimeout(5000); con.setConnectTimeout(5000); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.connect(); JSONObject obj = new JSONObject(); obj.put("accessToken", accessToken); obj.put("selectedProfile", loginDetails.get("selectedProfile")); obj.put("serverId", hash); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8)); try { obj.writeJSONString(writer); writer.flush(); writer.close(); } catch (IOException e) { if (writer != null) { writer.close(); con.disconnect(); return; } } if (con.getResponseCode() != 200) { throw new IOException("Unable to verify username, please restart proxy"); } BufferedReader reader = new BufferedReader( new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); try { String reply = reader.readLine(); if (reply != null) { throw new IOException("Auth server replied (" + reply + ")"); } } finally { reader.close(); con.disconnect(); } }
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
public static JSONObject sendRequest(JSONObject request, String endpoint) { URL url;//from w w w .ja v a 2 s .c o m try { url = new URL(authServer + "/" + endpoint); } catch (MalformedURLException e) { return null; } try { HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setDoOutput(true); con.setInstanceFollowRedirects(false); con.setReadTimeout(5000); con.setConnectTimeout(5000); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.connect(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8)); try { request.writeJSONString(writer); writer.flush(); writer.close(); if (con.getResponseCode() != 200) { return null; } BufferedReader reader = new BufferedReader( new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); try { JSONParser parser = new JSONParser(); try { return (JSONObject) parser.parse(reader); } catch (ParseException e) { return null; } } finally { reader.close(); } } finally { writer.close(); con.disconnect(); } } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:cc.pinel.mangue.storage.AbstractStorage.java
/** * Writes the JSON object to the storage. * /*from w ww . j av a 2 s.c o m*/ * @param json the JSON object * @throws IOException */ protected void writeJSON(JSONObject json) throws IOException { FileWriter fw = null; try { fw = new FileWriter(getPath()); json.writeJSONString(fw); } finally { if (fw != null) fw.close(); } }