List of usage examples for com.fasterxml.jackson.core.util DefaultPrettyPrinter DefaultPrettyPrinter
public DefaultPrettyPrinter()
From source file:org.apache.arrow.vector.ipc.JsonFileWriter.java
public JsonFileWriter(File outputFile, JSONWriteConfig config) throws IOException { MappingJsonFactory jsonFactory = new MappingJsonFactory(); this.generator = jsonFactory.createGenerator(outputFile, JsonEncoding.UTF8); if (config.pretty) { DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(NopIndenter.instance); this.generator.setPrettyPrinter(prettyPrinter); }//from www . j a v a 2 s. co m // Allow writing of floating point NaN values not as strings this.generator.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false); }
From source file:com.chenchengzhi.windtalkers.server.WindMessageFactory.java
@Override public HttpResponse serialize(Message message) throws IOException { Issue issue = message.get(Issue.class); if (issue != null) { return throwError(issue); }//w ww .java 2 s . co m HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); ObjectNode response = message.getResponseBody(); if (response != null) { httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json"); StringWriter writer = new StringWriter(); JsonGenerator generator = jsonFactory.createJsonGenerator(writer); generator.setPrettyPrinter(new DefaultPrettyPrinter()); response.serialize(generator, new DefaultSerializerProvider.Impl()); generator.flush(); httpResponse.setContent(ChannelBuffers.copiedBuffer(writer.getBuffer(), Charsets.UTF_8)); } httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.getContent().readableBytes()); return httpResponse; }
From source file:io.debezium.document.JacksonWriter.java
protected void configure(JsonGenerator generator) { if (pretty) generator.setPrettyPrinter(new DefaultPrettyPrinter()); }
From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java
public String render() throws IOException { final JsonFactory jsonFactory = new JsonFactory(); final StringWriter writer = new StringWriter(); final JsonGenerator generator = jsonFactory.createGenerator(writer); generator.setPrettyPrinter(new DefaultPrettyPrinter()); render(generator);//ww w . j a v a 2 s . c o m return writer.getBuffer().toString(); }
From source file:com.orange.ngsi.model.RegisterContextModelTest.java
@Test public void serializationSimpleRegisterContext() throws URISyntaxException, JsonProcessingException { RegisterContext registerContext = createRegisterContextTemperature(); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(registerContext); List<ContextRegistration> contextRegistrationList = JsonPath.read(json, "$.contextRegistrations[*]"); assertEquals(1, contextRegistrationList.size()); List<EntityId> entityIdList = JsonPath.read(json, "$.contextRegistrations[0].entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("Room*", JsonPath.read(json, "$.contextRegistrations[0].entities[0].id")); assertEquals("Room", JsonPath.read(json, "$.contextRegistrations[0].entities[0].type")); assertEquals(true, JsonPath.read(json, "$.contextRegistrations[0].entities[0].isPattern")); List<ContextRegistrationAttribute> attributes = JsonPath.read(json, "$.contextRegistrations[0].attributes[*]"); assertEquals(1, attributes.size());//from ww w . j a v a2s.c o m assertEquals("temperature", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].name")); assertEquals("float", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].type")); assertEquals(false, JsonPath.read(json, "$.contextRegistrations[0].attributes[0].isDomain")); assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.contextRegistrations[0].providingApplication")); assertEquals("PT10S", JsonPath.read(json, "$.duration")); }
From source file:org.wrml.runtime.format.application.vnd.wrml.design.schema.SchemaDesignFormatter.java
@Override public void writeModel(final OutputStream out, final Model model, final ModelWriteOptions writeOptions) throws ModelWritingException { if (!(model instanceof Schema)) { throw new ModelWritingException("The \"" + getFormatUri() + "\" format cannot write the model.", null, this); }/* w ww.j a v a 2s .c om*/ final Schema schema = (Schema) model; final ObjectNode rootNode; final ObjectWriter objectWriter; try { // TODO: Should this ObjectMapper be stored in a field? final ObjectMapper objectMapper = new ObjectMapper(); rootNode = createSchemaDesignObjectNode(objectMapper, schema); objectWriter = objectMapper.writer(new DefaultPrettyPrinter()); objectWriter.writeValue(out, rootNode); } catch (final Exception e) { throw new ModelWritingException(getClass().getSimpleName() + " encounter an error while attempting to write a SchemaDesign. Message: " + e.getMessage(), null, this); } }
From source file:com.chenchengzhi.windtalkers.server.WindMessageFactory.java
protected HttpResponse throwError(Issue issue) throws IOException { HttpResponse errorResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(issue.getStatusCode().getCode())); ObjectNode errorNode = issue.translate(); StringWriter writer = new StringWriter(); JsonGenerator generator = jsonFactory.createJsonGenerator(writer); generator.setPrettyPrinter(new DefaultPrettyPrinter()); errorNode.serialize(generator, new DefaultSerializerProvider.Impl()); generator.flush();//from w ww . j a v a 2 s . co m errorResponse.setContent(ChannelBuffers.copiedBuffer(writer.getBuffer(), Charsets.UTF_8)); errorResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json"); errorResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, errorResponse.getContent().readableBytes()); return errorResponse; }
From source file:com.vmware.bdd.cli.commands.CommandsUtils.java
public static void prettyJsonOutput(Object object, String fileName) throws Exception { OutputStream out = null;/*from ww w . j av a 2 s . co m*/ try { if (fileName != null) { out = new FileOutputStream(fileName); } else { out = System.out; } JsonFactory factory = new JsonFactory(); JsonGenerator generator = factory.createJsonGenerator(out); ObjectMapper mapper = getMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); generator.setCodec(mapper); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); DefaultPrettyPrinter.Indenter indenter = new DefaultPrettyPrinter.Lf2SpacesIndenter(); prettyPrinter.indentArraysWith(indenter); generator.setPrettyPrinter(prettyPrinter); generator.writeObject(object); writeEndingMsgToScreen(fileName); } finally { if (out != null && !(out instanceof PrintStream)) { out.close(); } } }
From source file:org.wrml.runtime.format.application.schema.json.JsonSchemaFormatter.java
@Override public void writeModel(final OutputStream out, final Model model, final ModelWriteOptions writeOptions) throws ModelWritingException, UnsupportedOperationException { final Context context = getContext(); final SchemaLoader schemaLoader = context.getSchemaLoader(); if (!(model instanceof Schema)) { throw new UnsupportedOperationException(getClass().getSimpleName() + " can be used to read and write schemas only (" + schemaLoader.getSchemaSchemaUri() + ")"); }/* ww w . j a v a 2 s. c o m*/ final Schema wrmlSchema = (Schema) model; final ObjectWriter objectWriter = new ObjectMapper().writer(new DefaultPrettyPrinter()); final JsonSchema jsonSchema = schemaLoader.getJsonSchemaLoader().load(wrmlSchema); try { objectWriter.writeValue(out, jsonSchema.getRootNode()); } catch (final Exception e) { throw new ModelWritingException( getClass().getSimpleName() + " encounter an error while attempting to write a JSON Schema (" + model + "). Message: " + e.getMessage(), null, this); } }
From source file:com.ning.metrics.action.hdfs.reader.HdfsListing.java
@SuppressWarnings({ "unchecked", "unused" }) public void toJson(final OutputStream out, final boolean pretty) throws IOException { final String parentPath = getParentPath() == null ? "" : getParentPath(); final JsonGenerator generator = new JsonFactory().createJsonGenerator(out); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); if (pretty) { generator.setPrettyPrinter(new DefaultPrettyPrinter()); }//w w w. j a v a 2s. co m generator.writeStartObject(); generator.writeObjectField(JSON_LISTING_PATH, getPath()); generator.writeObjectField(JSON_LISTING_PARENT_PATH, parentPath); generator.writeArrayFieldStart(JSON_LISTING_ENTRIES); // Important: need to flush before appending pre-serialized events generator.flush(); for (HdfsEntry entry : getEntries()) { entry.toJson(generator); } generator.writeEndArray(); generator.writeEndObject(); generator.close(); }