List of usage examples for com.google.gson GsonBuilder GsonBuilder
public GsonBuilder()
From source file:br.cefetmg.radar.services.GetGenders.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 { 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.// w ww .j av 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 { 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 . ja v a2 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 { 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 w w . j a va2 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 { 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./*from w w w. j a v a2s .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 { 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); } }
From source file:br.com.anteros.social.facebook.utils.JsonUtils.java
License:Apache License
private static Gson buildGson() { Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateTypeAdapter()).create(); return gson;//ww w .j a va 2 s. c om }
From source file:br.com.anteros.social.facebook.utils.JsonUtils.java
License:Apache License
public static <T> T fromJsonExcludeFields(String json, Class<T> cls) { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(json); if (cls.isArray() && element instanceof JsonArray == false) { JsonArray jsonArray = new JsonArray(); jsonArray.add(element);//from w w w. ja va 2 s.c o m Type listType = new TypeToken<T>() { }.getType(); return gson.fromJson(jsonArray, listType); } return gson.fromJson(json, cls); }
From source file:br.com.ateliens.service.CadastroEnderecos.java
public Endereco buscarEnderecoPor(String urlJson) { System.out.println("CHAMOU O SERVIO...."); final GsonBuilder gsonBuilder = new GsonBuilder(); final Gson gson = gsonBuilder.create(); Gson g = new Gson(); Endereco retornoEndereco = gson.fromJson(urlJson, Endereco.class); return retornoEndereco; }
From source file:br.com.bean.RestControllers.alunoController.java
@RequestMapping(value = "busca-alunos", method = RequestMethod.GET) public String get() throws ParseException { Session sessao = HibernateUtility.getSession(); Transaction transacao = sessao.beginTransaction(); try {//from w ww . ja va 2 s . c o m Criteria query = sessao.createCriteria(Aluno.class); Criterion ativo = Restrictions.eq("ativo", 1); query.add(ativo); List<Aluno> listaDeAlunos = query.list(); Type TipolistaDeAlunos = new TypeToken<List<Aluno>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(Collection.class, new CollectionDeserializer()) .create(); return gson.toJson(listaDeAlunos, TipolistaDeAlunos); } catch (HibernateException e) { transacao.rollback(); return null; } }
From source file:br.com.bean.RestControllers.faculdadeController.java
@RequestMapping(value = "busca-faculdades", method = RequestMethod.GET) public String get() throws ParseException { Session sessao = HibernateUtility.getSession(); Transaction transacao = sessao.beginTransaction(); try {/*w w w. j a v a2s .com*/ Criteria query = sessao.createCriteria(Faculdade.class); Criterion ativo = Restrictions.eq("ativo", 1); query.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE); query.add(ativo); List<Faculdade> listaDeFaculdades = query.list(); // Retira Referencia Circular for (Faculdade f : listaDeFaculdades) { f.setTurmas(null); } Type TipolistaDeFaculdades = new TypeToken<List<Faculdade>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(Collection.class, new CollectionDeserializer()) .create(); String faculdades = gson.toJson(listaDeFaculdades, TipolistaDeFaculdades); return faculdades; } catch (HibernateException e) { transacao.rollback(); return null; } catch (Exception e) { String erro = e.getMessage(); return null; } finally { sessao.close(); } }