@Override public String toString() { try { return WRITER.writeValueAsString(innerMetrics); } catch (IOException e) { LOG.warn("Failed to dump to Json.", e); return ExceptionUtils.getStackTrace(e); } } }
public static String toJSON(final Object obj, final boolean prettyPrint) { final ObjectMapper mapper = new ObjectMapper(); try { if (prettyPrint) { final ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); return writer.writeValueAsString(obj); } return mapper.writeValueAsString(obj); } catch (final Exception e) { throw new RuntimeException(e); } }
private static String jacksonPrettyPrint( final String json ) throws Exception { Object myObject = MAPPER.readValue( json, Object.class ); return WRITER.writeValueAsString( myObject ); } }
public static String prettyPrint( Object item ) throws IOException { return OBJECT_MAPPER.writer().withDefaultPrettyPrinter().writeValueAsString( item ); } }
@JsonIgnore public String toJson() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String endpointKey = URLDecoder.decode(request.getParameter(ENDPOINT_KEY_PARAMETER), "UTF-8"); try { String json = configurationService.findEndpointConfigurationByEndpointKeyHash(endpointKey); String fileName = MessageFormatter.arrayFormat(EP_CONF_FILE_NAME_PATTERN, new Object[]{endpointKey}).getMessage(); Object jsonObject = FORMATTER.readValue(json, Object.class); byte[] body = FORMATTER.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonObject) .getBytes("UTF-8"); ServletUtils.prepareDisposition(request, response, fileName); response.setContentType(JSON); response.setContentLength(body.length); response.setBufferSize(BUFFER); response.getOutputStream().write(body); response.flushBuffer(); } catch (Exception ex) { LOG.error("Unexpected error in ProfileDownloadServlet.doGet: ", ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to get file: " + ex.getMessage()); } } }
private Schema parseDependencies(CTLSchemaDto schema, final Schema.Parser parser) throws Exception { if (schema.getDependencySet() != null) { for (CTLSchemaDto dependency : schema.getDependencySet()) { this.parseDependencies(dependency, parser); } } ObjectNode object = new ObjectMapper().readValue(schema.getBody(), ObjectNode.class); object.remove(DEPENDENCIES); String body = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object); return parser.parse(body); }
.writeValueAsString(jsonObject) .getBytes("UTF-8"); ServletUtils.prepareDisposition(request, response, fileName);
public void outputJson(PrintWriter writer) throws LlapStatusCliException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY); mapper.setVisibility(JsonMethod.ALL, Visibility.NON_PRIVATE); try { writer.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(appStatusBuilder)); } catch (IOException e) { LOG.warn("Failed to create JSON", e); throw new LlapStatusCliException(ExitCode.LLAP_JSON_GENERATION_ERROR, "Failed to create JSON", e); } }
byte[] body = FORMATTER .writerWithDefaultPrettyPrinter() .writeValueAsString(jsonObject) .getBytes("UTF-8");
@Override public FileData shallowExport(CTLSchemaDto schema) { try { FileData result = new FileData(); result.setContentType(JSON); result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion())); // Format schema body Object json = FORMATTER.readValue(schema.getBody(), Object.class); result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json) .getBytes()); return result; } catch (Exception cause) { throw new RuntimeException("An unexpected exception occured: " + cause.toString()); } }
@Override public FileData flatExport(CTLSchemaDto schema) { try { FileData result = new FileData(); result.setContentType(JSON); result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion())); // Get schema body String body = flatExportAsString(schema); // Format schema body Object json = FORMATTER.readValue(body, Object.class); result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json) .getBytes()); return result; } catch (Exception cause) { throw new RuntimeException("An unexpected exception occured: " + cause.toString()); } }
public void printJson(final SessionState.LogHelper console) { try { waitForReturnSessionEvent(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); // serialize json based on field annotations only objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withSetterVisibility(JsonAutoDetect.Visibility.NONE)); String wmContextJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); console.printInfo(""); console.printInfo(WM_EVENTS_TITLE); console.printInfo(SEPARATOR); console.printInfo(wmContextJson); console.printInfo(SEPARATOR); console.printInfo(""); } catch (Exception e) { LOG.warn("Unable to serialize WMContext to json.", e); } }
body = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object); schema.setBody(body); return schema;
private void getMetrics(final HttpServletRequest request, final String pathInfo, final HttpServletResponse response) throws Exception { String queryString = request.getQueryString(); QueryParam queryParam = getCassandraQueryParam(queryString); ListenableFuture<List<RawNumericMetric>> future = metricService.findData(queryParam); try{ List<RawNumericMetric> metrics = future.get(5000, TimeUnit.MILLISECONDS); String result = metricResultWriter.writeValueAsString(metrics); response.getWriter().print(result); response.setContentLength(result.length()); response.setStatus(HttpServletResponse.SC_OK); }catch (TimeoutException e){ response.getWriter().print(QUERY_CASSANDRA_TIMEOUT); response.setContentLength(QUERY_CASSANDRA_TIMEOUT.length()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }catch (ExecutionException e){ response.getWriter().print(QUERY_CASSANDRA_ERROR); response.setContentLength(QUERY_CASSANDRA_ERROR.length()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
public void load() throws IOException { // on load, we just rewind the iterator for provided volumes. rewind(); LOG.trace("load({}, {}): loaded iterator {}: {}", getStorageID(), bpid, name, WRITER.writeValueAsString(state)); } }
public static <T> String toJson(T t) { String json = null; try { json = ow.writeValueAsString(t); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json; }
private void getCounters(final HttpServletRequest request, final String pathInfo, final HttpServletResponse response) throws Exception { String queryString = request.getQueryString(); QueryParam queryParam = getCassandraQueryParam(queryString); ListenableFuture<List<Counter>> future = metricService.findCounters((String)queryParam.getParameters().get(QueryParam.METRIC_NAME), (String)queryParam.getParameters().get(QueryParam.GROUP_ID)); try{ List<Counter> counters = future.get(5000, TimeUnit.MILLISECONDS); String result = counterResultWriter.writeValueAsString(counters); response.getWriter().print(result); response.setContentLength(result.length()); response.setStatus(HttpServletResponse.SC_OK); }catch (TimeoutException e){ response.getWriter().print(QUERY_CASSANDRA_TIMEOUT); response.setContentLength(QUERY_CASSANDRA_TIMEOUT.length()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }catch (ExecutionException e){ response.getWriter().print(QUERY_CASSANDRA_ERROR); response.setContentLength(QUERY_CASSANDRA_ERROR.length()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
/** * Convert a SearchRequest to JSON. * @param request The search request. * @return The JSON representation of the SearchRequest. */ public static Optional<String> toJSON(Object request) { Optional<String> json = Optional.empty(); if(request != null) { try { json = Optional.of( new ObjectMapper() .writer() .withDefaultPrettyPrinter() .writeValueAsString(request)); } catch (Throwable t) { LOG.error("Failed to convert request to JSON", t); } } return json; }
/** * Generate bson from java object. * * @param obj * the obj * @return the BSON object */ protected BSONObject generateBSONFromJavaObject(Object obj) { ObjectWriter ow = new ObjectMapper().writer(); String json = null; try { json = ow.writeValueAsString(obj); return (BSONObject) JSON.parse(json); } catch (JsonGenerationException | JsonMappingException e) { throw new KunderaException( "Error in converting BSON Object from Java Object due to error in JSON generation/mapping. Caused BY:", e); } catch (IOException e) { throw new KunderaException("Error in converting BSON Object from Java Object. Caused BY:", e); } }