List of usage examples for com.google.gson GsonBuilder GsonBuilder
public GsonBuilder()
From source file:blobstoretest.DownloadServlet.java
License:Apache License
/** * Serves a file by means a blobKey.// w w w .jav a2 s .c o m */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String blobKey = req.getParameter("blobKey"); if (blobKey == null) { ErrorResponse error = new ErrorResponse(); error.setCode(HttpServletResponse.SC_BAD_REQUEST); ErrorMessage message = new ErrorMessage(); message.setMessage("The Blobkey parameter is required"); resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); resp.setContentType("application/json"); GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); resp.getWriter().print(gson.toJson(error)); } else { blobstore.serve(new BlobKey(blobKey), resp); } }
From source file:blobstoretest.OAuthFilter.java
License:Apache License
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try {/*from www. j a va 2 s.c o m*/ oauth.getCurrentUser(); chain.doFilter(req, res); } catch (OAuthRequestException e) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); HttpServletResponse httpRes = (HttpServletResponse) res; httpRes.setStatus(HttpServletResponse.SC_UNAUTHORIZED); httpRes.setContentType("application/json"); ErrorResponse error = new ErrorResponse(); error.setCode(HttpServletResponse.SC_UNAUTHORIZED); ErrorMessage message = new ErrorMessage(); message.setMessage("This service requires authentication"); httpRes.getWriter().print(gson.toJson(error)); } }
From source file:blobstoretest.UploadServlet.java
License:Apache License
/** * Registers the uploaded files in the Datastore. *//* ww w. j a v a 2 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:blockplus.exports.ContextRepresentation.java
License:Open Source License
public ContextRepresentation(final Context game) { this.gson = new GsonBuilder().enableComplexMapKeySerialization() .registerTypeAdapter(Polyomino.class, new PolyominoSerializer()) .registerTypeAdapter(IPosition.class, new PositionSerializer()).create(); this.context = game; }
From source file:blog.BlogHelper.java
License:Apache License
public static List<ImageHolder> loadImagesFrom(String pathname) { List<ImageHolder> imageHolders = new ArrayList<>(); try {//from w w w.ja v a 2 s . c o 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();/* ww w . jav a 2 s .com*/ return gson.fromJson( FileUtils.readFileToString(new File("src/test/resources/blog/sets/love_hate_candidates.json"), Charset.defaultCharset()), new TypeToken<List<ImageHolder>>() { }.getType()); }
From source file:bogdanrechi.lansator.MainWindow.java
License:Open Source License
/** * Load application configuration/*from w ww. j a v a2 s .c o m*/ */ private void loadConfiguration() { GsonBuilder builder = new GsonBuilder(); _mainWindow._gson = builder.serializeNulls().setPrettyPrinting().create(); try { _mainWindow._config = _mainWindow._gson.fromJson( FileContent.readTextFile(Platform.CURRENT_DIRECTORY + "lansator.json"), Configuration.class); } catch (JsonSyntaxException | StringException e) { _mainWindow._log.fatal(e.getMessage()); _mainWindow.displayFatalErrorAndExit("CANNOT_LOAD_CONFIGURATION_FILE"); } }
From source file:bogdanrechi.lansator.updater.App.java
License:Open Source License
public static void main(String[] args) { if (args.length != 1) { Console.println("Usage: LansatorUpdater path_to_lansator.json"); return;//from w w w . j a v a 2s . com } Configuration _config; GsonBuilder builder = new GsonBuilder(); Gson _gson = builder.serializeNulls().setPrettyPrinting().create(); try { _config = _gson.fromJson(FileContent.readTextFile(args[0]), Configuration.class); String imagesFolder = Files.getParent(args[0]) + Platform.FILE_SEPARATOR + "images"; for (ProgramsGroupItem groupItem : _config.programsGroups) { setItemImageFile(imagesFolder, groupItem.imageFile, groupItem); for (ProgramItem programItem : groupItem.programs) { setItemImageFile(imagesFolder, programItem.imageFile, programItem); } } Console.println(_gson.toJson(_config) + "\r\n"); FileContent.writeTextFile(_gson.toJson(_config) + "\r\n", args[0]); } catch (JsonSyntaxException | StringException e) { Console.println(e.getMessage()); } }
From source file:br.cefetmg.radar.services.Filter.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . ja va2s. 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 { 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.// 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 { 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(); }