项目作者: skjolber

项目描述 :
Syntax highlighting (via ANSI) for JSON output using Jackson
高级语言: Java
项目地址: git://github.com/skjolber/jackson-syntax-highlight.git
创建时间: 2018-01-27T23:31:22Z
项目社区:https://github.com/skjolber/jackson-syntax-highlight

开源协议:Apache License 2.0

下载


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
xml <jackson-syntax-highlight.version>1.0.8</jackson-syntax-highlight.version>

then add

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

or


Gradle coordinates

For

groovy ext { jacksonSyntaxHighlightVersion = '1.0.8' }

add

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

Usage

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

  1. // construct output generator
  2. JsonGenerator delegate = new JsonFactory().createGenerator(writer);
  3. // wrap with syntax highlighter
  4. JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate);
  5. // write JSON output
  6. jsonGenerator.writeStartObject(); // start root object
  7. jsonGenerator.writeFieldName("name");
  8. jsonGenerator.writeNumber(123);
  9. jsonGenerator.writeEndObject();
  10. // .. etc

Supply an instance of SyntaxHighlighter using the builder:

  1. SyntaxHighlighter highlighter = DefaultSyntaxHighlighter
  2. .newBuilder()
  3. .withNumber(AnsiSyntaxHighlight.BLUE)
  4. .build();
  5. 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.

  1. JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate, highlighter, prettyprint);
  2. 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