List of usage examples for com.google.gson JsonParser parse
@Deprecated public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
From source file:ProcessRequest.java
public int insertNewTicketDetail(JSONObject recordObject) throws SQLException, JSONException { String valuesPortion = ""; String columnsPortion = ""; //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER); //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber()); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); {//from w w w. j ava 2 s. c om Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //if(value.isString() && value.getAsString().equals("")) { //don't want this null value/key pair //} else { if (!columnsPortion.equals("")) { columnsPortion += ","; } columnsPortion += entry.getKey(); //System.out.println(entry.getKey()+" : "+value.getAsString()); //} } } } //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); String ticketNumber = new Long(value.getAsLong()).toString(); valuesPortion += ticketNumber; } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString())); if (value.getAsBoolean()) valuesPortion += "1"; else valuesPortion += "0"; } } } } String insertQuery = "INSERT INTO " + DbSingleton.TICKET_DETAIL_TABLE_NAME + "(" + columnsPortion + ") VALUES (" + valuesPortion + ")"; String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.TICKET_DETAIL_TABLE_NAME + "') AS TicketDetailID"; executeQuery(insertQuery); ResultSet identResultSet = executeQuery(identQuery); identResultSet.first(); //System.out.println(identResultSet.getRow()); int ticketDetailID = identResultSet.getInt(1); //System.out.println(ticketID); return ticketDetailID; }
From source file:ProcessRequest.java
public int insertNewTicket(JSONObject recordObject) throws SQLException, JSONException { String valuesPortion = ""; String columnsPortion = ""; recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER); recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber()); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); {// www .jav a2 s . c om Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata") || entry.getKey().equals("TicketID")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //if(value.isString() && value.getAsString().equals("")) { //don't want this null value/key pair //} else { if (!columnsPortion.equals("")) { columnsPortion += ","; } columnsPortion += entry.getKey(); //System.out.println(entry.getKey()+" : "+value.getAsString()); //} } } } //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata") || entry.getKey().equals("TicketID")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); String ticketNumber = new Long(value.getAsLong()).toString(); valuesPortion += ticketNumber; } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } if (value.getAsBoolean()) valuesPortion += "1"; else valuesPortion += "0"; } } } } String insertQuery = "INSERT INTO " + DbSingleton.TICKET_TABLE_NAME + "(" + columnsPortion + ") VALUES (" + valuesPortion + ")"; String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.TICKET_TABLE_NAME + "') AS TicketID"; executeQuery(insertQuery); ResultSet identResultSet = executeQuery(identQuery); identResultSet.first(); //System.out.println(identResultSet.getRow()); int ticketID = identResultSet.getInt(1); //System.out.println(ticketID); ////////////////////TICKET NUMBER CHECK boolean uniqueTicketNumber = false; while (!uniqueTicketNumber) { String ticketNumberCheckQuery = "SELECT * FROM " + DbSingleton.TICKET_TABLE_NAME + " WHERE " + DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER + "=" + recordObject.getInt(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER); ResultSet ticketNumberCheckResultSet = executeQuery(ticketNumberCheckQuery); int rowCount = 0; ticketNumberCheckResultSet.last(); rowCount = ticketNumberCheckResultSet.getRow(); ticketNumberCheckResultSet.first(); if (rowCount < 1) throw new SQLException(LOG_TAG + ": wat..."); else if (rowCount > 1) { uniqueTicketNumber = false; } else { uniqueTicketNumber = true; } if (!uniqueTicketNumber) { //update the ticket for the next iteration of this loop, attempting, again, to get a unique ticket number String uniqueTicketNumberQuery = "UPDATE " + DbSingleton.TICKET_TABLE_NAME + " SET " + DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER + "=" + getNextTicketNumber() + " WHERE " + DbSingleton.TicketSchema.COLUMN_TICKET_ID + "=" + ticketID; executeQuery(uniqueTicketNumberQuery); } } //////////////////// return ticketID; }
From source file:adams.data.conversion.JsonToString.java
License:Open Source License
/** * Performs the actual conversion.// w ww . jav a 2 s . c o m * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { String result; Gson gson; JsonParser jp; JsonElement je; result = ((JSONAware) m_Input).toJSONString(); if (m_PrettyPrinting) { gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(result); result = gson.toJson(je); } return result; }
From source file:adams.data.io.output.ViaAnnotationsReportWriter.java
License:Open Source License
/** * Performs the actual writing.//from w ww . j a va 2 s . c o m * * @param data the data to write * @return true if successfully written */ @Override protected boolean writeData(Report data) { LocatedObjects objs; int n; LocatedObject obj; JSONObject all; JSONObject jrep; JSONObject jregions; JSONObject jregion; JSONObject jshape; JSONArray jpoints; JSONObject jatts; String name; Polygon polygon; int[] x; int[] y; int i; String content; Gson gson; JsonParser jp; JsonElement je; all = new JSONObject(); jrep = new JSONObject(); jrep.put("fileref", ""); jrep.put("size", 0); jrep.put("base64_img_data", ""); jrep.put("file_attributes", new JSONObject()); jregions = new JSONObject(); jrep.put("regions", jregions); // get filename name = m_Output.getName(); // fallback if (data.hasValue("Name")) name = data.getStringValue("Name"); else if (data.hasValue("Filename")) name = data.getStringValue("Filename"); jrep.put("filename", name); all.put(name, jrep); // iterate objects objs = m_Finder.findObjects(data); for (n = 0; n < objs.size(); n++) { obj = objs.get(n); jregion = new JSONObject(); jregions.put("" + n, jregion); jshape = new JSONObject(); jregion.put("shape_attributes", jshape); jatts = new JSONObject(); jregion.put("region_attributes", jatts); if (!m_LabelKey.isEmpty() && (obj.getMetaData().get(m_LabelKey) != null)) jatts.put("name", obj.getMetaData().get(m_LabelKey)); jshape.put("name", "polygon"); if (obj.hasPolygon()) { polygon = obj.getPolygon(); // x jpoints = new JSONArray(); x = polygon.xpoints; for (i = 0; i < x.length; i++) jpoints.add(x[i]); jshape.put("all_points_x", jpoints); // y jpoints = new JSONArray(); y = polygon.ypoints; for (i = 0; i < y.length; i++) jpoints.add(y[i]); jshape.put("all_points_y", jpoints); } else { // x jpoints = new JSONArray(); jpoints.add(obj.getX()); jpoints.add(obj.getX() + obj.getWidth() - 1); jpoints.add(obj.getX() + obj.getWidth() - 1); jpoints.add(obj.getX()); jshape.put("all_points_x", jpoints); // y jpoints = new JSONArray(); jpoints.add(obj.getY()); jpoints.add(obj.getY()); jpoints.add(obj.getY() + obj.getHeight() - 1); jpoints.add(obj.getY() + obj.getHeight() - 1); jshape.put("all_points_y", jpoints); } } if (m_PrettyPrinting) { gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(all.toString()); content = gson.toJson(je); } else { content = all.toString(); } return FileUtils.writeToFile(m_Output.getAbsolutePath(), content, false); }
From source file:adams.data.report.ReportJsonUtils.java
License:Open Source License
/** * Creates a report from the reader, reading in JSON. * * @param reader the reader to obtain the JSON from * @return the report, null if failed to create or find data * @throws Exception if reading/parsing fails *//*from w ww . j a v a2s . co m*/ public static Report fromJson(Reader reader) throws Exception { JsonParser jp; JsonElement je; jp = new JsonParser(); je = jp.parse(reader); return ReportJsonUtils.fromJson(je.getAsJsonObject()); }
From source file:adams.flow.sink.JsonFileWriter.java
License:Open Source License
/** * Executes the flow item.//from ww w .ja v a 2 s .c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; String content; Gson gson; JsonParser jp; JsonElement je; result = null; content = ((JSONAware) m_InputToken.getPayload()).toJSONString(); if (m_PrettyPrinting) { gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(content); content = gson.toJson(je); } if (!FileUtils.writeToFile(m_OutputFile.getAbsolutePath(), content, false)) result = "Failed to write JSON file: " + m_OutputFile; return result; }
From source file:adams.gui.tools.previewbrowser.JsonPrettyPrintHandler.java
License:Open Source License
/** * Creates the actual view./*from w w w. ja v a 2 s. c o m*/ * * @param file the file to create the view for * @return the view */ @Override protected PreviewPanel createPreview(File file) { PreviewPanel result; TextEditorPanel textPanel; FileReader freader; BufferedReader breader; Gson gson; JsonParser jp; JsonElement je; String content; freader = null; breader = null; try { freader = new FileReader(file.getAbsolutePath()); breader = new BufferedReader(freader); gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(breader); content = gson.toJson(je); textPanel = new TextEditorPanel(); textPanel.setContent(content); textPanel.setEditable(false); result = new PreviewPanel(textPanel, textPanel.getTextArea()); } catch (Exception e) { textPanel = new TextEditorPanel(); textPanel.open(file); textPanel.setEditable(false); result = new PreviewPanel(textPanel, textPanel.getTextArea()); } finally { FileUtils.closeQuietly(breader); FileUtils.closeQuietly(freader); } return result; }
From source file:ai.nitro.bot4j.integration.slack.receive.webhook.impl.SlackActionWebhookImpl.java
License:Open Source License
@Override public String post(final HttpServletRequest req, final HttpServletResponse res) { try {/*from w w w . ja v a2s . c o m*/ final String payload = req.getParameter("payload"); final JsonParser jsonParser = new JsonParser(); final JsonObject json = jsonParser.parse(payload).getAsJsonObject(); slackReceiveHandler.handleAction(json, (long) 0); } catch (final Exception e) { handleException(e); } return ""; }
From source file:ai.nitro.bot4j.integration.slack.receive.webhook.impl.SlackEventWebhookImpl.java
License:Open Source License
@Override public String post(final HttpServletRequest req, final HttpServletResponse res) { String result = ""; try {/* ww w . j ava2 s. c o m*/ final String body = CharStreams.toString(req.getReader()); final JsonParser jsonParser = new JsonParser(); final JsonObject jsonReq = jsonParser.parse(body).getAsJsonObject(); if (!jsonReq.has(TYPE)) { LOG.warn("no type in JSON"); } else { final JsonElement typeJsonElement = jsonReq.get(TYPE); final String type = typeJsonElement.getAsString(); switch (type) { case URL_VERIFICATION: result = handleUrlVerification(jsonReq, res); break; case EVENT_CALLBACK: handleEvent(jsonReq, res); break; default: LOG.info("unknown type {}", type); break; } } } catch (final Exception e) { handleException(e); } return result; }
From source file:algorithmi.models.Categories.java
public Categories(String data) { //Transforma a string recebida pelo pedido http para json JsonParser jsonParser = new JsonParser(); JsonObject Categories = (JsonObject) jsonParser.parse(data); //Exibe os dados, em formato json System.out.println(Categories.entrySet()); /**/* www.ja v a2 s .c o m*/ * * Revalidar TUDO, formatos, campos vazios, TUDO!! * */ validateData(); //Associa os dados ao objecto Question this._id = Categories.get("_id").getAsInt(); //ir buscar o max id da bd + 1 this.description = Categories.get("description").getAsString(); }