List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValueAsString
@SuppressWarnings("resource") public String writeValueAsString(Object value) throws JsonProcessingException
From source file:org.shareok.data.webserv.WebUtil.java
public static ModelAndView getRepoTypeList(ModelAndView model) throws JsonProcessingException { int index = 0; Map repoTypeMap = new HashMap(); for (String repoType : DataUtil.REPO_TYPES) { repoTypeMap.put(index, repoType); index++;// w w w.j a va 2 s.co m } String repoTypeListStr; ObjectMapper mapper = new ObjectMapper(); repoTypeListStr = mapper.writeValueAsString(repoTypeMap); model.addObject("repoTypeList", repoTypeListStr); return model; }
From source file:org.neo4j.jdbc.http.driver.Neo4jStatement.java
/** * Convert the list of query to a JSON compatible with Neo4j endpoint. * * @param queries List of cypher queries. * @param mapper mapper/*w w w. ja va2 s .c om*/ * @return The JSON string that correspond to the body of the API call * @throws SQLException sqlexception */ public static String toJson(List<Neo4jStatement> queries, ObjectMapper mapper) throws SQLException { StringBuilder sb = new StringBuilder(); try { sb.append("{\"statements\":"); sb.append(mapper.writeValueAsString(queries)); sb.append("}"); } catch (JsonProcessingException e) { throw new SQLException("Can't convert Cypher statement(s) into JSON"); } return sb.toString(); }
From source file:com.amazonaws.services.kinesis.clientlibrary.proxies.util.KinesisLocalFileDataCreator.java
/** * Creates a temp file (in default temp file location) with fake Kinesis data records. * Records will be put in all shards.//from w w w .j a va 2s . c o m * @param fileNamePrefix Prefix for the name of the temp file * @param shardList List of shards (we use the shardId and sequenceNumberRange fields) * @param numRecordsPerShard Num records per shard (the shard sequenceNumberRange should be large enough * for us to allow these many records with some "holes") * @return File with stream data filled in * @throws IOException Thrown if there are issues creating/updating the file */ public static File generateTempDataFile(List<Shard> shardList, int numRecordsPerShard, String fileNamePrefix) throws IOException { File file = File.createTempFile(fileNamePrefix, FILE_NAME_SUFFIX); try (BufferedWriter fileWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) { ObjectMapper objectMapper = new ObjectMapper(); String serializedShardList = objectMapper .writeValueAsString(new KinesisLocalFileProxy.SerializedShardList(shardList)); fileWriter.write(serializedShardList); fileWriter.newLine(); BigInteger sequenceNumberIncrement = new BigInteger("0"); long timestamp = STARTING_TIMESTAMP; for (int i = 0; i < numRecordsPerShard; i++) { for (Shard shard : shardList) { BigInteger sequenceNumber = new BigInteger( shard.getSequenceNumberRange().getStartingSequenceNumber()) .add(sequenceNumberIncrement); String endingSequenceNumber = shard.getSequenceNumberRange().getEndingSequenceNumber(); BigInteger maxSequenceNumber = KinesisLocalFileProxy.MAX_SEQUENCE_NUMBER; if (endingSequenceNumber != null) { maxSequenceNumber = new BigInteger(endingSequenceNumber); } if (maxSequenceNumber.compareTo(sequenceNumber) != 1) { throw new IllegalArgumentException("Not enough space in shard"); } String partitionKey = PARTITION_KEY_PREFIX + shard.getShardId() + generateRandomString(PARTITION_KEY_LENGTH); String data = generateRandomString(DATA_LENGTH); // Allow few records to have the same timestamps (to mimic real life scenarios). timestamp = (i % DIVISOR == 0) ? timestamp : timestamp + 1; String line = shard.getShardId() + "," + sequenceNumber + "," + partitionKey + "," + data + "," + timestamp; fileWriter.write(line); fileWriter.newLine(); sequenceNumberIncrement = sequenceNumberIncrement.add(BigInteger.ONE); sequenceNumberIncrement = sequenceNumberIncrement .add(new BigInteger(NUM_BITS, randomGenerator)); } } } return file; }
From source file:com.microsoft.alm.client.utils.JsonHelper.java
/** * Map to QueryParameters/*from ww w . ja va 2 s . co m*/ * * @param model * @return Map<String, String> */ public static Map<String, String> toQueryParametersMap(final Object model) { final ObjectMapper objectMapper = getObjectMapper(); try { return objectMapper.readValue(objectMapper.writeValueAsString(model), new TypeReference<Map<String, String>>() { }); } catch (final Exception e) { log.error(e.getMessage(), e); throw new VssServiceException(e.getMessage(), e); } }
From source file:com.basistech.rosette.examples.ExampleBase.java
/** * Converts a response to JSON string//from w w w.j av a2 s.c o m * * @param response {@link com.basistech.rosette.apimodel.Response Response} from RosetteAPI * @return the json string. * @throws JsonProcessingException if the Jackson library throws an error serializing. */ protected static String responseToJson(Response response) throws JsonProcessingException { ObjectMapper mapper = ApiModelMixinModule.setupObjectMapper(new ObjectMapper()); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return mapper.writeValueAsString(response); }
From source file:com.xeiam.xchange.rest.JSONUtils.java
/** * Given any object return the JSON String representation of it * // ww w. j a v a 2 s . com * @param object * @param objectMapper * @return */ public static String getJSONString(Object object, ObjectMapper objectMapper) { Assert.notNull(object, "object cannot be null"); Assert.notNull(objectMapper, "objectMapper cannot be null"); try { return objectMapper.writeValueAsString(object); } catch (IOException e) { // Rethrow as runtime exception throw new ExchangeException("Problem getting JSON String", e); } }
From source file:org.wso2.app.catalog.utils.CommonUtils.java
/** * Convert given object to json formatted string. * @param obj Object to be converted.//from ww w . ja v a 2 s . c o m * @return Json formatted string. * @throws AppCatalogException */ public static String toJSON(Object obj) throws AppCatalogException { try { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } catch (JsonMappingException e) { throw new AppCatalogException("Error occurred while mapping class to json", e); } catch (JsonGenerationException e) { throw new AppCatalogException("Error occurred while generating json", e); } catch (IOException e) { throw new AppCatalogException("Error occurred while reading the stream", e); } }
From source file:com.world.watch.worldwatchcron.util.PushToUser.java
public static void push(List<String> data, String userId) { try {//from w w w . j a va 2 s . com InputStream jsonStream = PushToUser.class.getResourceAsStream("/parsePush.json"); ObjectMapper mapper = new ObjectMapper(); PushData push = mapper.readValue(jsonStream, PushData.class); push.getWhere().setUsername(userId); push.getData().setKeywords(data); String json = mapper.writeValueAsString(push); HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()) .build(); HttpPost post = new HttpPost(URL); post.setHeader("X-Parse-Application-Id", "WhqWj009luOxOtIH3rM9iWJICLdf0NKbgqdaui8Q"); post.setHeader("X-Parse-REST-API-Key", "lThhKObAz1Tkt092Cl1HeZv4KLUsdATvscOaGN2y"); post.setHeader("Content-Type", "application/json"); logger.debug("JSON to push {}", json.toString()); StringEntity strEntity = new StringEntity(json); post.setEntity(strEntity); httpClient.execute(post); logger.debug("Pushed {} to userId {}", data.toString(), userId); } catch (Exception ex) { logger.error("Push Failed for {} ", userId, ex); } }
From source file:si.mazi.rescu.JSONUtils.java
/** * Given any object return the JSON String representation of it * * @param object/* w w w .j ava 2 s. co m*/ * @param objectMapper * @return */ public static String getJSONString(Object object, ObjectMapper objectMapper) { AssertUtil.notNull(object, "object cannot be null"); AssertUtil.notNull(objectMapper, "objectMapper cannot be null"); try { return objectMapper.writeValueAsString(object); } catch (IOException e) { // Rethrow as runtime exception throw new RuntimeException("Problem getting JSON String", e); } }
From source file:uk.ac.sanger.cgp.wwdocker.actions.Utils.java
public static String objectToJson(Object obj) { ObjectMapper mapper = new ObjectMapper(); String json;//w w w. j a v a 2s .co m try { json = mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { throw new RuntimeException(e.getMessage(), e); } return json; }