/** * @deprecated Since 1.9, use {@link #writerWithDefaultPrettyPrinter} instead. */ @Deprecated public ObjectWriter defaultPrettyPrintingWriter() { return writerWithDefaultPrettyPrinter(); }
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); } }
public static void toJSON(final Object obj, final OutputStream stream, final boolean prettyPrint) { final ObjectMapper mapper = new ObjectMapper(); try { if (prettyPrint) { final ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); writer.writeValue(stream, obj); return; } mapper.writeValue(stream, obj); } catch (final Exception e) { throw new RuntimeException(e); } }
@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 IOException { // admin check - // allows when hadoop.security.instrumentation.requires.admin is set to false // when hadoop.security.instrumentation.requires.admin is set to true, checks if hadoop.security.authorization // is true and if the logged in user (via PAM or SPNEGO + kerberos) is in hive.users.in.admin.role list final ServletContext context = getServletContext(); if (!HttpServer.isInstrumentationAccessAllowed(context, request, response)) { LOG.warn("Unauthorized to perform GET action. remoteUser: {}", request.getRemoteUser()); return; } setResponseHeaders(response); ServletContext ctx = getServletContext(); AtomicBoolean isLeader = (AtomicBoolean) ctx.getAttribute("hs2.isLeader"); LOG.info("Returning isLeader: {}", isLeader); ObjectMapper mapper = new ObjectMapper(); mapper.writerWithDefaultPrettyPrinter().writeValue(response.getWriter(), isLeader); response.setStatus(HttpServletResponse.SC_OK); response.flushBuffer(); }
@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()); } } }
byte[] body = FORMATTER.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonObject) .getBytes("UTF-8");
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); }
private void listLoggers(final HttpServletResponse response) throws IOException { PrintWriter writer = null; try { writer = response.getWriter(); ConfLoggers confLoggers = new ConfLoggers(); Collection<LoggerConfig> loggerConfigs = conf.getLoggers().values(); loggerConfigs.forEach(lc -> confLoggers.getLoggers().add(new ConfLogger(lc.getName(), lc.getLevel().toString()))); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writerWithDefaultPrettyPrinter().writeValue(writer, confLoggers); } catch (IOException e) { LOG.error("Caught an exception while processing Log4j2 configuration request", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } finally { if (writer != null) { writer.close(); } } response.setStatus(HttpServletResponse.SC_OK); } }
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); } }
.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonObject) .getBytes("UTF-8");
failoverResponse.setSuccess(false); failoverResponse.setMessage(msg); mapper.writerWithDefaultPrettyPrinter().writeValue(response.getWriter(), failoverResponse); response.setStatus(HttpServletResponse.SC_FORBIDDEN); return; failoverResponse.setSuccess(true); failoverResponse.setMessage(msg); mapper.writerWithDefaultPrettyPrinter().writeValue(response.getWriter(), failoverResponse); response.setStatus(HttpServletResponse.SC_OK); } catch (Exception e) { failoverResponse.setSuccess(false); failoverResponse.setMessage(errMsg); mapper.writerWithDefaultPrettyPrinter().writeValue(response.getWriter(), failoverResponse); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
@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;
/** * @deprecated Since 1.9, use {@link #writerWithDefaultPrettyPrinter} instead. */ @Deprecated public ObjectWriter defaultPrettyPrintingWriter() { return writerWithDefaultPrettyPrinter(); }
public static void writeObjectToFile(String filename, Object object) { final ObjectMapper mapper = getMapper(); try { File resultFile = new File(filename); if(resultFile.exists()) { resultFile.delete(); } resultFile.createNewFile(); mapper.writerWithDefaultPrettyPrinter().writeValue(resultFile, object); } catch(Exception e) { throw new PerfTestException("Cannot write object to file "+filename, e); } }
public static <T> String marshalToJson(T object) throws IOException { LOG.debug("Reference Object: {}", object); String json = DEFAULT_OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object); LOG.debug("JSON: {}", json); return json; }
public static String nodeToJsonPretty(JsonNode a) { try { return Tools.MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(a); } catch (IOException e) { e.printStackTrace(); } return null; }