List of usage examples for com.google.gson JsonObject add
public void add(String property, JsonElement value)
From source file:bootwildfly.ProblemaJsonSerializer.java
@Override public JsonElement serialize(Problema p, Type type, JsonSerializationContext jsc) { //jsc.serialize() JsonObject object = new JsonObject(); TypeToken<List<Teste>> typeTestes = new TypeToken<List<Teste>>() { };/*from w ww .ja va2s. c o m*/ JsonElement sumario = jsc.serialize(p.getSumario(), SumarioDeProblema.class); JsonElement testes = jsc.serialize(p.getTeste()); object.addProperty("id", p.getId()); object.addProperty("dica", p.getDica()); object.addProperty("isPublicado", p.isIsPublicado()); object.addProperty("isPublicado", p.isIsPublicado()); object.add("sumario", sumario); object.add("teste", testes); return object; }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
/** * Appends key and/or value to json/*from w w w . j av a 2 s .c o m*/ * * @param json string json which will be modified * @param key key that will be appended * @param value value that will be appended * @return the specified JSON string with appended key and/or value */ public static String appendValueToJson(String json, String key, Object value) { JsonElement jsonElement = parseObject(json); if (jsonElement != null) { if (jsonElement.isJsonPrimitive()) { JsonArray jsonArray = new JsonArray(); jsonArray.add(jsonElement); jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } else if (jsonElement.isJsonObject()) { if (key == null) { throw new IllegalArgumentException( "to append some value into a JsonObject the 'key' parameter must not be null"); } JsonObject jsonObject = (JsonObject) jsonElement; jsonObject.add(key, objectToJsonElementTree(value)); return jsonObject.toString(); } else if (jsonElement.isJsonArray()) { JsonArray jsonArray = (JsonArray) jsonElement; jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } } return getJsonElementAsString(jsonElement); }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonObjectSearchToJsonObjectFlattened(JsonObject jsonObject, JsonObject jsonObjectFlattened) { if (jsonObject == null || jsonObjectFlattened == null) { throw new IllegalArgumentException( "JsonObject and/or JsonObjectFlattened parameter(s) must not be null"); }//from www . j av a2s. c o m for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { deepJsonObjectSearchToJsonObjectFlattened((JsonObject) value, jsonObjectFlattened); } else if (value.isJsonArray()) { // Creates new flattened JsonArray instance. JsonArray jsonArray = new JsonArray(); // Iterates recursively in JsonArray (value), checking content and populating the flattened JsonArray instance. deepJsonArraySearchToJsonArrayFlattened((JsonArray) value, jsonArray); // Adds the flattened JsonArray instance in the flattened JsonObject instance. jsonObjectFlattened.add(key, jsonArray); } else { // FIXME - WORKAROUND // Attention: A map can not contain duplicate keys. So, the // duplicate key is concatenated with a counter, to avoid data // loss. I could not think of anything better yet. if (jsonObjectFlattened.has(key)) { jsonObjectFlattened.add(key + COUNTER.getAndIncrement(), value); } else { jsonObjectFlattened.add(key, value); } } } }
From source file:br.edu.ifc.fraiburgo.rubble.servlets.StatusServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf8"); String start = request.getParameter("start"); String limit = request.getParameter("limit"); PrintWriter out = response.getWriter(); response.setContentType("application/json"); response.setHeader("Cache-control", "no-cache, no-store"); response.setHeader("Pragma", "no-cache"); response.setHeader("Expires", "-1"); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET,POST"); response.setHeader("Access-Control-Allow-Headers", "Content-Type"); response.setHeader("Access-Control-Max-Age", "86400"); boolean sucess = false; JsonArray arrayObj = new JsonArray(); JsonObject myObj = new JsonObject(); List<Comentario> comments = new ArrayList(); if (request.getParameter("action").equals("change")) { try {//from w w w .j av a 2s.c o m Postagem p = new PostagemController() .getPostagemById(Integer.parseInt(request.getParameter("idPostagem"))); Usuario u = new UsuarioController() .getUsuarioById(Integer.parseInt(request.getParameter("idUsuario"))); Status s = new Status(); s.setAtivo(true); s.setData(new Date()); s.setIdPostagem(p); s.setIdUsuario(u); s.setStatus(request.getParameter("status")); s = new StatusController().mergeStatus(s); new StatusController().setAllStatusInactive(s.getIdStatus(), p); s.setIdPostagem(null); s.setIdUsuario(null); Gson gson = new Gson(); JsonElement statusObj = gson.toJsonTree(s); myObj.add("status", statusObj); sucess = true; } catch (Exception e) { e.printStackTrace(); } } myObj.addProperty("success", sucess); out.println(myObj.toString()); out.close(); }
From source file:br.unicamp.cst.bindings.soar.JSoarCodelet.java
License:Open Source License
public void addToJson(JsonObject newBranch, JsonObject json, String property) { json.add(property, newBranch); }
From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java
License:Open Source License
public JsonObject getOutputLinkJSON() { JsonObject json = new JsonObject(); try {//ww w .j ava2s .c om if (getAgent() != null) { List<Wme> Commands = getOutputLink_WME(); setOutputLinkAsString(getWMEStringOutput()); for (Wme command : Commands) { String commandType = command.getAttribute().toString(); json.add(commandType, new JsonObject()); String parameter = command.getAttribute().toString(); String parvalue = command.getValue().toString(); Iterator<Wme> children = command.getChildren(); while (children.hasNext()) { Wme child = children.next(); parameter = child.getAttribute().toString(); parvalue = child.getValue().toString(); Float floatvalue = tryParseFloat(parvalue); if (floatvalue != null) { json.get(commandType).getAsJsonObject().addProperty(parameter, floatvalue); } else { json.get(commandType).getAsJsonObject().addProperty(parameter, parvalue); } } } } } catch (Exception e) { logger.severe("Error while creating SOAR Kernel" + e); } setJsonOutputLinkAsString(json.toString()); return (json); }
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 {// w w w . j a va 2 s . c om 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 w w . j a va 2 s . c om*/ 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, JsonObject 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.add(treeNodes[i], value); } else {/*from w w w . ja v a2 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 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 w w.j a v a 2 s.c om } else { temp = createJsonFromString(newBranch.substring(newNodes[0].length() + 1), value); json.add(newNodes[0], temp); } } else { json.addProperty(newNodes[0], value); } }