public static String prettyPrint( Object item ) throws IOException { return OBJECT_MAPPER.writer().withDefaultPrettyPrinter().writeValueAsString( item ); } }
/** * 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; }
private static String prettyPrinted(Object json) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter(); return writer.writeValueAsString(json).replace(System.lineSeparator(), "<br>"); }
String objectToJson ( Object o ) { try { return new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString( o ); } catch ( Exception e ) { throw new RuntimeException( String.format( "Failed to convert object '%s' to JSON", o ), e ); } }
/** * Serialize the template to a JSON string * @return * @throws JsonGenerationException * @throws JsonMappingException * @throws IOException */ String getTemplateAsString() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter(); return writer.writeValueAsString(template); }
public static String prettyPrint( Object item ) throws IOException { return OBJECT_MAPPER.writer().withDefaultPrettyPrinter().writeValueAsString( item ); } }
/** * {@inheritDoc} */ @Override public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException { out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize())); try { ObjectWriter writer = _objectMapper.writerWithType(type); if (isPrettyPrint()) { writer = writer.withDefaultPrettyPrinter(); } writer.writeValue(out, obj); } finally { if (isCloseEnabled()) { out.close(); } } return ((CountingOutputStream)out).getCount(); }
/** * {@inheritDoc} */ @Override public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException { out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize())); try { ObjectWriter writer = _objectMapper.writerWithType(type); if (isPrettyPrint()) { writer = writer.withDefaultPrettyPrinter(); } writer.writeValue(out, obj); } finally { if (isCloseEnabled()) { out.close(); } } return ((CountingOutputStream)out).getCount(); }