@VisibleForTesting public String dumpJson() throws Exception { ObjectMapper jsonMapper = new ObjectMapper().registerModule( new MetricsModule(TimeUnit.MILLISECONDS, TimeUnit.MILLISECONDS, false)); return jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(metricRegistry); }
ByteArrayOutputStream out = new ByteArrayOutputStream(1024); JsonEncoding encoding = getJsonEncoding(getMimeType(headers)); JsonGenerator generator = this.objectMapper.getFactory().createGenerator(out, encoding); if (view != null) { this.objectMapper.writerWithView(view).writeValue(generator, payload); this.objectMapper.writeValue(generator, payload); Writer writer = new StringWriter(); if (view != null) { this.objectMapper.writerWithView(view).writeValue(writer, payload);
private byte[] writeObject(Object o) { try { return this.mapper.writer().writeValueAsBytes(o); } catch (JsonProcessingException e) { throw new AssertionError(e); } }
@Override protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable MimeType mimeType, ResolvableType elementType, @Nullable Map<String, Object> hints) { return (this.ssePrettyPrinter != null && MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mimeType) && writer.getConfig().isEnabled(SerializationFeature.INDENT_OUTPUT) ? writer.with(this.ssePrettyPrinter) : writer); }
@Override public void writeValue(OutputStream out, Object value) throws IOException, JsonGenerationException, JsonMappingException { writerFor(type).with(schema).writeValue(out, value); } @Override
@Override public byte[] writeValueAsBytes(Object value) throws JsonProcessingException { return writerFor(type).with(schema).writeValueAsBytes(value); }
@Override @SuppressWarnings("unchecked") public void execute() throws MojoExecutionException { try { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); Map<String, List<Map<String, String>>> map = (Map<String, List<Map<String, String>>>) mapper.readValue(input, LinkedHashMap.class); Map config = new LinkedHashMap(); config.put("extension", "revapi.ignore"); final List<Map> nodes = new ArrayList<>(); map.forEach((code, instances) -> { nodes.addAll(instances.stream() .map(instance -> { Map node = new LinkedHashMap(); node.put("code", code); node.putAll(instance); return node; }) .collect(Collectors.toList())); config.put("configuration", nodes); }); mapper.writer() .withDefaultPrettyPrinter() .writeValue(output, singletonList(config)); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } }
public static <T extends Message> T decodeProtoYamlFile(Path path, T prototype) throws IOException { ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); ObjectMap map = yamlMapper.readerFor(ObjectMap.class).readValue(path.toFile()); ObjectMapper jsonMapper = new ObjectMapper(new JsonFactory()); String json = jsonMapper.writerFor(ObjectMap.class).writeValueAsString(map); return decodeProtoJson(json, prototype); }
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(object);
private void writeFile(ParquetTableMetadataDirs parquetTableMetadataDirs, Path p, FileSystem fs) throws IOException { JsonFactory jsonFactory = new JsonFactory(); jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false); jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); ObjectMapper mapper = new ObjectMapper(jsonFactory); SimpleModule module = new SimpleModule(); mapper.registerModule(module); OutputStream os = fs.create(p); mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadataDirs); os.flush(); os.close(); }
public static byte[] sha1digest(final Object obj) throws IOException { final String json = NORMALIZING_OBJECT_WRITER.writeValueAsString(obj); final Map<String, Object> map = OBJECT_MAPPER.readValue(json, MAP_TYPE); return sha1digest(map); }
@Deprecated public void write(Path path) throws IOException { new ObjectMapper(new JsonFactory()).writerWithDefaultPrettyPrinter().withoutAttribute("inverseFileIds").writeValue(path.toFile(), this); }
private void initialize() throws IOException { this.generator = new JsonFactory().createGenerator(outputStream).configure(Feature.AUTO_CLOSE_TARGET, false); initializer.execute(this.generator); this.writer = new ObjectMapper() .writerFor(clazz) .with(SerializationFeature.FLUSH_AFTER_WRITE_VALUE) .writeValuesAsArray(this.generator); } };
public <T> String writeToString(T object) { try { ObjectWriter writer = objectWriterByClass.get(object.getClass()); if (writer == null) { return mapper.writeValueAsString(object); } return writer.writeValueAsString(object); } catch (IOException e) { throw new IllegalArgumentException("Cannot encode provided object", e); } }
httpResponseHandler.handleResponse(httpResponse, null); try { List<DataSegmentChangeRequest> changeRequests = ServerTestHelper.MAPPER.readValue( request.getContent().array(), new TypeReference<List<DataSegmentChangeRequest>>() new ByteArrayInputStream( ServerTestHelper.MAPPER .writerWithType(HttpLoadQueuePeon.RESPONSE_ENTITY_TYPE_REF) .writeValueAsBytes(statuses)
/** * Convert Java object to a JSON string. * * @param val Java object * @param pretty enable/disable pretty print * @return JSON string. */ public static String toJsonString(Object val, boolean pretty) { try { return pretty ? mapper.writerWithDefaultPrettyPrinter().with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsString(val) : mapper.writeValueAsString(val); } catch (Exception e) { throw new RuntimeException(e); } }
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding); try { writePrefix(generator, object); this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer()); if (filters != null) { objectWriter = objectWriter.with(filters); objectWriter = objectWriter.forType(javaType); SerializationConfig config = objectWriter.getConfig(); if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) { objectWriter = objectWriter.with(this.ssePrettyPrinter); objectWriter.writeValue(generator, value); generator.flush();
/** * Write the actual JSON content to the stream. * @param stream the output stream to use * @param object the value to be rendered, as returned from {@link #filterModel} * @throws IOException if writing failed */ protected void writeContent(OutputStream stream, Object object) throws IOException { JsonGenerator generator = this.objectMapper.getFactory().createGenerator(stream, this.encoding); writePrefix(generator, object); Object value = object; Class<?> serializationView = null; FilterProvider filters = null; if (value instanceof MappingJacksonValue) { MappingJacksonValue container = (MappingJacksonValue) value; value = container.getValue(); serializationView = container.getSerializationView(); filters = container.getFilters(); } ObjectWriter objectWriter = (serializationView != null ? this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer()); if (filters != null) { objectWriter = objectWriter.with(filters); } objectWriter.writeValue(generator, value); writeSuffix(generator, object); generator.flush(); }
@Override public TaskStatus apply(TaskStatus taskStatus) { try { log.info( "Task completed with status: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskStatus) ); final File statusFileParent = statusFile.getParentFile(); if (statusFileParent != null) { FileUtils.forceMkdir(statusFileParent); } jsonMapper.writeValue(statusFile, taskStatus); return taskStatus; } catch (Exception e) { throw Throwables.propagate(e); } } }
Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null); ObjectWriter writer = (jsonView != null ? getObjectMapper().writerWithView(jsonView) : getObjectMapper().writer()); writer = writer.forType(javaType); getObjectMapper().getFactory().createGenerator(outputStream, encoding); writer.writeValue(generator, value); release = false;