Example usage for com.fasterxml.jackson.core.util DefaultPrettyPrinter indentArraysWith

List of usage examples for com.fasterxml.jackson.core.util DefaultPrettyPrinter indentArraysWith

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.util DefaultPrettyPrinter indentArraysWith.

Prototype

public void indentArraysWith(Indenter i) 

Source Link

Usage

From source file:com.mirth.connect.util.MirthJsonUtil.java

public static String prettyPrint(String input) {
    ObjectMapper mapper = new ObjectMapper(new JsonFactory());
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    try {//  w  w w  . j  a v  a2s  . c  o m
        // Modified Jackson's default pretty printer to separate each array element onto its own line
        DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
        prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
        JsonNode json = mapper.readTree(input);

        return mapper.writer(prettyPrinter).writeValueAsString(json);
    } catch (Exception e) {
        logger.warn("Error pretty printing json.", e);
    }

    return input;
}

From source file:com.evanzeimet.queryinfo.QueryInfoTestUtils.java

@SuppressWarnings("deprecation")
public static ObjectWriter createObjectWriter() {
    ObjectMapper objectMapper = createObjectMapper();

    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());

    return objectMapper.writer(prettyPrinter);
}

From source file:com.vmware.bdd.cli.commands.CommandsUtils.java

public static void prettyJsonOutput(Object object, String fileName) throws Exception {
    OutputStream out = null;/*from  w  w  w.j a  va  2s. c  o  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.forgerock.openig.migrate.Main.java

void execute(OutputStream stream) throws Exception {

    // Pre-parse the original files with JSON-Simple parser (which is more lenient than Jackson's one)
    File source = new File(sources.get(0));
    JSONParser parser = new JSONParser();
    JSONObject object = (JSONObject) parser.parse(new FileReader(source));

    // Then serialize again the structure (should clean up the JSON)
    StringWriter writer = new StringWriter();
    object.writeJSONString(writer);/*from  w w w  .  j a  v  a2s. c  om*/

    // Load migration actions to apply in order
    List<Action> actions = loadActions();

    // Parse the cleaned-up content with Jackson this time
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode node = (ObjectNode) mapper.readTree(writer.toString());

    // Apply migration actions
    for (Action action : actions) {
        node = action.migrate(node);
    }

    // Serialize the migrated content on the given stream (with pretty printer)
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultPrettyPrinter.Lf2SpacesIndenter.instance);
    mapper.writeTree(factory.createGenerator(stream).setPrettyPrinter(prettyPrinter), node);
}

From source file:com.amazon.feeds.SampleFeedGenerator.java

/**
 * The method for generating sample feeds.
 *
 * @param format The class containing the format specifications.
 * @param items The number of items to generate.
 * @param ext File extension.//from w ww .  jav a  2  s. c o m
 */
public void createSampleFeed(IFeedFormat format, int items, String ext) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    mapper.getFactory().setCharacterEscapes(format.getEscapeRules());

    // create output file
    String out = format.getFeedFormat() + "-" + items + "." + ext;
    // TODO: add XML support

    File outFile = new File(SAMPLE_PATH, out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }

    // populate sample feed
    System.out.println("Generating " + items + (items == 1 ? " item" : " items") + " for "
            + format.getProvider() + " feed at " + outFile.getAbsolutePath());
    format.populate(items);

    // write JSON to file
    if (format.usePrettyPrint()) {

        DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("   ", DefaultIndenter.SYS_LF);
        DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
        printer.indentObjectsWith(indenter);
        printer.indentArraysWith(indenter);
        mapper.writer(printer).writeValue(outFile, format);
    } else {
        mapper.writeValue(outFile, format);
    }
}

From source file:org.apache.arrow.vector.file.json.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  w w w.  j  a  v  a2 s  . c  o  m
}

From source file:org.wrml.runtime.format.application.json.JsonModelPrinter.java

public JsonModelPrinter(final JsonGenerator jsonGenerator, final ModelWriteOptions writeOptions) {

    _JsonGenerator = jsonGenerator;//from  w ww. j  a  va2  s  .c  o  m
    _WriteOptions = writeOptions;

    if (_WriteOptions.isPrettyPrint()) {
        final DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
        prettyPrinter.indentObjectsWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());
        prettyPrinter.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());
        _JsonGenerator.setPrettyPrinter(prettyPrinter);

        // _JsonGenerator.useDefaultPrettyPrinter();
    }
}

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);
    }//  w w w .ja  va2  s .  c  o m
    // Allow writing of floating point NaN values not as strings
    this.generator.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false);
}