List of usage examples for com.google.gson JsonObject addProperty
public void addProperty(String property, Character value)
From source file:br.ufg.inf.es.saep.sandbox.dominio.infraestrutura.ValorSerializer.java
License:Creative Commons License
@Override public JsonElement serialize(Valor valor, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject obj = new JsonObject(); byte tipo = valor.getTipo(); obj.addProperty("tipo", tipo); switch (tipo) { case REAL://w w w.j a v a2 s.c o m obj.addProperty("valor", valor.getReal()); break; case Valor.LOGICO: obj.addProperty("valor", valor.getBoolean()); break; case Valor.DATA: obj.addProperty("valor", valor.getData().format(Valor.FORMATO_DATA)); break; case Valor.STRING: obj.addProperty("valor", valor.getString()); break; } return obj; }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public JsonObject createJsonFromString(String pathToLeaf, double value) { String[] treeNodes = pathToLeaf.split("\\."); JsonObject json = new JsonObject(); for (int i = treeNodes.length - 1; i >= 0; i--) { JsonObject temp = new JsonObject(); if (i == treeNodes.length - 1) { temp.addProperty(treeNodes[i], value); } else {//from w w w. j a v a 2 s . c o m temp.add(treeNodes[i], json); } json = temp; } return json; }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public JsonObject createJsonFromString(String pathToLeaf, String value) { String[] treeNodes = pathToLeaf.split("\\."); JsonObject json = new JsonObject(); for (int i = treeNodes.length - 1; i >= 0; i--) { JsonObject temp = new JsonObject(); if (i == treeNodes.length - 1) { temp.addProperty(treeNodes[i], value); } else {//from w ww. j av a2 s . co m temp.add(treeNodes[i], json); } json = temp; } return json; }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public void addBranchToJson(String newBranch, JsonObject json, double value) { String[] newNodes = newBranch.split("\\."); JsonObject temp;// = new JsonObject(); if (newNodes.length > 1) { if (json.has(newNodes[0])) { addBranchToJson(newBranch.substring(newNodes[0].length() + 1), json.getAsJsonObject(newNodes[0]), value);/*from w ww . j ava 2s . c o m*/ } else { temp = createJsonFromString(newBranch.substring(newNodes[0].length() + 1), value); json.add(newNodes[0], temp); } } else { json.addProperty(newNodes[0], value); } }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public void addBranchToJson(String newBranch, JsonObject json, String value) { String[] newNodes = newBranch.split("\\."); JsonObject temp;// = new JsonObject(); if (newNodes.length > 1) { if (json.has(newNodes[0])) { addBranchToJson(newBranch.substring(newNodes[0].length() + 1), json.getAsJsonObject(newNodes[0]), value);/*w w w . j ava 2 s. co m*/ } else { temp = createJsonFromString(newBranch.substring(newNodes[0].length() + 1), value); json.add(newNodes[0], temp); } } else { json.addProperty(newNodes[0], value); } }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public JsonObject fromBeanToJson(Object bean) { JsonObject json = new JsonObject(); Class type = bean.getClass(); json.add(type.getName(), new JsonObject()); try {/*from ww w. ja v a 2 s . c om*/ Object obj = type.newInstance(); type.cast(obj); for (Field field : type.getFields()) { json.addProperty(field.getName(), field.get(bean).toString()); } } catch (Exception e) { } return json; }
From source file:brand.AddUpdateBrandMaster.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w . j a v a2s.co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final DBHelper helper = DBHelper.GetDBHelper(); final Connection dataConnection = helper.getConnMpAdmin(); final Library lb = Library.getInstance(); String brand_cd = request.getParameter("brand_cd"); final String brand_name = request.getParameter("brand_name"); final String user_id = request.getParameter("user_id"); final JsonObject jResultObj = new JsonObject(); if (dataConnection != null) { try { if (brand_cd.equalsIgnoreCase("")) { brand_cd = lb.generateKey(dataConnection, "BRANDMST", "brand_cd", "B", 7); String sql = "insert into BRANDMST (brand_cd,brand_name,user_id) values(?,?,?)"; PreparedStatement pstLocal = dataConnection.prepareStatement(sql); pstLocal.setString(1, brand_cd); pstLocal.setString(2, brand_name); pstLocal.setString(3, user_id); pstLocal.executeUpdate(); } else if (!brand_cd.equalsIgnoreCase("")) { String sql = "update BRANDMST set brand_name=?,edit_no=edit_no+1,user_id=?,time_stamp=current_timestamp where brand_cd=?"; PreparedStatement pstLocal = dataConnection.prepareStatement(sql); pstLocal.setString(1, brand_name); pstLocal.setString(2, user_id); pstLocal.setString(3, brand_cd); pstLocal.executeUpdate(); } jResultObj.addProperty("result", 1); jResultObj.addProperty("Cause", "success"); jResultObj.addProperty("brand_cd", brand_cd); } catch (SQLNonTransientConnectionException ex1) { jResultObj.addProperty("result", -1); jResultObj.addProperty("Cause", "Server is down"); } catch (SQLException ex) { jResultObj.addProperty("result", -1); jResultObj.addProperty("Cause", ex.getMessage()); } } response.getWriter().print(jResultObj); }
From source file:brand.GetBrandMaster.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww. j a va 2 s.c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final DBHelper helper = DBHelper.GetDBHelper(); final Connection dataConnection = helper.getConnMpAdmin(); final JsonObject jResultObj = new JsonObject(); if (dataConnection != null) { try { String sql = "select BRAND_CD,BRAND_NAME,USER_ID from BRANDMST"; PreparedStatement pstLocal = dataConnection.prepareStatement(sql); ResultSet rsLocal = pstLocal.executeQuery(); JsonArray array = new JsonArray(); while (rsLocal.next()) { JsonObject object = new JsonObject(); object.addProperty("BRAND_CD", rsLocal.getString("BRAND_CD")); object.addProperty("BRAND_NAME", rsLocal.getString("BRAND_NAME")); object.addProperty("USER_ID", rsLocal.getInt("USER_ID")); array.add(object); } jResultObj.addProperty("result", 1); jResultObj.addProperty("Cause", "success"); jResultObj.add("data", array); } catch (SQLNonTransientConnectionException ex1) { jResultObj.addProperty("result", -1); jResultObj.addProperty("Cause", "Server is down"); } catch (SQLException ex) { jResultObj.addProperty("result", -1); jResultObj.addProperty("Cause", ex.getMessage()); } } response.getWriter().print(jResultObj); }
From source file:buri.ddmsence.util.Util.java
License:Open Source License
/** * Adds a value to a JSON object, but only if it is not empty and not null. * //from w w w . j a va 2s. co m * @param object the object to add to * @param name the name of the array, if added * @param value the value to add */ public static void addNonEmptyJsonProperty(JsonObject object, String name, Object value) { if (value == null) return; if (value instanceof AbstractAttributeGroup) { AbstractAttributeGroup castValue = (AbstractAttributeGroup) value; if (!castValue.isEmpty()) { if (Boolean.valueOf(PropertyReader.getProperty("output.json.inlineAttributes"))) { JsonObject enclosure = castValue.getJSONObject(); for (Entry<String, JsonElement> entry : enclosure.entrySet()) { object.add(entry.getKey(), entry.getValue()); } } else { object.add(name, castValue.getJSONObject()); } } } else if (value instanceof Boolean) { Boolean castValue = (Boolean) value; object.addProperty(name, castValue); } else if (value instanceof Double) { Double castValue = (Double) value; object.addProperty(name, castValue); } else if (value instanceof Integer) { Integer castValue = (Integer) value; object.addProperty(name, castValue); } else if (value instanceof JsonArray) { JsonArray castValue = (JsonArray) value; if (castValue.size() != 0) object.add(name, castValue); } else if (value instanceof JsonObject) { JsonObject castValue = (JsonObject) value; object.add(name, castValue); } else if (value instanceof String) { String castValue = (String) value; if (!Util.isEmpty(castValue)) object.addProperty(name, castValue); } else throw new IllegalArgumentException("Unexpected class for JSON property: " + value); }
From source file:business.DataGenerator.java
public String getData(int amount, String properties) { JsonArray names = new JsonArray(); JsonObject person = new JsonObject(); Gson gson = new Gson(); for (int i = 0; i < amount; i++) { person = new JsonObject(); if (properties.contains("fname")) { person.addProperty("fname", fnames.get(new Random().nextInt(6))); }//w w w . j av a 2 s .c o m if (properties.contains("lname")) { person.addProperty("lname", lnames.get(new Random().nextInt(6))); } if (properties.contains("street")) { person.addProperty("street", streets.get(new Random().nextInt(7))); } if (properties.contains("city")) { person.addProperty("city", cities.get(new Random().nextInt(7))); } // System.out.println(i+") "+gson.toJson(person)); names.add(person); } String jsonStr = gson.toJson(names); return jsonStr; }