List of usage examples for com.google.gson.stream JsonReader JsonReader
public JsonReader(Reader in)
From source file:me.lucko.luckperms.common.storage.backing.JSONBacking.java
License:Open Source License
private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) { boolean success = false; try {// w ww. j a v a2 s.com try (FileInputStream fileInput = new FileInputStream(file)) { try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) { try (BufferedReader bufferedReader = new BufferedReader(inputReader)) { try (JsonReader jsonReader = new JsonReader(bufferedReader)) { success = readOperation.apply(jsonReader); } } } } } catch (Exception e) { plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath()); e.printStackTrace(); } return success; }
From source file:me.lucko.luckperms.common.storage.dao.file.FileActionLogger.java
License:MIT License
public void flush() { this.writeLock.lock(); try {/* www . ja v a 2s .c o m*/ // don't perform the i/o process if there's nothing to be written if (this.entryQueue.peek() == null) { return; } try { // read existing array data into memory JsonArray array; if (Files.exists(this.contentFile)) { try (JsonReader reader = new JsonReader( Files.newBufferedReader(this.contentFile, StandardCharsets.UTF_8))) { array = JSON_PARSER.parse(reader).getAsJsonArray(); } catch (IOException e) { e.printStackTrace(); array = new JsonArray(); } } else { array = new JsonArray(); } // poll the queue for new entries for (LogEntry e; (e = this.entryQueue.poll()) != null;) { array.add(LogEntryJsonSerializer.serialize(e)); } // write the full content back to the file try (JsonWriter writer = new JsonWriter( Files.newBufferedWriter(this.contentFile, StandardCharsets.UTF_8))) { writer.setIndent(" "); GSON.toJson(array, writer); } } catch (IOException e) { e.printStackTrace(); } } finally { this.writeLock.unlock(); } }
From source file:me.lucko.luckperms.common.storage.dao.file.FileActionLogger.java
License:MIT License
public Log getLog() throws IOException { Log.Builder log = Log.builder();// ww w. j a va 2s. c om try (JsonReader reader = new JsonReader( Files.newBufferedReader(this.contentFile, StandardCharsets.UTF_8))) { JsonArray array = JSON_PARSER.parse(reader).getAsJsonArray(); for (JsonElement element : array) { log.add(LogEntryJsonSerializer.deserialize(element)); } } return log.build(); }
From source file:me.lucko.luckperms.storage.methods.JSONDatastore.java
License:Open Source License
private boolean doRead(File file, ReadOperation readOperation) { boolean success = false; try {/*ww w .ja v a 2 s .co m*/ @Cleanup FileReader fileReader = new FileReader(file); @Cleanup BufferedReader bufferedReader = new BufferedReader(fileReader); @Cleanup JsonReader jsonReader = new JsonReader(bufferedReader); success = readOperation.onRun(jsonReader); } catch (IOException e) { e.printStackTrace(); } return success; }
From source file:melnorme.lang.utils.gson.JsonParserX.java
License:Open Source License
public static JsonReader newReader(Reader reader, boolean lenient) { JsonReader jsonReader = new JsonReader(reader); jsonReader.setLenient(lenient);//from w w w . java 2s . c o m return jsonReader; }
From source file:melnorme.lang.utils.gson.JsonParserX.java
License:Open Source License
public JsonElement parse(Reader reader, boolean lenient) throws IOException, JsonSyntaxExceptionX { JsonReader json = new JsonReader(reader); boolean originalLenient = json.isLenient(); json.setLenient(lenient);/*from w w w . jav a 2 s. c om*/ try { return parse(json); } finally { json.setLenient(originalLenient); } }
From source file:mobi.jenkinsci.alm.assembla.client.AssemblaClient.java
License:Apache License
public void login() throws IOException { Document pinDoc = Jsoup.parse(getData(String.format(AUTH, appId), false)); if (getLatestRedirectedUrl().getPath().startsWith(LOGIN)) { pinDoc = postLoginForm(pinDoc);// www .j ava2 s. c om } final Element pinBox = pinDoc.select("div[class=box]").first(); if (pinBox == null) { throw new IOException("Missing PIN code from Assembla auth response"); } final Element pinLabel = pinBox.select("p").first(); final Element pinValue = pinBox.select("h1").first(); if (pinLabel == null || pinValue == null) { throw new IOException("Missing PIN code from Assembla auth response"); } final String pin = pinValue.childNode(0).toString(); final HttpPost authPost = new HttpPost( String.format(ASSEMBLA_SITE_APP_AUTH, appId, appSecret) + String.format(PIN_AUTH, pin)); final HttpResponse pinResponse = httpClient.execute(authPost); try { if (pinResponse.getStatusLine().getStatusCode() != HttpURLConnection.HTTP_OK) { throw new IOException( "Post " + authPost.getURI() + " for a PIN failed: " + pinResponse.getStatusLine()); } accessToken = gson.fromJson( new JsonReader(new InputStreamReader(pinResponse.getEntity().getContent(), "UTF-8")), AssemblaAccessToken.class); } finally { authPost.releaseConnection(); } }
From source file:mobi.jenkinsci.alm.assembla.client.AssemblaClient.java
License:Apache License
public void loginRefresh() throws IOException { if (accessToken == null) { login();/*from w w w .ja va 2s. c o m*/ } else { accessToken.access_token = ""; final HttpPost authPost = new HttpPost(String.format(ASSEMBLA_SITE_APP_AUTH, appId, appSecret) + String.format(TOKEN_REFRESH, accessToken.refresh_token)); final HttpResponse response = httpClient.execute(authPost); try { if (response.getStatusLine().getStatusCode() != HttpURLConnection.HTTP_OK) { throw new IOException( "Post " + authPost.getURI() + " for Token refresh failed: " + response.getStatusLine()); } final AssemblaAccessToken refreshToken = gson.fromJson( new JsonReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")), AssemblaAccessToken.class); accessToken.renew(refreshToken); } finally { authPost.releaseConnection(); } } }
From source file:Modules.Client.Model.Utils.Files_lib.json.java
/** * Used to auto load client data from JSON file format */// w w w. jav a2 s .c o m public static void autoloadjsonclient() { String[] p = { "/src/Modules/Client/Model/Utils/Files/json/client.json", "/src/Modules/Client/Model/Utils/Files/json/dummy_client.json" }; String p2 = ""; String PATH = ""; Client_class a = new Client_class(""); if (Config_class.getinstance().isDummy() == true) { p2 = p[1]; } else { p2 = p[0]; } try { XStream xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias("Client_class", Client_class.class); PATH = new java.io.File(".").getCanonicalPath() + p2; File path = new File(PATH); if (path.exists()) { JsonReader reader = new JsonReader(new FileReader(path)); JsonParser parser = new JsonParser(); JsonElement root = parser.parse(reader); Gson json = new Gson(); JsonArray list = root.getAsJsonArray(); for (JsonElement element : list) { a = json.fromJson(element, Client_class.class); Singleton_client.cli.add(a); } //for end } //if end } catch (Exception e) { JOptionPane.showMessageDialog(null, Singleton_app.lang.getProperty("problopejson"), "Error!", JOptionPane.INFORMATION_MESSAGE); // JOptionPane.showMessageDialog(null,"Error auto-loading JSON file"); } }
From source file:Modules.Client.Model.Utils.Files_lib.json.java
/** * Used to load client data from JSON file format *///from w w w .j a v a 2s . co m public static void loadjsonclient() { String path = ""; Client_class client = new Client_class(""); try { XStream xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias("Client_class", Client_class.class); JFileChooser fileChooser = new JFileChooser(); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JSON (*.json)", "json")); int selection = fileChooser.showOpenDialog(null); if (selection == JFileChooser.APPROVE_OPTION) { File JFC = fileChooser.getSelectedFile(); path = JFC.getAbsolutePath(); Singleton_client.cli.clear(); JsonReader reader = new JsonReader(new FileReader(path)); JsonParser parser = new JsonParser(); JsonElement root = parser.parse(reader); Gson json = new Gson(); JsonArray list = root.getAsJsonArray(); for (JsonElement element : list) { client = json.fromJson(element, Client_class.class); Singleton_client.cli.add(client); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, Singleton_app.lang.getProperty("problopejson"), "Error", JOptionPane.INFORMATION_MESSAGE); // JOptionPane.showMessageDialog(null,"Error loading JSON file"); } }