List of usage examples for com.fasterxml.jackson.databind ObjectWriter writeValueAsString
@SuppressWarnings("resource") public String writeValueAsString(Object value) throws JsonProcessingException
From source file:com.samlikescode.stackoverflow.questions.q31009815.ContainerTest.java
@Test public void testContainerSerialization_ObjectMapperConfig() throws JsonProcessingException { ObjectMapper om = new ObjectMapper().registerModule(new ContainerModule2()); // .registerModule(new Element1Module()) // .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); ObjectWriter objectWriter = om.writerWithDefaultPrettyPrinter(); System.out.println("Full container: " + objectWriter.writeValueAsString(buildFakeContainerFull())); System.out.println("No Element 1: " + objectWriter.writeValueAsString(buildFakeContainerNoElement1())); System.out.println("No Element 2: " + objectWriter.writeValueAsString(buildFakeContainerNoElement2())); System.out.println("Empty Container: " + objectWriter.writeValueAsString(buildFakeContainerEmpty())); }
From source file:com.orange.ngsi.model.SubscribeContextModelTest.java
@Test public void serializationSimpleSubscribeContext() throws IOException { SubscribeContext subscribeContext = null; try {/*from w w w . j av a 2s. c o m*/ subscribeContext = createSubscribeContextTemperature(); } catch (URISyntaxException e) { Assert.fail(e.getMessage()); } ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(subscribeContext); List<EntityId> entityIdList = JsonPath.read(json, "$.entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("Room1", JsonPath.read(json, "$.entities[0].id")); assertEquals("Room", JsonPath.read(json, "$.entities[0].type")); assertEquals(false, JsonPath.read(json, "$.entities[0].isPattern")); assertEquals("P1M", JsonPath.read(json, "$.duration")); List<String> attributes = JsonPath.read(json, "$.attributes[*]"); assertEquals(1, attributes.size()); assertEquals("temperature", JsonPath.read(json, "$.attributes[0]")); assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.reference")); List<NotifyCondition> notifyConditionList = JsonPath.read(json, "$.notifyConditions[*]"); assertEquals(1, notifyConditionList.size()); assertEquals(NotifyConditionEnum.ONTIMEINTERVAL.getLabel(), JsonPath.read(json, "$.notifyConditions[0].type")); List<String> condValues = JsonPath.read(json, "$.notifyConditions[0].condValues[*]"); assertEquals(1, condValues.size()); assertEquals("PT10S", JsonPath.read(json, "$.notifyConditions[0].condValues[0]")); }
From source file:com.orange.cepheus.broker.persistence.SubscriptionsRepository.java
/** * Save a subscription updated/*from ww w. j a va2 s. c om*/ * @param subscription * @throws SubscriptionPersistenceException */ public void updateSubscription(Subscription subscription) throws SubscriptionPersistenceException { try { //serialization ObjectWriter writer = mapper.writer(); String susbcribeContextString = writer.writeValueAsString(subscription.getSubscribeContext()); String expirationDate = subscription.getExpirationDate().toString(); jdbcTemplate.update("update t_subscriptions set expirationDate=? , subscribeContext=? where id=?", expirationDate, susbcribeContextString, subscription.getSubscriptionId()); } catch (Exception e) { throw new SubscriptionPersistenceException(e); } }
From source file:cz.muni.fi.mushroomhunter.restclient.LocationUpdateSwingWorker.java
@Override protected Integer doInBackground() throws Exception { DefaultTableModel model = (DefaultTableModel) restClient.getTblLocation().getModel(); int selectedRow = restClient.getTblLocation().getSelectedRow(); String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD; byte[] plainCredsBytes = plainCreds.getBytes(); byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); String base64Creds = new String(base64CredsBytes); RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); List<MediaType> mediaTypeList = new ArrayList<>(); mediaTypeList.add(MediaType.ALL);/*from w ww.j a va2s . co m*/ headers.setAccept(mediaTypeList); headers.add("Authorization", "Basic " + base64Creds); HttpEntity request = new HttpEntity<>(headers); ResponseEntity<LocationDto> responseEntity = restTemplate.exchange( RestClient.SERVER_URL + "pa165/rest/location/" + RestClient.getLocationIDs().get(selectedRow), HttpMethod.GET, request, LocationDto.class); LocationDto locationDto = responseEntity.getBody(); locationDto.setName(restClient.getTfLocationName().getText()); locationDto.setDescription(restClient.getTfLocationDescription().getText()); locationDto.setNearCity(restClient.getTfLocationNearCity().getText()); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(locationDto); request = new HttpEntity(json, headers); restTemplate.exchange(RestClient.SERVER_URL + "pa165/rest/location", HttpMethod.PUT, request, LocationDto.class); return selectedRow; }
From source file:com.orange.cepheus.broker.persistence.SubscriptionsRepository.java
/** * Save a subscription.//from ww w. jav a2 s . com * @param subscription * @throws SubscriptionPersistenceException */ public void saveSubscription(Subscription subscription) throws SubscriptionPersistenceException { try { //Mapping from model to database model ObjectWriter writer = mapper.writer(); String susbcribeContextString = writer.writeValueAsString(subscription.getSubscribeContext()); String expirationDate = subscription.getExpirationDate().toString(); //insert into database jdbcTemplate.update("insert into t_subscriptions(id,expirationDate,subscribeContext) values(?,?,?)", subscription.getSubscriptionId(), expirationDate, susbcribeContextString); } catch (Exception e) { throw new SubscriptionPersistenceException(e); } }
From source file:com.orange.ngsi.model.RegisterContextModelTest.java
@Test public void serializationSimpleRegisterContext() throws URISyntaxException, JsonProcessingException { RegisterContext registerContext = createRegisterContextTemperature(); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(registerContext); List<ContextRegistration> contextRegistrationList = JsonPath.read(json, "$.contextRegistrations[*]"); assertEquals(1, contextRegistrationList.size()); List<EntityId> entityIdList = JsonPath.read(json, "$.contextRegistrations[0].entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("Room*", JsonPath.read(json, "$.contextRegistrations[0].entities[0].id")); assertEquals("Room", JsonPath.read(json, "$.contextRegistrations[0].entities[0].type")); assertEquals(true, JsonPath.read(json, "$.contextRegistrations[0].entities[0].isPattern")); List<ContextRegistrationAttribute> attributes = JsonPath.read(json, "$.contextRegistrations[0].attributes[*]"); assertEquals(1, attributes.size());/* w w w. ja v a 2 s . c o m*/ assertEquals("temperature", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].name")); assertEquals("float", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].type")); assertEquals(false, JsonPath.read(json, "$.contextRegistrations[0].attributes[0].isDomain")); assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.contextRegistrations[0].providingApplication")); assertEquals("PT10S", JsonPath.read(json, "$.duration")); }
From source file:com.basistech.rosette.dm.json.array.ExtendedPropertyTest.java
@Test public void testExtendedPropertyOnAttribute() throws Exception { // 012345678901234567890 String rawText = "Cuthbert Girdlestone"; AnnotatedText.Builder builder = new AnnotatedText.Builder().data(rawText); ListAttribute.Builder<com.basistech.rosette.dm.EntityMention> emListBuilder = new ListAttribute.Builder<>( com.basistech.rosette.dm.EntityMention.class); com.basistech.rosette.dm.EntityMention.Builder emBuilder = new com.basistech.rosette.dm.EntityMention.Builder( 0, 20, "PERSON"); emBuilder.extendedProperty("extra_key", "extra_value"); emListBuilder.add(emBuilder.build()); builder.entityMentions(emListBuilder.build()); AnnotatedText text = builder.build(); ObjectMapper mapper = AnnotatedDataModelArrayModule.setupObjectMapper(new ObjectMapper()); ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter(); String json = objectWriter.writeValueAsString(text); AnnotatedText deserialized = mapper.readValue(json, AnnotatedText.class); assertEquals("extra_value", deserialized.getEntityMentions().get(0).getExtendedProperties().get("extra_key")); }
From source file:cz.muni.fi.mushroomhunter.restclient.MushroomCreateSwingWorker.java
@Override protected Void doInBackground() throws Exception { MushroomDto mushroomDto = new MushroomDto(); mushroomDto.setName(restClient.getTfMushroomName().getText()); mushroomDto.setType(cz.fi.muni.pa165.mushroomhunter.api.Type .valueOf(restClient.getComboBoxMushroomType().getSelectedItem().toString())); //to create date object only month is used, day and year are fixed values String dateInString = restClient.getComboBoxMushroomStartOfOccurence().getSelectedItem().toString() + " 1, 2000"; SimpleDateFormat formatter = new SimpleDateFormat("MMMM d, yyyy", new Locale("en_US")); mushroomDto.setStartOfOccurence(formatter.parse(dateInString)); //to create date object only month is used, day and year are fixed values dateInString = restClient.getComboBoxMushroomEndOfOccurence().getSelectedItem().toString() + " 1, 2000"; mushroomDto.setEndOfOccurence(formatter.parse(dateInString)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); List<MediaType> mediaTypeList = new ArrayList<>(); mediaTypeList.add(MediaType.ALL);/*ww w.ja v a2 s . co m*/ headers.setAccept(mediaTypeList); ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(mushroomDto); String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD; byte[] plainCredsBytes = plainCreds.getBytes(); byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); String base64Creds = new String(base64CredsBytes); headers.add("Authorization", "Basic " + base64Creds); HttpEntity request = new HttpEntity(json, headers); RestTemplate restTemplate = new RestTemplate(); Long[] result = restTemplate.postForObject(RestClient.SERVER_URL + "pa165/rest/mushroom", request, Long[].class); System.out.println("Id of the created mushroom: " + result[0]); RestClient.getMushroomIDs().add(result[0]); return null; }
From source file:edu.ucsd.crbs.cws.App.java
License:asdf
public static void uploadPreviewWorkflowFile(final String url, Workflow w) throws Exception { ObjectMapper om = new ObjectMapper(); File tmpFile = File.createTempFile("preview", ".json"); try {//from ww w.j a v a 2s . com BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile)); ObjectWriter ow = om.writer(); bw.write(ow.writeValueAsString(w)); bw.flush(); bw.close(); RunCommandLineProcess procRunner = new RunCommandLineProcessImpl(); System.out.println("TODO SWITCH THIS TO USE JERSEY CLIENT!!!\n" + "Attempting to run this command: curl -i -X POST --form '" + "_formexamplefile=@" + tmpFile.getAbsolutePath() + "' " + url); String res = procRunner.runCommandLineProcess("curl", "-i", "-X", "POST", "--form", "_formexamplefile=@" + tmpFile.getAbsolutePath(), url); System.out.println("\n"); System.out.println("--------------- URL to Preview ----------------"); System.out.println(res); System.out.println("-----------------------------------------------"); } finally { if (tmpFile != null) { tmpFile.delete(); } } }
From source file:com.ebay.pulsar.analytics.metricstore.druid.query.model.GroupByQuery.java
@Override public String toString() { String query = ""; ObjectWriter statswriter = new ObjectMapper().writerWithType(GroupByQuery.class); try {//from w ww .j a va 2s . c o m query = statswriter.writeValueAsString(this); } catch (IOException e) { logger.warn(e.getMessage()); ; } return query; }