com.fasterxml.jackson.core.util.DefaultIndenter Java Examples

The following examples show how to use com.fasterxml.jackson.core.util.DefaultIndenter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSON.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private static ObjectMapper prettyMapper() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL);
	mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

	mapper.registerModule(tuuidModule());

	if (!Env.dev()) {
		mapper.registerModule(new AfterburnerModule());
	}

	DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
	pp = pp.withObjectIndenter(new DefaultIndenter("  ", "\n"));

	mapper.setDefaultPrettyPrinter(pp);

	return mapper;
}
 
Example #2
Source File: MutableProfileTest.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static void prettyPrint(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(json);

    CustomPrettyPrinter prettyPrinter = new CustomPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb))
            .setPrettyPrinter(prettyPrinter);
    jg.writeTree(node);
    jg.close();

    System.out.println(sb.toString().replace("\"", "\\\"").replace(DefaultIndenter.SYS_LF,
            "\"" + DefaultIndenter.SYS_LF + " + \""));
}
 
Example #3
Source File: ServiceResponseMarshaller.java    From keycloak-protocol-cas with Apache License 2.0 6 votes vote down vote up
public static String marshalJson(CASServiceResponse serviceResponse) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //Force newlines to be LF (default is system dependent)
    DefaultPrettyPrinter printer = new DefaultPrettyPrinter()
            .withObjectIndenter(new DefaultIndenter("  ", "\n"));

    //create wrapper node
    Map<String, Object> casModel = new HashMap<>();
    casModel.put("serviceResponse", serviceResponse);
    try {
        return mapper.writer(printer).writeValueAsString(casModel);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: SnapshotMatcher.java    From json-snapshot.github.io with MIT License 6 votes vote down vote up
private static PrettyPrinter buildDefaultPrettyPrinter() {
  DefaultPrettyPrinter pp =
      new DefaultPrettyPrinter("") {
        @Override
        public DefaultPrettyPrinter withSeparators(Separators separators) {
          this._separators = separators;
          this._objectFieldValueSeparatorWithSpaces =
              separators.getObjectFieldValueSeparator() + " ";
          return this;
        }
      };
  Indenter lfOnlyIndenter = new DefaultIndenter("  ", "\n");
  pp.indentArraysWith(lfOnlyIndenter);
  pp.indentObjectsWith(lfOnlyIndenter);
  return pp;
}
 
Example #5
Source File: RDFJSONWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void modelToRdfJsonInternal(final Model graph, final WriterConfig writerConfig,
		final JsonGenerator jg) throws IOException, JsonGenerationException {
	if (writerConfig.get(BasicWriterSettings.PRETTY_PRINT)) {
		// SES-2011: Always use \n for consistency
		Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
		// By default Jackson does not pretty print, so enable this unless
		// PRETTY_PRINT setting is disabled
		DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter)
				.withObjectIndenter(indenter);
		jg.setPrettyPrinter(pp);
	}
	jg.writeStartObject();
	for (final Resource nextSubject : graph.subjects()) {
		jg.writeObjectFieldStart(RDFJSONWriter.resourceToString(nextSubject));
		for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) {
			jg.writeArrayFieldStart(nextPredicate.stringValue());
			for (final Value nextObject : graph.filter(nextSubject, nextPredicate, null).objects()) {
				// contexts are optional, so this may return empty in some
				// scenarios depending on the interpretation of the way contexts
				// work
				final Set<Resource> contexts = graph.filter(nextSubject, nextPredicate, nextObject).contexts();

				RDFJSONWriter.writeObject(nextObject, contexts, jg);
			}
			jg.writeEndArray();
		}
		jg.writeEndObject();
	}
	jg.writeEndObject();
}
 
Example #6
Source File: JsonContentHandlerJacksonWrapper.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void withNl() {
    if (isFormattedOutput) {
      // upgrade from Lf2SpacesIndenter - removed in v 2.7
      
      uimaPrettyPrinter.indentObjectsWith(
//          DefaultPrettyPrinter.Lf2SpacesIndenter.instance
            DefaultIndenter.SYSTEM_LINEFEED_INSTANCE
          );
    }
  }
 
Example #7
Source File: AbstractQueryCommand.java    From buck with Apache License 2.0 5 votes vote down vote up
private static <T extends SortedMap<String, Object>> void printAttributesAsJson(
    ImmutableSortedMap<String, T> result, PrintStream printStream) throws IOException {
  ObjectMappers.WRITER
      .with(
          new DefaultPrettyPrinter().withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE))
      // Jackson closes stream by default - we do not want it
      .without(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
      .writeValue(printStream, result);

  // Jackson does not append a newline after final closing bracket. Do it to make JSON look
  // nice on console.
  printStream.println();
}
 
Example #8
Source File: StandardJsonPrettyPrinter.java    From typescript-generator with MIT License 5 votes vote down vote up
public StandardJsonPrettyPrinter(String indent, String eol) {
    this.indent = indent;
    this.eol = eol;
    final DefaultIndenter indenter = new DefaultIndenter(indent, eol);
    this._arrayIndenter = indenter;
    this._objectIndenter = indenter;
}
 
Example #9
Source File: JsonGraphFormatter.java    From depgraph-maven-plugin with Apache License 2.0 5 votes vote down vote up
private String serialize(JsonGraph jsonGraph) {
  DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter()
      .withObjectIndenter(new DefaultIndenter("  ", "\n"));

  ObjectWriter writer = this.objectMapper.writer(prettyPrinter);
  StringWriter jsonWriter = new StringWriter();
  try {
    writer.writeValue(jsonWriter, jsonGraph);
  } catch (IOException e) {
    // should never happen with StringWriter
    throw new IllegalStateException(e);
  }

  return jsonWriter.toString();
}
 
Example #10
Source File: JSONUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static String toJsonString(ObjectMapper aMapper, boolean aPretty, Object aObject)
    throws IOException
{
    StringWriter out = new StringWriter();

    JsonGenerator jsonGenerator = aMapper.getFactory().createGenerator(out);
    if (aPretty) {
        jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter()
                .withObjectIndenter(new DefaultIndenter().withLinefeed("\n")));
    }

    jsonGenerator.writeObject(aObject);
    return out.toString();
}
 
Example #11
Source File: JsonFormatter.java    From formatter-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
    super.initCfg(cfg);

    int indent = Integer.parseInt(options.getOrDefault("indent", "4"));
    String lineEnding = options.getOrDefault("lineending", System.lineSeparator());
    boolean spaceBeforeSeparator = Boolean.parseBoolean(options.getOrDefault("spaceBeforeSeparator", "true"));

    formatter = new ObjectMapper();

    // Setup a pretty printer with an indenter (indenter has 4 spaces in this case)
    DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(Strings.repeat(" ", indent), lineEnding);
    DefaultPrettyPrinter printer = new DefaultPrettyPrinter() {
        private static final long serialVersionUID = 1L;

        @Override
        public DefaultPrettyPrinter createInstance() {
            return new DefaultPrettyPrinter(this);
        }

        @Override
        public DefaultPrettyPrinter withSeparators(Separators separators) {
            this._separators = separators;
            this._objectFieldValueSeparatorWithSpaces = (spaceBeforeSeparator ? " " : "")
                    + separators.getObjectFieldValueSeparator() + " ";
            return this;
        }
    };

    printer.indentObjectsWith(indenter);
    printer.indentArraysWith(indenter);
    formatter.setDefaultPrettyPrinter(printer);
    formatter.enable(SerializationFeature.INDENT_OUTPUT);
}
 
Example #12
Source File: JacksonObjectMapper.java    From bonita-ui-designer with GNU General Public License v2.0 5 votes vote down vote up
public byte[] toPrettyJson(Object object, Class<?> serializationView) throws IOException {
    // Use UTF8 to accept any character and have platform-independent files.
    return objectMapper.writerWithView(serializationView)
            .with(new DefaultPrettyPrinter()
                .withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE))
            .writeValueAsString(object)
            .getBytes(StandardCharsets.UTF_8);
}
 
Example #13
Source File: AbstractSPARQLJSONWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void startDocument() throws QueryResultHandlerException {
	if (!documentOpen) {
		documentOpen = true;
		headerOpen = false;
		headerComplete = false;
		tupleVariablesFound = false;
		firstTupleWritten = false;
		linksFound = false;

		if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
			// SES-2011: Always use \n for consistency
			Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
			// By default Jackson does not pretty print, so enable this unless
			// PRETTY_PRINT setting is disabled
			DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter)
					.withObjectIndenter(indenter);
			jg.setPrettyPrinter(pp);
		}

		try {
			if (getWriterConfig().isSet(BasicQueryWriterSettings.JSONP_CALLBACK)) {
				// SES-1019 : Write the callbackfunction name as a wrapper for
				// the results here
				String callbackName = getWriterConfig().get(BasicQueryWriterSettings.JSONP_CALLBACK);
				jg.writeRaw(callbackName);
				jg.writeRaw("(");
			}
			jg.writeStartObject();
		} catch (IOException e) {
			throw new QueryResultHandlerException(e);
		}
	}
}
 
Example #14
Source File: NpmJsonPrettyPrinter.java    From updatebot with Apache License 2.0 5 votes vote down vote up
public NpmJsonPrettyPrinter() {
    this._objectFieldValueSeparatorWithSpaces = ": ";

    Indenter indenter = new DefaultIndenter("  ", System.lineSeparator());
    this.indentObjectsWith(indenter);
    this.indentArraysWith(indenter);
}
 
Example #15
Source File: AbstractJackson2HttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void init(ObjectMapper objectMapper) {
	this.objectMapper = objectMapper;
	setDefaultCharset(DEFAULT_CHARSET);
	DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
	prettyPrinter.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	this.ssePrettyPrinter = prettyPrinter;
}
 
Example #16
Source File: AbstractJackson2HttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected AbstractJackson2HttpMessageConverter(ObjectMapper objectMapper) {
	this.objectMapper = objectMapper;
	setDefaultCharset(DEFAULT_CHARSET);
	DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
	prettyPrinter.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	this.ssePrettyPrinter = prettyPrinter;
}
 
Example #17
Source File: AbstractJackson2HttpMessageConverter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected AbstractJackson2HttpMessageConverter(ObjectMapper objectMapper) {
	this.objectMapper = objectMapper;
	DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
	prettyPrinter.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	this.ssePrettyPrinter = prettyPrinter;
}
 
Example #18
Source File: JsonFormatter.java    From alexa-utterance-generator with Apache License 2.0 4 votes vote down vote up
PrettyPrinter() {
    _arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
}
 
Example #19
Source File: JSONUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private PrettyPrintMappingJsonFactory() {
  this.pp = new DefaultPrettyPrinter();
  pp.indentArraysWith(new DefaultIndenter());
}
 
Example #20
Source File: MessageMarshaller.java    From curiostack with MIT License 4 votes vote down vote up
private MessagePrettyPrinter() {
  _objectIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE.withLinefeed("\n");
}
 
Example #21
Source File: AbstractReadWriteJackson2HttpMessageConverter.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void initSsePrettyPrinter() {
	setDefaultCharset(DEFAULT_CHARSET);
	DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
	prettyPrinter.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	this.ssePrettyPrinter = prettyPrinter;
}
 
Example #22
Source File: BatfishObjectMapper.java    From batfish with Apache License 2.0 4 votes vote down vote up
public PrettyPrinter() {
  _arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
}
 
Example #23
Source File: ObjectMappers.java    From glowroot with Apache License 2.0 4 votes vote down vote up
public static PrettyPrinter getPrettyPrinter() {
    CustomPrettyPrinter prettyPrinter = new CustomPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
    return prettyPrinter;
}
 
Example #24
Source File: Jackson2JsonEncoder.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static PrettyPrinter initSsePrettyPrinter() {
	DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
	printer.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	return printer;
}
 
Example #25
Source File: JsonUtils.java    From render with GNU General Public License v2.0 4 votes vote down vote up
public ArraysOnNewLinePrettyPrinter() {
    _arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
}
 
Example #26
Source File: JSONIO.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
public MyPrettyPrinter( Charset charset ) throws ManipulationException
{
    super( DEFAULT_ROOT_VALUE_SEPARATOR );
    this.charset = charset;
    _objectIndenter = new DefaultIndenter("  ", getEOL() );
}
 
Example #27
Source File: EvaluateEventJsonGeneratorTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public TestPrettyPrinter() {
    _arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
    _objectFieldValueSeparatorWithSpaces = ": ";
}
 
Example #28
Source File: Jackson2JsonEncoder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static PrettyPrinter initSsePrettyPrinter() {
	DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
	printer.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
	return printer;
}