List of usage examples for com.google.gson JsonParser JsonParser
@Deprecated
public JsonParser()
From source file:com.cloudant.client.api.Search.java
License:Open Source License
/** * Performs a Cloudant Search and returns the result as an {@link SearchResult} * * @param <T> Object type T, an instance into which the rows[].doc/group[].rows[].doc * attribute of the Search result response should be deserialized into. Same * goes for the rows[].fields/group[].rows[].fields attribute * @param query the Lucene query to be passed to the Search index * @param classOfT The class of type T./*from w ww. jav a 2 s . c om*/ * @return The Search result entries */ public <T> SearchResult<T> querySearchResult(String query, Class<T> classOfT) { InputStream instream = null; try { Reader reader = new InputStreamReader(instream = queryForStream(query), "UTF-8"); JsonObject json = new JsonParser().parse(reader).getAsJsonObject(); SearchResult<T> sr = new SearchResult<T>(); sr.setTotalRows(getAsLong(json, "total_rows")); sr.setBookmark(getAsString(json, "bookmark")); if (json.has("rows")) { sr.setRows(getRows(json.getAsJsonArray("rows"), sr, classOfT)); } else if (json.has("groups")) { setGroups(json.getAsJsonArray("groups"), sr, classOfT); } if (json.has("counts")) { sr.setCounts(getFieldsCounts(json.getAsJsonObject("counts").entrySet())); } if (json.has("ranges")) { sr.setRanges(getFieldsCounts(json.getAsJsonObject("ranges").entrySet())); } return sr; } catch (UnsupportedEncodingException e) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException(e); } finally { close(instream); } }
From source file:com.cloudant.client.org.lightcouch.CouchDbClient.java
License:Open Source License
/** * @return DB Server version./* ww w .j a va2s. c o m*/ */ public String serverVersion() { InputStream instream = null; try { instream = get(getBaseUri()); Reader reader = new InputStreamReader(instream, "UTF-8"); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), "version"); } catch (UnsupportedEncodingException e) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException(e); } finally { close(instream); } }
From source file:com.cloudant.client.org.lightcouch.CouchDbClientBase.java
License:Open Source License
/** * @return DB Server version.//from w w w. j a va 2 s . com */ public String serverVersion() { InputStream instream = null; try { instream = get(buildUri(getBaseUri()).build()); Reader reader = new InputStreamReader(instream, "UTF-8"); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), "version"); } catch (UnsupportedEncodingException e) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException(e); } finally { close(instream); } }
From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java
License:Open Source License
/** * @return A JSON element as a String, or null if not found, from the response *///from w ww . ja v a 2 s . c om public static String getAsString(HttpResponse response, String e) { InputStream instream = null; try { instream = getStream(response); Reader reader = new InputStreamReader(instream, "UTF-8"); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), e); } catch (UnsupportedEncodingException e1) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException(e1); } finally { close(instream); } }
From source file:com.cloudant.client.org.lightcouch.Replicator.java
License:Open Source License
/** * Finds all documents in the replicator database. *//*from www .ja v a 2 s . com*/ public List<ReplicatorDocument> findAll() { InputStream instream = null; try { final URI uri = buildUri(dbURI).path("_all_docs").query("include_docs", "true").build(); final Reader reader = new InputStreamReader(instream = client.get(uri), "UTF-8"); final JsonArray jsonArray = new JsonParser().parse(reader).getAsJsonObject().getAsJsonArray("rows"); final List<ReplicatorDocument> list = new ArrayList<ReplicatorDocument>(); for (JsonElement jsonElem : jsonArray) { JsonElement elem = jsonElem.getAsJsonObject().get("doc"); if (!getAsString(elem.getAsJsonObject(), "_id").startsWith("_design")) { // skip // design docs ReplicatorDocument rd = client.getGson().fromJson(elem, ReplicatorDocument.class); list.add(rd); } } return list; } catch (UnsupportedEncodingException e) { // This should never happen as every implementation of the java platform is required // to support UTF-8. throw new RuntimeException(e); } finally { close(instream); } }
From source file:com.cloudbees.gasp.activity.GaspRESTLoaderActivity.java
License:Apache License
@Override public void onLoadFinished(Loader<RESTLoader.RESTResponse> loader, RESTLoader.RESTResponse data) { int code = data.getCode(); String json = data.getData(); // Check to see if we got an HTTP 200 code and have some data. if (code == 200 && !json.equals("")) { Log.i(TAG, "RESTLoader returns:" + json); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonArray(); Iterator<JsonElement> reviews = array.iterator(); mAdapter.clear();/*from ww w. j ava2 s . c om*/ while (reviews.hasNext()) { mAdapter.add(reviews.next().toString()); } } else { Toast.makeText(this, getResources().getString(R.string.gasp_network_error), Toast.LENGTH_SHORT).show(); } }
From source file:com.cloudbees.gasp.fragment.GaspReviewsResponderFragment.java
License:Apache License
@Override public void onRESTResult(int code, String result) { // Here is where we handle our REST response. This is similar to the // LoaderCallbacks<D>.onLoadFinished() call from the previous tutorial. // Check to see if we got an HTTP 200 code and have some data. if (code == 200 && result != null) { JsonParser parser = new JsonParser(); JsonArray array = parser.parse(result).getAsJsonArray(); Iterator<JsonElement> reviews = array.iterator(); mList.clear();/*from w w w . jav a2 s .c o m*/ while (reviews.hasNext()) { String theReview = reviews.next().toString(); mList.add(theReview); Log.d(TAG, "Review: " + theReview); } setReviews(); } else { Activity activity = getActivity(); if (activity != null) { Toast.makeText(activity, getResources().getString(R.string.gasp_network_error), Toast.LENGTH_SHORT) .show(); } } }
From source file:com.cn.bean.OrderInfo.java
public void setProductLineStructJson(String productLineStructJson) { // System.out.println(productLineStructJson); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(productLineStructJson); JsonObject object = element.getAsJsonObject(); ProductLineStruct struct = new ProductLineStruct(); struct.setFieldName(object.get("fieldName").getAsString()); struct.setFieldValue(object.get("fieldValue").getAsString()); struct.setChildRowNum(object.get("childRowNum").getAsInt()); struct.setViewType(object.get("viewType").getAsInt()); struct.setFieldType(object.get("fieldType").getAsInt()); struct.setTextHorizon(object.get("textHorizon").getAsBoolean()); struct.setWidth(object.get("width").getAsInt()); for (int i = 0; i < struct.getChildRowNum(); i++) { String fieldName = object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .get("fieldName").getAsString(); //System.out.println("fieldName:" + fieldName); switch (fieldName) { case "ProductZlValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productCommand) ? ("") : (productCommand)); break; }// w ww.ja v a 2s .c o m case "ProductNameValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", ((null == productName) ? ("") : (productName)) + "/" + ((null == graphicCode) ? ("") : (graphicCode))); break; } case "ProductCodeValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productCode) ? ("") : (productCode)); break; } case "CustomerNameValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == customerName) ? ("") : (customerName)); break; } case "PlanNumValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", planNum); break; } case "ProductGraphicValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == graphicCode) ? ("") : (graphicCode)); if (!Units.strIsEmpty(graphicCode)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "YaZhuangOrHuaXianValue": { /* if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("true") == 0) yaZhuangOrHuaXian = ""; if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("false") == 0) yaZhuangOrHuaXian = ""; */ object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == yaZhuangOrHuaXian) ? ("") : (yaZhuangOrHuaXian)); if (!Units.strIsEmpty(yaZhuangOrHuaXian)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "YaZhuangOrHuaXianSortValue": { /* System.out.println(yaZhuangOrHuaXianSort); if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("true") == 0) yaZhuangOrHuaXianSort = ""; if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("false") == 0) yaZhuangOrHuaXianSort = ""; */ object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == yaZhuangOrHuaXianSort) ? ("") : (yaZhuangOrHuaXianSort)); if (!Units.strIsEmpty(yaZhuangOrHuaXianSort)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductStandardValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productStandard) ? ("") : (productStandard)); if (!Units.strIsEmpty(productStandard)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductBatchValue": { //System.out.println("productBatch:" + productBatch); object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productBatch) ? ("") : (productBatch)); if (!Units.strIsEmpty(productBatch)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "ProductLengthValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productLength) ? ("") : (productLength)); if (!Units.strIsEmpty(productLength)) object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject() .addProperty("viewType", 1); break; } case "MarkColorValue": { object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue", (null == productColor) ? ("") : (productColor)); object.get("rowData" + (i + 1)).getAsJsonArray().get(2).getAsJsonObject().addProperty("fieldValue", (null == productColor1) ? ("") : (productColor1)); break; } case "MarkColorValue1": { break; } } } Gson gson = new Gson(); /* if (isGuanShu == 1) { object.addProperty("childRowNum", 7); JsonElement serialElement = parser.parse("[{\"fieldName\":\"FinishedSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"FinishedSerialValue\", \"fieldValue\":\"%s\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"CurSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}," + "{\"fieldName\":\"GuanShuSerial\", \"fieldValue\":\"\", \"viewType\":2,\"fieldType\":1,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}]"); object.add("rowData7", serialElement); } */ this.productLineStructJson = gson.toJson(object); // System.out.println("productLineStructJson" + this.productLineStructJson); }
From source file:com.cn.controller.OrderController.java
/** * APP??/*from w w w . j ava 2s. c o m*/ * * @param info * @param username * @param cardNum * @param orderId * @param isGuanShu * @param jsonData * @return */ public int addStationData(StationInfo info, String username, String cardNum, int orderId, int isGuanShu, String jsonData) { DatabaseOpt opt; Connection conn = null; CallableStatement statement = null; CallableStatement updateMarkColor = null; int uploadProductInfoParamsCount = 0; int updateProductInfoParamsCount = 0; int result = -1; try { JsonArray array = new JsonParser().parse(jsonData).getAsJsonArray(); String sql = "{call " + info.getAddDataPro() + "(?, ?, ?, ?"; for (JsonElement element : array) { if (!Units.isStrInArray(element.getAsJsonObject().get("fieldName").getAsString(), productInfoField)) { sql += ", ?"; } else { uploadProductInfoParamsCount++; } } if (isGuanShu == 1) { sql += ", ?)}"; } if (isGuanShu == 0) { sql += ", ?, ?)}"; } System.out.println("sql:" + sql + ",isGuanShu:" + isGuanShu + ",cardNum:" + cardNum); opt = new DatabaseOpt(); conn = opt.getConnect(); statement = conn.prepareCall(sql); statement.setString("cardNum", cardNum); statement.setString("username", username); statement.setInt("orderid", orderId); statement.setInt("isGuanShu", isGuanShu); statement.registerOutParameter("result", Types.INTEGER); if (isGuanShu == 0) { statement.setInt("GuanShuSerial", 0); } for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); if (Units.isStrInArray(object.get("fieldName").getAsString(), productInfoField)) { if (null != updateMarkColor) { updateMarkColor.setString(object.get("fieldName").getAsString(), object.get("fieldValue").getAsString()); } else { String sql1 = "{call [tbUpdateMarkColor](?"; for (int i = 0; i < productInfoField.length; i++) { sql1 += ", ?"; } sql1 += ")}"; updateMarkColor = conn.prepareCall(sql1); updateMarkColor.setInt("orderId", orderId); for (String field : productInfoField) { updateMarkColor.setString(field, null); } updateMarkColor.setString(object.get("fieldName").getAsString(), object.get("fieldValue").getAsString()); } updateProductInfoParamsCount++; if (updateProductInfoParamsCount == uploadProductInfoParamsCount) { updateMarkColor.executeUpdate(); } continue; } /* if (object.get("fieldName").getAsString().equals("MarkColorValue")) { updateMarkColorParamsCount++; if (null != updateMarkColor) { updateMarkColor.setString("MarkColorValue", object.get("fieldValue").getAsString()); if (updateMarkColorParamsCount == 2) { updateMarkColor.executeUpdate(); } } else { updateMarkColor = conn.prepareCall("{call [tbUpdateMarkColor](?, ?, ?, ?)}"); updateMarkColor.setInt("orderId", orderId); updateMarkColor.setString("MarkColorValue", object.get("fieldValue").getAsString()); } continue; } if (object.get("fieldName").getAsString().equals("MarkColorValue1")) { updateMarkColorParamsCount++; if (null != updateMarkColor) { updateMarkColor.setString("MarkColorValue1", object.get("fieldValue").getAsString()); if (updateMarkColorParamsCount == 2) { updateMarkColor.executeUpdate(); } } else { updateMarkColor = conn.prepareCall("{call [tbUpdateMarkColor](?, ?, ?, ?)}"); updateMarkColor.setInt("orderId", orderId); updateMarkColor.setString("MarkColorValue1", object.get("fieldValue").getAsString()); } continue; } */ switch (object.get("fieldType").getAsInt()) { case 1: { statement.setInt(object.get("fieldName").getAsString(), object.get("fieldValue").getAsInt()); break; } case 2: { statement.setFloat(object.get("fieldName").getAsString(), object.get("fieldValue").getAsFloat()); break; } case 3: { statement.setString(object.get("fieldName").getAsString(), object.get("fieldValue").getAsString()); break; } case 4: { if (object.get("fieldValue").getAsBoolean()) { statement.setString(object.get("fieldName").getAsString(), "true"); } else { statement.setString(object.get("fieldName").getAsString(), "false"); } break; } } } statement.executeUpdate(); result = statement.getInt("result"); System.out.println("result:" + result); } catch (SQLException ex) { logger.error("??", ex); } finally { try { if (updateMarkColor != null) { updateMarkColor.close(); } if (statement != null) { statement.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { logger.error("?", ex); } } return result; }
From source file:com.codequicker.quick.templates.state.EngineContext.java
License:Apache License
public void setJson(String key, String payload) { if (TemplateUtil.isNullOrEmpty(payload) || TemplateUtil.isNullOrEmpty(key)) return;/*from ww w . j a v a2 s . co m*/ JsonParser jsonParser = new JsonParser(); JsonElement jsonObject = jsonParser.parse(payload).getAsJsonObject(); context.put(key, jsonObject); }