Skip to content

Latest commit

 

History

History
130 lines (100 loc) · 4.17 KB

README.md

File metadata and controls

130 lines (100 loc) · 4.17 KB

Build Status Maven Central

This project is currently maintained at Entur.

jackson-syntax-highlight

Simple utility for generating syntax-highlighted JSON text using the Jackson library. Inlines ANSI color-codes visible in ANSI-enabled consoles.

Features:

  • works with the popular Jackson JSON library.
  • configurable color schemes
    • datatype
      • string
      • number
      • boolean
      • null
    • field name
    • comma
    • brackets
    • colon
    • whitespace

The library is primarily intended for adding coloring while doing minimal changes to existing applications. For example, coloring of status codes during unit testing.

License

Apache 2.0

Obtain

The project is built with Maven and is available on the central Maven repository.

Maven coordinates

Add the property

<jackson-syntax-highlight.version>1.0.8</jackson-syntax-highlight.version>

then add

<dependency>
    <groupId>com.github.skjolber.jackson</groupId>
    <artifactId>jackson-syntax-highlight</artifactId>
    <version>${jackson-syntax-highlight.version}</version>
</dependency>

or

Gradle coordinates

For

ext {
  jacksonSyntaxHighlightVersion = '1.0.8'
}

add

api ("com.github.skjolber.jackson:jackson-syntax-highlight:${jacksonSyntaxHighlightVersion}")

Usage

The highlighter wraps a normal JsonGenerator. Pretty-printing is enabled by default.

// construct output generator
JsonGenerator delegate = new JsonFactory().createGenerator(writer);

// wrap with syntax highlighter
JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate);

// write JSON output
jsonGenerator.writeStartObject(); // start root object
jsonGenerator.writeFieldName("name");
jsonGenerator.writeNumber(123);
jsonGenerator.writeEndObject();
// .. etc

Supply an instance of SyntaxHighlighter using the builder:

SyntaxHighlighter highlighter = DefaultSyntaxHighlighter
                                    .newBuilder()
                                    .withNumber(AnsiSyntaxHighlight.BLUE)
                                    .build();
		
JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate, highlighter);

In addition, the JSON structure can be tracked via JsonStreamContextListener, for stateful coloring of subtrees.

Highlighting an object

Write a full object using writeObject, i.e.

JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate, highlighter, prettyprint);
jsonGenerator.writeObject(obj);

See also

History

  • 1.0.8: Add module info.
  • 1.0.7: Do not set default colors.
  • 1.0.6: Add option for single-line output
  • 1.0.3 to 1.0.5: Bump Jackson dependency due to security issue
  • 1.0.2: More tests, minor fixes.
  • 1.0.1: Various improvements, works better with logback-logstash-syntax-highlighting-decorators for Logback logging.
  • 1.0.0: Initial version