List of usage examples for com.google.gson GsonBuilder create
public Gson create()
From source file:AppSpringLayout.java
protected ArrayList<String> searchToDisplayOnJList(String text, String imageType, String sizeType, String licenseType, String safeSearchType, String numberOfImg) { SearchForSimilarImages bingSearch = new SearchForSimilarImages(); // String responseBing = bingSearch.GetUrlContentAsString(bingToken, // text, imageType, sizeType, licenseType, // safeSearchType); String responseBing = bingSearch.GetUrlContentAsStringWithNumber(bingToken, text, imageType, sizeType, licenseType, safeSearchType, numberOfImg); GsonBuilder gsonBingBuilder = new GsonBuilder(); Gson gsonBing = gsonBingBuilder.create(); RootBing rootBing = gsonBing.fromJson(responseBing, RootBing.class); // created temporary array list with all urls ArrayList<Urls> originalUrls = rootBing.getValue(); // filtered only working links ArrayList<String> checkedUrls = checkLinks(originalUrls); // System.out.println("Links to display: "); // converted it to a string array linksResponse = new String[checkedUrls.size()]; linksResponse = checkedUrls.toArray(linksResponse); listModel.clear();//from ww w . ja va 2s . co m for (String item : linksResponse) { // String listItemText = item; displayImageOnJList(item); // shows only for the last one // TODO modify - may be with ListCellRenderer // subclass DefaultListCellRenderer and override the // getListCellRendererComponent() method // list.setToolTipText("<html><img src=\"" + item + "\">"); listModel.addElement(icon); } list.setModel(listModel); System.out.println("found all links"); progressBar.setVisible(false); lblFoundLinks.setText(""); return checkedUrls; }
From source file:AppSpringLayout.java
protected String analyse() { AnalyseImage computerVisionSearch = new AnalyseImage(); String response = computerVisionSearch.describeImage(originalImage, computerVisionImageToken); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); RootComputerVision root = gson.fromJson(response, RootComputerVision.class); String[] tags = root.getDescription().getTags(); System.out.println("============="); System.out.println("tags " + Arrays.toString(tags)); int numberOfTags; // limit number of tags displayed to maximum first five if (tags.length >= 5) { numberOfTags = 5;/* w ww. j a v a2 s . c o m*/ } else { numberOfTags = tags.length; } tagsTextArea.setText(""); numberOfImagesToSearchFor.setText(""); for (int i = 0; i < numberOfTags; i++) { tagsTextArea.append(tags[i] + "\n"); // get tags from string array to a string variable tagsString = tagsString + tags[i] + "%20"; } for (Captions currentCaption : root.getDescription().getCaptions()) { text = currentCaption.getText(); System.out.println("description: " + text); System.out.println("============="); descriptionTextArea.setText(text); } String textString = text.replace(" ", "%20"); return tagsString + textString; }
From source file:adams.data.conversion.ReportToJson.java
License:Open Source License
/** * Performs the actual conversion.//from w ww.j av a2 s . c om * * @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.//from w w w . j av a 2 s . co 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:alfio.manager.NotificationManager.java
License:Open Source License
@Autowired public NotificationManager(Mailer mailer, MessageSource messageSource, PlatformTransactionManager transactionManager, EmailMessageRepository emailMessageRepository, EventRepository eventRepository, EventDescriptionRepository eventDescriptionRepository, OrganizationRepository organizationRepository, ConfigurationManager configurationManager, FileUploadManager fileUploadManager, TemplateManager templateManager, TicketReservationRepository ticketReservationRepository) { this.messageSource = messageSource; this.mailer = mailer; this.emailMessageRepository = emailMessageRepository; this.eventRepository = eventRepository; this.organizationRepository = organizationRepository; this.tx = new TransactionTemplate(transactionManager); this.configurationManager = configurationManager; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Mailer.Attachment.class, new AttachmentConverter()); this.gson = builder.create(); attachmentTransformer = new EnumMap<>(Mailer.AttachmentIdentifier.class); attachmentTransformer.put(Mailer.AttachmentIdentifier.CALENDAR_ICS, (model) -> { Event event = eventRepository.findById(Integer.valueOf(model.get("eventId"), 10)); Locale locale = Json.fromJson(model.get("locale"), Locale.class); String description = eventDescriptionRepository.findDescriptionByEventIdTypeAndLocale(event.getId(), EventDescription.EventDescriptionType.DESCRIPTION, locale.getLanguage()).orElse(""); Organization organization = organizationRepository.getById(event.getOrganizationId()); return event.getIcal(description, organization.getName(), organization.getEmail()).orElse(null); });/* w w w . j a v a 2 s .c o m*/ attachmentTransformer.put(Mailer.AttachmentIdentifier.RECEIPT_PDF, (model) -> { String reservationId = model.get("reservationId"); Event event = eventRepository.findById(Integer.valueOf(model.get("eventId"), 10)); Locale language = Json.fromJson(model.get("language"), Locale.class); Map<String, Object> reservationEmailModel = Json.fromJson(model.get("reservationEmailModel"), new TypeReference<Map<String, Object>>() { }); //FIXME hack: reservationEmailModel should be a minimal and typed container reservationEmailModel.put("event", event); Optional<byte[]> receipt = TemplateProcessor.buildReceiptPdf(event, fileUploadManager, language, templateManager, reservationEmailModel); if (!receipt.isPresent()) { log.warn("was not able to generate the bill for reservation id " + reservationId + " for locale " + language); } return receipt.orElse(null); }); attachmentTransformer.put(Mailer.AttachmentIdentifier.TICKET_PDF, (model) -> { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Ticket ticket = Json.fromJson(model.get("ticket"), Ticket.class); try { TicketReservation reservation = ticketReservationRepository .findReservationById(ticket.getTicketsReservationId()); TicketCategory ticketCategory = Json.fromJson(model.get("ticketCategory"), TicketCategory.class); Event event = eventRepository.findById(ticket.getEventId()); Organization organization = organizationRepository .getById(Integer.valueOf(model.get("organizationId"), 10)); PDFTemplateGenerator pdfTemplateGenerator = TemplateProcessor.buildPDFTicket( Locale.forLanguageTag(ticket.getUserLanguage()), event, reservation, ticket, ticketCategory, organization, templateManager, fileUploadManager); pdfTemplateGenerator.generate().createPDF(baos); } catch (IOException e) { log.warn("was not able to generate ticket pdf for ticket with id" + ticket.getId(), e); } return baos.toByteArray(); }); }
From source file:allout58.mods.techtree.tree.TreeManager.java
License:Open Source License
public String getTreeAsString() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TechNode.class, new TechNodeGSON()); Gson gson = gsonBuilder.create(); TechNode[] nodes = currentTree.getRealNodes().toArray(new TechNode[currentTree.getNodes().size()]); return gson.toJson(nodes); // return treeString; }
From source file:allout58.mods.techtree.tree.TreeManager.java
License:Open Source License
public void makeFromReader(Reader reader) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TechNode.class, new TechNodeGSON()); gsonBuilder.registerTypeAdapter(ItemStack.class, new ItemStackGSON()); Gson gson = gsonBuilder.create(); TechNode[] nodes = gson.fromJson(reader, TechNode[].class); TechNode head = null;/*from w w w . j ava 2s . c o m*/ for (TechNode node : nodes) { if (node.getParentID().size() == 0) { head = node; } else { for (int parentID : node.getParentID()) { if (parentID == node.getId()) throw new RuntimeException("Tree node with a self-referencing parent: " + parentID); TechNode parent = nodes[parentID]; node.addParentNode(parent); parent.addChildNode(node); } } } currentTree = new TechTree(head); }
From source file:android.zetterstrom.com.forecast.ForecastClient.java
License:Apache License
private static Gson createGson() { final long MILLIS = 1000; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong() * MILLIS); }/* ww w .ja v a 2 s. co m*/ }); return builder.create(); }
From source file:angularBeans.util.AngularBeansUtil.java
License:Open Source License
public String getJson(Object object) { if (object instanceof Properties) { return new Gson().toJson(object); }/*from ww w . j av a 2 s .co m*/ GsonBuilder builder = new GsonBuilder().serializeNulls(); if (object == null) { return new GsonBuilder().serializeNulls().create().toJson(null); } Class clazz = object.getClass(); builder.registerTypeAdapter(LobWrapper.class, new ByteArrayJsonAdapter(cache)); Gson gson = builder.create(); return gson.toJson(object); }
From source file:angularBeans.util.AngularBeansUtils.java
License:LGPL
public void initJsonSerialiser() { GsonBuilder builder = new GsonBuilder().serializeNulls(); builder.registerTypeAdapter(LobWrapper.class, new LobWrapperJsonAdapter(cache)); builder.registerTypeAdapter(byte[].class, new ByteArrayJsonAdapter(cache, contextPath)); builder.registerTypeAdapter(LobWrapper.class, new JsonDeserializer<LobWrapper>() { @Override//from w w w .j av a 2 s. co m public LobWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { return null; } }); mainSerializer = builder.create(); }