List of usage examples for javax.json JsonArrayBuilder build
JsonArray build();
From source file:com.seniorproject.semanticweb.services.WebServices.java
public String prepareAdvancedSearchResult(String queryString, ArrayList<String> results) throws FileNotFoundException { JsonArrayBuilder out = Json.createArrayBuilder(); JsonArrayBuilder resultArray = Json.createArrayBuilder(); String words[] = queryString.split("\\s+"); for (int i = 0; i < words.length; i++) { if (words[i].equalsIgnoreCase("select")) { int j = i + 1; if (words[j].equalsIgnoreCase("distinct")) { j++;// w w w .j av a 2 s.c o m } while (!words[j].equalsIgnoreCase("where")) { resultArray.add(words[j].substring(1)); j++; } break; } } out.add(resultArray); for (String result : results) { List<String> matchList = new ArrayList<>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); Matcher regexMatcher = regex.matcher(result); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); } for (int i = 0; i < matchList.size(); i++) { resultArray.add(matchList.get(i)); } out.add(resultArray); } return out.build().toString(); }
From source file:searcher.CollStat.java
public String constructJSONForRetrievedSet(HashMap<Integer, Integer> indexOrder, String queryStr, ScoreDoc[] hits, int indexNum, int pageNum, int[] selection) throws Exception { System.out.println("Num Retrieved = " + hits.length); Query query = buildQuery(queryStr); IndexReader reader = multiReader != null ? multiReader : getReaderInOrder(indexOrder, indexNum); JsonArrayBuilder arrayBuilder = factory.createArrayBuilder(); JsonObjectBuilder objectBuilder = factory.createObjectBuilder(); if (selection != null) { for (int i : selection) { if (0 <= i && i < hits.length) { ScoreDoc hit = hits[i];//www . j av a 2 s. c om arrayBuilder.add(constructJSONForDoc(reader, query, hit.doc)); } } } else { int start = (pageNum - 1) * pageSize; int end = start + pageSize; if (start < 0) { start = 0; } if (end >= hits.length) { end = hits.length; } int hasMore = end < hits.length ? 1 : 0; for (int i = start; i < end; i++) { ScoreDoc hit = hits[i]; arrayBuilder.add(constructJSONForDoc(reader, query, hit.doc)); } // append the hasMore flag and the number of hits for this query... objectBuilder.add("hasmore", hasMore); } objectBuilder.add("numhits", hits.length); arrayBuilder.add(objectBuilder); return arrayBuilder.build().toString(); }
From source file:csg.files.CSGFiles.java
public void saveRecitationData(AppDataComponent recData, String filePath) throws IOException { RecitationData recDataManager = (RecitationData) recData; JsonArrayBuilder recArrayBuilder = Json.createArrayBuilder(); ObservableList<Recitation> recitations = recDataManager.getRecitations(); for (Recitation recitation : recitations) { JsonObject recitationJson = Json.createObjectBuilder().add(JSON_SECTION, recitation.getSection()) .add(JSON_INSTRUCTOR, recitation.getInstructor()).add(JSON_DAYTIME, recitation.getDayTime()) .add(JSON_LOCATION, recitation.getLocation()).add(JSON_FIRSTTA, recitation.getFirstTA()) .add(JSON_SECONDTA, recitation.getSecondTA()).build(); recArrayBuilder.add(recitationJson); }//from w w w . ja v a2 s. co m JsonArray recitaitonArray = recArrayBuilder.build(); JsonObject dataManagerJSO = Json.createObjectBuilder().add(JSON_RECITATION, recitaitonArray).build(); Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); StringWriter sw = new StringWriter(); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(dataManagerJSO); jsonWriter.close(); // INIT THE WRITER OutputStream os = new FileOutputStream(filePath); JsonWriter jsonFileWriter = Json.createWriter(os); jsonFileWriter.writeObject(dataManagerJSO); String prettyPrinted = sw.toString(); PrintWriter pw = new PrintWriter(filePath); pw.write(prettyPrinted); pw.close(); }
From source file:no.sintef.jarfter.PostgresqlInteractor.java
public JsonObject selectAllTransfomations(String filter_uri) throws JarfterException { checkConnection();//from ww w .j a va 2 s. c o m String table = "transformations"; String uri = "uri"; String name = "name"; String metadata = "metadata"; String clojure = "clojure"; JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); try { PreparedStatement st = conn.prepareStatement("SELECT " + uri + ", " + name + ", " + metadata + ", " + clojure + " FROM transformations WHERE uri ~ ?;"); if (filter_uri == null) { st.setString(1, ".*"); } else { st.setString(1, filter_uri); } log("selectAllTransfomations - calling executeQuery"); ResultSet rs = st.executeQuery(); while (rs.next()) { JsonObjectBuilder job = Json.createObjectBuilder(); job.add(uri, rs.getString(uri)); job.add(name, rs.getString(name)); job.add(metadata, rs.getString(metadata)); job.add(clojure, rs.getString(clojure)); jsonArrayBuilder.add(job.build()); } rs.close(); st.close(); } catch (SQLException sqle) { log("selectAllTransformations - got SQLException"); error(sqle); throw new JarfterException(JarfterException.Error.SQL_UNKNOWN_ERROR, sqle.getLocalizedMessage()); } return Json.createObjectBuilder().add(table, jsonArrayBuilder.build()).build(); }
From source file:prod.products.java
private String getResults(String query, String... params) { Boolean Result = false; JsonArrayBuilder pList = Json.createArrayBuilder(); StringBuilder sb = new StringBuilder(); try (Connection cn = credentials.getConnection()) { PreparedStatement pstmt = cn.prepareStatement(query); for (int i = 1; i <= params.length; i++) { pstmt.setString(i, params[i - 1]); Result = true;/*from ww w.j a v a 2 s .c o m*/ } ResultSet rs = pstmt.executeQuery(); if (Result == false) { while (rs.next()) { JsonObjectBuilder productBuilder = Json.createObjectBuilder(); productBuilder.add("productId", rs.getInt("product_id")) .add("name", rs.getString("product_name")) .add("description", rs.getString("product_description")) .add("quantity", rs.getInt("quantity")); pList.add(productBuilder); } } else { while (rs.next()) { JsonObject jsonObt = Json.createObjectBuilder().add("productId", rs.getInt("product_id")) .add("name", rs.getString("product_name")) .add("description", rs.getString("product_description")) .add("quantity", rs.getInt("quantity")).build(); return jsonObt.toString(); } } } catch (SQLException ex) { Logger.getLogger(products.class.getName()).log(Level.SEVERE, null, ex); } return pList.build().toString(); }
From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java
@Override public Response getAllItems() throws ServiceException { List<Item> allItems = itemsService.getAllItems(); JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); for (Item i : allItems) { jsonObjectBuilder.add("id", (i.getId() == null) ? "" : i.getId().toString()) .add("user_id", (i.getUser().getId() == null) ? "" : i.getUser().getId().toString()) .add("user_email", (i.getUser().getEmail() == null) ? "" : i.getUser().getEmail()) .add("club_id", (i.getClub().getId() == null) ? "" : i.getClub().getId().toString()) .add("type", (i.getType() == null) ? "" : i.getType().toString()) .add("name", (i.getName() == null) ? "" : i.getName()) .add("description", (i.getDescription() == null) ? "" : i.getDescription()) .add("hasImage", i.hasImage()) .add("minPrice", (i.getMinPrice() == null) ? "" : i.getMinPrice().toString()) .add("maxPrice", (i.getMaxPrice() == null) ? "" : i.getMaxPrice().toString()); if (i.getTags() != null) { JsonArrayBuilder jsonArrayBuilderInterest = Json.createArrayBuilder(); for (String s : i.getTags()) { jsonArrayBuilderInterest.add(Json.createObjectBuilder().add("text", s)); }/*from w w w. j a v a 2 s . c o m*/ jsonObjectBuilder.add("tags", jsonArrayBuilderInterest); } jsonArrayBuilder.add(jsonObjectBuilder); } JsonArray jsonArray = jsonArrayBuilder.build(); return Response.ok(jsonArray.toString()).build(); }
From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java
@Override public Response getAllItemsByClub(@PathParam("id") Long club_id) throws ServiceException { List<Item> allItems = itemsService.getAllItemsByClub(club_id); JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); for (Item i : allItems) { jsonObjectBuilder.add("id", (i.getId() == null) ? "" : i.getId().toString()) .add("user_id", (i.getUser().getId() == null) ? "" : i.getUser().getId().toString()) .add("user_email", (i.getUser().getEmail() == null) ? "" : i.getUser().getEmail()) .add("club_id", (i.getClub().getId() == null) ? "" : i.getClub().getId().toString()) .add("type", (i.getType() == null) ? "" : i.getType().toString()) .add("name", (i.getName() == null) ? "" : i.getName()) .add("description", (i.getDescription() == null) ? "" : i.getDescription()) .add("hasImage", i.hasImage()) .add("minPrice", (i.getMinPrice() == null) ? "" : i.getMinPrice().toString()) .add("maxPrice", (i.getMaxPrice() == null) ? "" : i.getMaxPrice().toString()); if (i.getTags() != null) { JsonArrayBuilder jsonArrayBuilderInterest = Json.createArrayBuilder(); for (String s : i.getTags()) { jsonArrayBuilderInterest.add(Json.createObjectBuilder().add("text", s)); }//from ww w . ja va2s . c o m jsonObjectBuilder.add("tags", jsonArrayBuilderInterest); } jsonArrayBuilder.add(jsonObjectBuilder); } JsonArray jsonArray = jsonArrayBuilder.build(); return Response.ok(jsonArray.toString()).build(); }
From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java
@Override public Response getAllItemsByUser(@PathParam("id") Long userId) throws ServiceException { List<Item> allItems = itemsService.getAllItemsByUser(userId); JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); for (Item i : allItems) { jsonObjectBuilder.add("id", (i.getId() == null) ? "" : i.getId().toString()) .add("user_id", (i.getUser().getId() == null) ? "" : i.getUser().getId().toString()) .add("user_email", (i.getUser().getEmail() == null) ? "" : i.getUser().getEmail()) .add("club_id", (i.getClub().getId() == null) ? "" : i.getClub().getId().toString()) .add("type", (i.getType() == null) ? "" : i.getType().toString()) .add("name", (i.getName() == null) ? "" : i.getName()) .add("description", (i.getDescription() == null) ? "" : i.getDescription()) .add("hasImage", i.hasImage()) .add("minPrice", (i.getMinPrice() == null) ? "" : i.getMinPrice().toString()) .add("maxPrice", (i.getMaxPrice() == null) ? "" : i.getMaxPrice().toString()); if (i.getTags() != null) { JsonArrayBuilder jsonArrayBuilderInterest = Json.createArrayBuilder(); for (String s : i.getTags()) { jsonArrayBuilderInterest.add(Json.createObjectBuilder().add("text", s)); }//from w w w . j a v a 2 s . c o m jsonObjectBuilder.add("tags", jsonArrayBuilderInterest); } jsonArrayBuilder.add(jsonObjectBuilder); } JsonArray jsonArray = jsonArrayBuilder.build(); return Response.ok(jsonArray.toString()).build(); }
From source file:csg.files.CSGFiles.java
public void saveTeamsAndStudentsData(AppDataComponent projectData, String filePath) throws IOException { ProjectData projectDataManager = (ProjectData) projectData; JsonArrayBuilder teamArrayBuilder = Json.createArrayBuilder(); JsonArrayBuilder studentArrayBuilder = Json.createArrayBuilder(); ObservableList<Team> teams = projectDataManager.getTeams(); ObservableList<Student> students = projectDataManager.getStudents(); for (Team team : teams) { JsonObject teamsJson = Json.createObjectBuilder().add(JSON_NAME, team.getName()) .add(JSON_RED, (Integer.parseInt(team.getColor().toString().substring(0, 2), 16))) .add(JSON_GREEN, (Integer.parseInt(team.getColor().toString().substring(2, 4), 16))) .add(JSON_BLUE, (Integer.parseInt(team.getColor().toString().substring(4, 6), 16))) .add(JSON_TEXTCOLOR, "#" + team.getTextColor()).build(); teamArrayBuilder.add(teamsJson); }// w w w. j ava 2s . co m JsonArray teamArray = teamArrayBuilder.build(); for (Student student : students) { JsonObject studentsJson = Json.createObjectBuilder().add(JSON_LASTNAME, student.getLastName()) .add(JSON_FIRSTNAME, student.getFirstName()).add(JSON_TEAM, student.getTeam()) .add(JSON_ROLE, student.getRole()).build(); studentArrayBuilder.add(studentsJson); } JsonArray studentArray = studentArrayBuilder.build(); JsonObject dataManagerJSO = Json.createObjectBuilder().add(JSON_TEAMS, teamArray) .add(JSON_STUDENTS, studentArray).build(); // AND NOW OUTPUT IT TO A JSON FILE WITH PRETTY PRINTING Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); StringWriter sw = new StringWriter(); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(dataManagerJSO); jsonWriter.close(); // INIT THE WRITER OutputStream os = new FileOutputStream(filePath); JsonWriter jsonFileWriter = Json.createWriter(os); jsonFileWriter.writeObject(dataManagerJSO); String prettyPrinted = sw.toString(); PrintWriter pw = new PrintWriter(filePath); pw.write(prettyPrinted); pw.close(); }
From source file:com.seniorproject.semanticweb.services.WebServices.java
public String prepareFacetedSearchResult(ArrayList<String> data, String category) throws FileNotFoundException { JsonArrayBuilder out = Json.createArrayBuilder(); JsonObjectBuilder resultObject = Json.createObjectBuilder(); for (String line : data) { List<String> matchList = new ArrayList<>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); Matcher regexMatcher = regex.matcher(line); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); }/*from w w w . j a va 2s . co m*/ if (matchList.size() >= 2) { try (BufferedReader br = new BufferedReader(new FileReader(servletContext .getRealPath("/WEB-INF/classes/hadoop/modified_isValueOfQuery/modified_isValueOfQuery_" + category + ".txt")))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { if (matchList.get(0).equalsIgnoreCase(sCurrentLine)) { matchList.set(0, "is " + matchList.get(0) + " of"); break; } } resultObject.add("value", matchList.get(0)); if (matchList.size() >= 2) { resultObject.add("head", matchList.get(1).replace("\"", "")); } else { resultObject.add("head", matchList.get(0).replace("\"", "")); } } catch (IOException e) { e.printStackTrace(); } ; } out.add(resultObject); } return out.build().toString(); }