List of usage examples for com.google.gson GsonBuilder create
public Gson create()
From source file:blobstoretest.UploadServlet.java
License:Apache License
/** * Registers the uploaded files in the Datastore. *///from ww w . ja v a2 s. c o m @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json"); GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Map<String, List<BlobInfo>> blobs; try { blobs = blobstore.getBlobInfos(req); } catch (IllegalStateException e) { ErrorResponse error = new ErrorResponse(); error.setCode(HttpServletResponse.SC_BAD_REQUEST); ErrorMessage message = new ErrorMessage(); message.setMessage("Blobstore service illegal state"); resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); resp.getWriter().print(gson.toJson(error)); return; } List<BlobInfo> infos = blobs.get("files"); Iterable<FileData> files = Iterables.transform(infos, blobInfoToEntity()); Iterable<Entity> entities = Iterables.transform(files, fileDataToEntity()); datastore.put(entities); resp.setStatus(HttpServletResponse.SC_OK); resp.getWriter().print(gson.toJson(files)); }
From source file:blog.BlogHelper.java
License:Apache License
public static List<ImageHolder> loadImagesFrom(String pathname) { List<ImageHolder> imageHolders = new ArrayList<>(); try {//from ww w . j ava2s . co m GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory()); gsonBuilder.registerTypeAdapter(Class.class, new ClassTypeAdapter()); Gson gson = gsonBuilder.create(); imageHolders .addAll(gson.fromJson(FileUtils.readFileToString(new File(pathname), Charset.defaultCharset()), new TypeToken<List<ImageHolder>>() { }.getType())); } catch (IOException e) { e.printStackTrace(); } return imageHolders; }
From source file:blog.identify.IdentifyMultipleFacesExample.java
License:Apache License
private static List<ImageHolder> candidates() throws IOException { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory()); gsonBuilder.registerTypeAdapter(Class.class, new ClassTypeAdapter()); Gson gson = gsonBuilder.create(); return gson.fromJson( FileUtils.readFileToString(new File("src/test/resources/blog/sets/love_hate_candidates.json"), Charset.defaultCharset()), new TypeToken<List<ImageHolder>>() { }.getType());/* w w w . j av a2 s . c o m*/ }
From source file:br.cefetmg.radar.services.Filter.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w . j a va 2s . 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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { IncidentDAO incidentDAO = new IncidentDAO(); int aux1 = Integer.parseInt(request.getParameter("armed")); String armed = ""; switch (aux1) { case -1: armed += "%"; break; case 1: armed += "1"; break; case 2: armed += "2"; break; case 3: armed += "3"; break; } int gender_id = Integer.parseInt(request.getParameter("gender")); String gender = ""; switch (gender_id) { case 0: gender += "%' or i.user.gender is null"; break; default: gender += Integer.toString(gender_id); gender += "'"; } String violence = request.getParameter("violence"); if (violence.equals("-1")) { violence = "false or i.violence = true"; } else if (violence.equalsIgnoreCase("false")) { violence = "false"; } else if (violence.equalsIgnoreCase("true")) { violence = "true"; } int period = Integer.parseInt(request.getParameter("period")); String period_init = ""; String period_end = ""; switch (period) { case 0: period_init += "00:00"; period_end += "23:59"; break; case 1: period_init += "00:00"; period_end += "05:59"; break; case 2: period_init += "06:00"; period_end += "11:59"; break; case 3: period_init += "12:00"; period_end += "17:59"; break; case 4: period_init += "18:00"; period_end += "23:59"; break; } List<Incident> list = incidentDAO.filterIncidents(armed, gender, violence, period_init, period_end); ArrayList<Integer> incidents_id = new ArrayList<Integer>(); for (int i = 0; i < list.size(); i++) { incidents_id.add(list.get(i).getId()); } out.println(gson.toJson(incidents_id)); } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } }
From source file:br.cefetmg.radar.services.GetColors.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w.j ava 2s .c om*/ * * @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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { ColorDAO colorDAO = new ColorDAO(); List<Color> list = colorDAO.GetColors(); for (int i = 0; i < list.size(); i++) { list.get(i).setUsers(null); } out.println(gson.toJson(list)); } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } out.close(); }
From source file:br.cefetmg.radar.services.GetGenders.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w ww. j av a 2 s. 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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { GenderDAO genderDAO = new GenderDAO(); List<Gender> list = genderDAO.GetGenders(); for (int i = 0; i < list.size(); i++) { list.get(i).setUsers(null); } out.println(gson.toJson(list)); } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } out.close(); }
From source file:br.cefetmg.radar.services.GetServices.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w . j av a2 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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { ServiceDAO serviceDAO = new ServiceDAO(); List<Incident> list = serviceDAO.GetServices(); out.println(gson.toJson(list)); } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } out.close(); }
From source file:br.cefetmg.radar.services.GetTransports.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//www .j a va2s .com * * @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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { TransportDAO transportDAO = new TransportDAO(); List<Incident> list = transportDAO.GetTransports(); out.println(gson.toJson(list)); } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } out.close(); }
From source file:br.cefetmg.radar.services.GetUser.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . ja v a 2 s.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 { response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); int iduser = Integer.parseInt(request.getParameter("id")); try (PrintWriter out = response.getWriter()) { try { UserDAO userDAO = new UserDAO(); User user = userDAO.getById(iduser); if (user != null) { user.setIncidents(null); if (user.getColor() != null) { user.getColor().setUsers(null); } if (user.getGender() != null) { user.getGender().setUsers(null); } if (user.getLocations() != null) { user.setLocations(null); } out.println(gson.toJson(user)); } else { out.println(gson.toJson(new Result(Result.USER_NOT_FOUND))); } } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } } }
From source file:br.cefetmg.radar.services.GetUserByUsername.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* w w w . jav a 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 { GsonBuilder b = new GsonBuilder(); b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create(); response.setContentType("text/json;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { UserDAO userDAO = new UserDAO(); String username = request.getParameter("username"); User user = userDAO.getByUsername(username); if (user != null) { if (user.getColor() != null) { user.getColor().setUsers(null); } if (user.getGender() != null) { user.getGender().setUsers(null); } for (Incident i : user.getIncidents()) { i.setUser(null); } out.println(gson.toJson(user)); } else { out.println(gson.toJson(new Result(Result.USER_DOESNT_EXISTS))); response.setStatus(404); } } catch (Exception ex) { StringBuilder error = new StringBuilder(); for (StackTraceElement element : ex.getStackTrace()) { error.append(element.toString()); error.append("\n"); } out.println(gson.toJson(new Result(Result.ERRO, error.toString()))); response.setStatus(500); } }