List of usage examples for com.google.gson GsonBuilder GsonBuilder
public GsonBuilder()
From source file:actions.ListeEvenementsAction.java
@Override public String execute(HttpServletRequest request) { ServiceMetier sm = new ServiceMetier(); List<DemandeEvenement> le = sm.obtenirDemandesFuturesNonComplet(); // TODO : a modifier, ca doit etre le commande complete. if (le != null) { request.setAttribute("ListActivite", le); } else {/* w ww .jav a 2 s.c om*/ request.setAttribute("ListActivite", "NULL"); } JsonArray jsonListe = new JsonArray(); for (DemandeEvenement de : le) { JsonObject jsonActivite = new JsonObject(); jsonActivite.addProperty("id", de.getId()); jsonActivite.addProperty("denomination", de.getActivity().getDenomination()); jsonActivite.addProperty("date", de.getDate().toString()); jsonActivite.addProperty("moment", de.getDay_moment().toString()); jsonActivite.addProperty("tarif", de.getActivity().getPayant()); jsonActivite.addProperty("nb_participants", de.getListSize()); jsonActivite.addProperty("nb_max", de.getActivity().getNbParticipants()); jsonActivite.addProperty("payant", de.getActivity().getPayant()); jsonListe.add(jsonActivite); } //Objet JSON "conteneur" JsonObject container = new JsonObject(); container.add("activites", jsonListe); //Serialisation & Ecriture sur le flux de sortie Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(container); return json; }
From source file:actions.ListeLieuxAction.java
@Override public String execute(HttpServletRequest request) { ServiceMetier sm = new ServiceMetier(); List<Lieu> ll = sm.obtenirLieux(); if (ll != null) { request.setAttribute("ListLieu", ll); } else {// w ww .ja va2s.co m request.setAttribute("ListLieu", "NULL"); } JsonArray jsonListe = new JsonArray(); for (Lieu lieu : ll) { JsonObject jsonActivite = new JsonObject(); jsonActivite.addProperty("id", lieu.getId()); jsonActivite.addProperty("denomination", lieu.getDenomination()); jsonActivite.addProperty("lat", lieu.getLatitude()); jsonActivite.addProperty("lng", lieu.getLongitude()); jsonActivite.addProperty("adresse", lieu.getAdresse()); jsonListe.add(jsonActivite); } //Objet JSON "conteneur" JsonObject container = new JsonObject(); container.add("lieux", jsonListe); //Serialisation & Ecriture sur le flux de sortie Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(container); return json; }
From source file:actions.MomentsAction.java
@Override public String execute(HttpServletRequest request) { ServiceMetier sm = new ServiceMetier(); List<Moment> lm = sm.obtenirMoments(); if (lm != null) { request.setAttribute("ListMoment", lm); } else {/*from w w w.ja v a2 s . co m*/ request.setAttribute("ListMoment", "NULL"); } JsonArray jsonListe = new JsonArray(); for (Moment moment : lm) { JsonObject jsonActivite = new JsonObject(); jsonActivite.addProperty("id", moment.ordinal()); jsonActivite.addProperty("denomination", moment.name()); jsonListe.add(jsonActivite); } //Objet JSON "conteneur" JsonObject container = new JsonObject(); container.add("moments", jsonListe); //Serialisation & Ecriture sur le flux de sortie Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(container); return json; }
From source file:actions.ValiderAction.java
@Override public String execute(HttpServletRequest request) { ServiceMetier sm = new ServiceMetier(); long prix = Long.parseLong(request.getParameter("prix")); long id_lieu = Long.parseLong(request.getParameter("lieu")); long id_demande = Long.parseLong(request.getParameter("id")); Evenement evt = null;//from w w w. j a v a2s . c o m Lieu lieu = null; //Recherche du lieu List<Lieu> ll = sm.obtenirLieux(); for (Lieu l : ll) { if (l.getId() == id_lieu) { lieu = l; break; } } //Recherche de la d'vnement List<Evenement> le = sm.obtenirEvenementAValider(); for (Evenement e : le) { if (e.getId() == id_demande) { if (e instanceof EvenementPayant) { System.out.println("EvenementPayant"); evt = sm.validerEvenement((EvenementPayant) e, lieu, prix); } if (e instanceof EvenementGratuit) { System.out.println("EvenementGratuit"); evt = sm.validerEvenement((EvenementGratuit) e, lieu); } break; } } JsonObject jsonResponse = new JsonObject(); if (evt == null) { // Evenement pas OK jsonResponse.addProperty("Validation", "KO"); } else { jsonResponse.addProperty("Validation", "OK"); } //Serialisation & Ecriture sur le flux de sortie Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(jsonResponse); return json; }
From source file:adams.data.conversion.JsonToString.java
License:Open Source License
/** * Performs the actual conversion.// ww w .jav a2s . co 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.conversion.ReportToJson.java
License:Open Source License
/** * Performs the actual conversion.//from w w w . ja v a 2s . co m * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { GsonBuilder builder; Gson gson; JsonObject jobj; jobj = ReportJsonUtils.toJson((Report) m_Input); builder = new GsonBuilder(); if (m_PrettyPrinting) builder.setPrettyPrinting(); gson = builder.create(); return gson.toJson(jobj); }
From source file:adams.data.io.output.AbstractSimpleJsonReportWriter.java
License:Open Source License
/** * Performs the actual writing./*w ww .ja va 2s . c o m*/ * * @param data the data to write * @return true if successfully written */ @Override protected boolean writeData(T data) { boolean result; GsonBuilder builder; Gson gson; JsonObject jobj; jobj = ReportJsonUtils.toJson(data); builder = new GsonBuilder(); if (m_PrettyPrinting) builder.setPrettyPrinting(); gson = builder.create(); result = FileUtils.writeToFile(m_Output.getAbsolutePath(), gson.toJson(jobj), false); if (!result) getLogger().severe("Error writing report #" + data.getDatabaseID() + " to " + m_Output); return result; }
From source file:adams.data.io.output.ViaAnnotationsReportWriter.java
License:Open Source License
/** * Performs the actual writing./*from ww w . j av a 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.flow.sink.JsonFileWriter.java
License:Open Source License
/** * Executes the flow item.//from w w w. j a v a 2s .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.//w w w . java2s. c om * * @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; }