Example usage for com.fasterxml.jackson.core JsonGenerator setPrettyPrinter

List of usage examples for com.fasterxml.jackson.core JsonGenerator setPrettyPrinter

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator setPrettyPrinter.

Prototype

public JsonGenerator setPrettyPrinter(PrettyPrinter pp) 

Source Link

Document

Method for setting a custom pretty printer, which is usually used to add indentation for improved human readability.

Usage

From source file:io.debezium.document.JacksonWriter.java

protected void configure(JsonGenerator generator) {
    if (pretty)/*from   www  . j av  a  2  s  .co  m*/
        generator.setPrettyPrinter(new DefaultPrettyPrinter());
}

From source file:com.ning.metrics.action.hdfs.data.Row.java

public String toJSON() throws IOException {
    final StringWriter s = new StringWriter();
    final JsonGenerator generator = new JsonFactory().createJsonGenerator(s);
    generator.setPrettyPrinter(new DefaultPrettyPrinter());
    toJSON(generator);//from w w  w .j  av a2s .  com
    generator.close();

    return s.toString();
}

From source file:org.seedstack.seed.core.internal.diagnostic.DefaultDiagnosticReporter.java

void writeDiagnosticReport(Map<String, Object> diagnosticInfo, OutputStream outputStream) throws IOException {
    JsonGenerator jsonGenerator = null;
    try {//from  w w w  .  ja va2 s  . c  om
        jsonGenerator = JSON_FACTORY
                .createGenerator(new OutputStreamWriter(outputStream, Charset.forName("UTF-8").newEncoder()));
        jsonGenerator.setPrettyPrinter(DEFAULT_PRETTY_PRINTER);
        jsonGenerator.writeObject(diagnosticInfo);
        jsonGenerator.flush();
    } finally {
        if (jsonGenerator != null) {
            try {
                jsonGenerator.close();
            } catch (IOException e) {
                LOGGER.warn("Unable to close diagnostic stream", e);
            }
        }
    }
}

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 ava 2s .  c  o 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();
}

From source file:com.boundary.zoocreeper.Backup.java

public void backup(OutputStream os) throws InterruptedException, IOException, KeeperException {
    JsonGenerator jgen = null;
    ZooKeeper zk = null;//from  w w w.  j  av a  2  s .  c  o  m
    try {
        zk = options.createZooKeeper(LOGGER);
        jgen = JSON_FACTORY.createGenerator(os);
        if (options.prettyPrint) {
            jgen.setPrettyPrinter(new DefaultPrettyPrinter());
        }
        jgen.writeStartObject();
        if (zk.exists(options.rootPath, false) == null) {
            LOGGER.warn("Root path not found: {}", options.rootPath);
        } else {
            doBackup(zk, jgen, options.rootPath);
        }
        jgen.writeEndObject();
    } finally {
        if (jgen != null) {
            jgen.close();
        }
        if (zk != null) {
            zk.close();
        }
    }
}

From source file:org.emfjson.jackson.streaming.StreamWriter.java

private void prepare(JsonGenerator generator, Resource resource) {
    if (isPrepared)
        return;//from w  w  w .  j  a v  a2  s .com

    if (options.indentOutput) {
        DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
        generator.setPrettyPrinter(printer);
    }

    if (refWriter == null) {
        refWriter = new RefAsObjectWriter();
    }

    if (!converters.containsKey(EcorePackage.Literals.EDATE)) {
        converters.put(EcorePackage.Literals.EDATE, new DefaultDateConverter());
    }

    referenceWriter = new References(cache, resource, refWriter, options);
    values = new Values(converters);

    isPrepared = true;
}

From source file:com.bazaarvoice.jsonpps.PrettyPrintJson.java

public void prettyPrint(List<File> inputFiles, File outputFile) throws IOException {
    JsonFactory factory = new JsonFactory();
    factory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
    if (!strict) {
        factory.enable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
        factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
        factory.enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
        factory.enable(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS);
        factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        factory.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
        factory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    }/* w  ww  .  j  a  v a  2s. co m*/

    ObjectMapper mapper = null;
    if (sortKeys) {
        mapper = new ObjectMapper(factory);
        mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
        mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
    }

    // Open the output stream and create the Json emitter.
    JsonGenerator generator;
    File tempOutputFile = null;
    if (STDINOUT.equals(outputFile)) {
        generator = factory.createGenerator(stdout, JsonEncoding.UTF8);
    } else if (!caseInsensitiveContains(inputFiles, outputFile)) {
        generator = factory.createGenerator(outputFile, JsonEncoding.UTF8);
    } else {
        // Writing to an input file.. use a temp file to stage the output until we're done.
        tempOutputFile = getTemporaryFileFor(outputFile);
        generator = factory.createGenerator(tempOutputFile, JsonEncoding.UTF8);
    }
    try {
        // Separate top-level objects by a newline in the output.
        String newline = System.getProperty("line.separator");
        generator.setPrettyPrinter(new DefaultPrettyPrinter(newline));

        if (wrap) {
            generator.writeStartArray();
        }

        for (File inputFile : inputFiles) {
            JsonParser parser;
            if (STDINOUT.equals(inputFile)) {
                parser = factory.createParser(stdin);
            } else {
                parser = factory.createParser(inputFile);
            }
            try {
                while (parser.nextToken() != null) {
                    copyCurrentStructure(parser, mapper, 0, generator);
                }
            } finally {
                parser.close();
            }
        }

        if (wrap) {
            generator.writeEndArray();
        }

        generator.writeRaw(newline);
    } finally {
        generator.close();
    }
    if (tempOutputFile != null && !tempOutputFile.renameTo(outputFile)) {
        System.err.println("error: unable to rename temporary file to output: " + outputFile);
        System.exit(1);
    }
}