@When("^I read json string from a file$") public void i_read_json_string_from_a_file() throws Throwable { FileInputStream fin = new FileInputStream( new File(System.getProperty("user.dir") + "//src//test//resources//sample.json")); InputStreamReader in = new InputStreamReader(fin); BufferedReader bufferedReader = new BufferedReader(in); StringBuilder sb = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } json = sb.toString(); }
@When("^I read the json data file \"(.*?)\"$") public void i_read_the_json_data_file(String arg1) throws Throwable { FileInputStream fin = new FileInputStream( new File(System.getProperty("user.dir") + "//src//test//resources//" + arg1)); InputStreamReader in = new InputStreamReader(fin); BufferedReader bufferedReader = new BufferedReader(in); StringBuilder sb = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } json = sb.toString(); System.out.println(json); }
@Then("^I print it as a string$") public void i_print_it_as_a_string() throws Throwable { FileInputStream fin = new FileInputStream(new File("output.json")); InputStreamReader in = new InputStreamReader(fin); BufferedReader bufferedReader = new BufferedReader(in); StringBuilder sb = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } String json = sb.toString(); System.out.println(json); Gson gson = new Gson(); Employee employee = gson.fromJson(json, Employee.class); // System.out.println(employee.getFirstName()); }
private CIJob getJob(ApplicationInfo appInfo) throws PhrescoException { Gson gson = new Gson(); try { BufferedReader br = new BufferedReader(new FileReader(getCIJobPath(appInfo))); CIJob job = gson.fromJson(br, CIJob.class); br.close(); return job; } catch (FileNotFoundException e) { S_LOGGER.debug(e.getLocalizedMessage()); return null; } catch (com.google.gson.JsonParseException e) { S_LOGGER.debug("it is already adpted project !!!!! " + e.getLocalizedMessage()); return null; } catch (IOException e) { S_LOGGER.debug(e.getLocalizedMessage()); return null; } }
public List<CIJob> getJobs(ApplicationInfo appInfo) throws PhrescoException { S_LOGGER.debug("GetJobs Called!"); try { boolean adaptedProject = adaptExistingJobs(appInfo); S_LOGGER.debug("Project adapted for new feature => " + adaptedProject); Gson gson = new Gson(); BufferedReader br = new BufferedReader(new FileReader(getCIJobPath(appInfo))); Type type = new TypeToken<List<CIJob>>() {}.getType(); List<CIJob> jobs = gson.fromJson(br, type); br.close(); return jobs; } catch (FileNotFoundException e) { S_LOGGER.debug("FileNotFoundException"); return null; } catch (IOException e) { S_LOGGER.debug("IOException"); throw new PhrescoException(e); } }
public LinkedList parseFile(String fileName, Class theClass) { LinkedList object = null; String nextLine = ""; String jsonFile; StringBuilder sb = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new FileReader(fileName)); while ((nextLine = reader.readLine()) != null) { sb.append(nextLine); } jsonFile = sb.toString(); if (jsonFile.indexOf("{") == 0) { jsonFile = jsonFile.substring(1, jsonFile.length() - 1); } object = (LinkedList) gson.fromJson(jsonFile, theClass); } catch (Exception e) { e.printStackTrace(); } return object; }
private List<String> parseTweetsFromFile(String listFilePath) throws ServletException { List<String> tweets = new ArrayList<String>(); FileReader in = null; try { in = new FileReader(listFilePath); } catch (FileNotFoundException e) { String emsg = "cant find file '" + listFilePath + "'"; throw new ServletException(emsg, e); } BufferedReader fileReader = new BufferedReader(in); String currentLine = null; try { currentLine = fileReader.readLine(); } catch (IOException e) { throw new ServletException("cant read line from file '" + listFilePath + "'", e); } while (currentLine != null) { tweets.add(currentLine); try { currentLine = fileReader.readLine(); } catch (IOException e) { throw new ServletException("cant read line from file '" + listFilePath + "'", e); } } try { fileReader.close(); } catch (IOException e) { final String emsg = "cant close file '" + listFilePath + "'"; throw new ServletException(e); } return tweets; }
private List<Long> parseUsersIdsFromFile(String listFilePath) throws ServletException { List<Long> ids = new ArrayList<Long>(); try { BufferedReader fileReader = new BufferedReader(new FileReader(listFilePath)); String currentLine = fileReader.readLine(); while (currentLine != null) { LOGGER.debug(currentLine); long id = Long.parseLong(currentLine); ids.add(id); currentLine = fileReader.readLine(); } fileReader.close(); } catch (FileNotFoundException e) { String emsg = "cant find file '" + listFilePath + "'"; throw new ServletException(emsg, e); } catch (IOException e) { String emsg = "cant read from file '" + listFilePath + "'"; throw new ServletException(emsg, e); } return ids; }
public synchronized Object __rpccall__(String endpoint, Object funcobj, boolean usecache) { try { // normal case is passing a functioncall, batch case is passing a List<FunctionCall> FunctionCall func = (FunctionCall) funcobj; if (usecache && has(endpoint, func)) return get(endpoint, func); if (!can_connect) if (has(endpoint, func)) { return get(endpoint, func); } else { return null; } func.params = __marshall_args__(func.params); funcobj = func; } catch (ClassCastException ex) { try { BatchClient b = (BatchClient) funcobj; List<FunctionCall> newcalls = new Vector<FunctionCall>(); for (int i = 0; i < b.batch.size(); i++) { b.batch.get(i).params = __marshall_args__(b.batch.get(i).params); } for (FunctionCall f : b.batch) { if (!b.called.contains(f)) { newcalls.add(f); } } funcobj = newcalls; } catch (ClassCastException ex2) { } } HttpPost post = new HttpPost(endpoint); StringEntity req = null; req = new StringEntity(gson.toJson(funcobj), "UTF-8"); post.setEntity(req); post.setHeader(HTTP.CONTENT_TYPE, "application/json"); HttpResponse resp = null; HttpClient client = Http.client(); try { resp = client.execute(post, Http.context()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } Object ret = null; HttpEntity rep = resp.getEntity(); InputStreamReader is = null; try { is = new InputStreamReader(rep.getContent()); BufferedReader rd = new BufferedReader(is); String out = ""; String line = null; while ((line = rd.readLine()) != null) { out += line; } resp.getEntity().consumeContent(); ret = __parse_response__(out); } catch (IOException e) { // TODO Auto-generated catch block throw new RuntimeException(); } finally { if (is != null) try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } FunctionCall func = (FunctionCall) funcobj; if (usecache) set(endpoint, func, ret); return ret; }