List of usage examples for com.fasterxml.jackson.core.util DefaultPrettyPrinter DefaultPrettyPrinter
public DefaultPrettyPrinter(DefaultPrettyPrinter base)
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); }/*from w ww .j av 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); } }