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
- datatype
The library is primarily intended for adding coloring while doing minimal changes to existing applications. For example, coloring of status codes during unit testing.
The project is built with Maven and is available on the central Maven repository.
Maven coordinates
Add the property
<jackson-syntax-highlight.version>1.1.x</jackson-syntax-highlight.version>then add
<dependency>
<groupId>org.entur.jackson</groupId>
<artifactId>jackson-syntax-highlight</artifactId>
<version>${jackson-syntax-highlight.version}</version>
</dependency>or
Gradle coordinates
For
ext {
jacksonSyntaxHighlightVersion = '1.1.x'
}add
api ("org.entur.jackson:jackson-syntax-highlight:${jacksonSyntaxHighlightVersion}")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();
// .. etcSupply 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.
Write a full object using writeObject, i.e.
JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate, highlighter, prettyprint);
jsonGenerator.writeObject(obj);- 1.1.0: Forked from jackson-syntax-highlight due to too few maintainers.