Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/branch-verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Build feature
on:
pull_request:
types:
- synchronize
- opened

jobs:
validate-maven-build:
uses: entur/abt-gha-public/.github/workflows/maven-open-source-verify.yml@main
with:
java-version: 21
21 changes: 21 additions & 0 deletions .github/workflows/deploy-manual.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Deploy to Maven Central
on:
workflow_dispatch:
inputs:
version-increment:
description: 'Version-increment'
required: true
type: choice
options:
- patch
- minor
- major

jobs:
deploy-maven:
uses: entur/gha-maven-central/.github/workflows/maven-publish.yml@main
secrets: inherit
with:
next_version: ${{ inputs.version-increment }}
version_strategy: tag
version_tag_prefix: v
10 changes: 10 additions & 0 deletions .github/workflows/master-verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Build
on:
push:
branches:
- master
jobs:
validate-maven-build:
uses: entur/abt-gha-public/.github/workflows/maven-open-source-verify.yml@main
with:
java-version: 21
16 changes: 0 additions & 16 deletions .github/workflows/maven.yml

This file was deleted.

69 changes: 35 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![Build Status](https://github.com/skjolber/jackson-syntax-highlight/actions/workflows/maven.yml/badge.svg)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.skjolber.jackson/jackson-syntax-highlight.svg)](https://mvnrepository.com/artifact/com.github.skjolber.jackson/jackson-syntax-highlight)
![Build Status](https://github.com/entur/jackson-syntax-highlight/actions/workflows/maven.yml/badge.svg)
[![Maven Central](https://img.shields.io/maven-central/v/org.entur.jackson/jackson-syntax-highlight.svg)](https://mvnrepository.com/artifact/org.entur.jackson/jackson-syntax-highlight)

# jackson-syntax-highlight
Simple utility for generating syntax-highlighted [JSON] text using the [Jackson](https://github.com/FasterXML/jackson) library. Inlines [ANSI] color-codes visible in ANSI-enabled consoles.
Expand All @@ -20,6 +20,8 @@ Features:

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

For Jackson `2.x` support, see the `jackson2.x` branch. Note that the `jackson2.x` branch is on another artifact (and Java package), so both versions can be used in parallel.

## License
[Apache 2.0]

Expand All @@ -31,15 +33,15 @@ The project is built with [Maven] and is available on the central Maven reposito

Add the property
```xml
<jackson-syntax-highlight.version>1.0.8</jackson-syntax-highlight.version>
<jackson-syntax-highlight.version>2.0.x</jackson-syntax-highlight.version>
```

then add

```xml
<dependency>
<groupId>com.github.skjolber.jackson</groupId>
<artifactId>jackson-syntax-highlight</artifactId>
<groupId>org.entur.jackson</groupId>
<artifactId>jackson3-syntax-highlight</artifactId>
<version>${jackson-syntax-highlight.version}</version>
</dependency>
```
Expand All @@ -54,53 +56,57 @@ For

```groovy
ext {
jacksonSyntaxHighlightVersion = '1.0.8'
jacksonSyntaxHighlightVersion = '2.0.x'
}
```

add

```groovy
api ("com.github.skjolber.jackson:jackson-syntax-highlight:${jacksonSyntaxHighlightVersion}")
api ("org.entur.jackson:jackson3-syntax-highlight:${jacksonSyntaxHighlightVersion}")
```
</details>

# Usage
The highlighter wraps a normal [JsonGenerator]. Pretty-printing is enabled by default.
The highlighter requires two changes to a regular setup.

```java
SyntaxHighlighter syntaxHighlighter = DefaultSyntaxHighlighter.newBuilder()
.withNumber(AnsiSyntaxHighlight.RED)
.withString(AnsiSyntaxHighlight.BLUE)
.build();

// construct output generator
JsonGenerator delegate = new JsonFactory().createGenerator(writer);
JsonMapper mapper = JsonMapper.builderWithJackson2Defaults()
.defaultPrettyPrinter(new SyntaxHighlightingPrettyPrinter(syntaxHighlighter))
.build();
ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter();

// wrap with syntax highlighter
JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate);
// create stream and create generator
gWriter writer = new StringWriter();
JsonGenerator delegate = objectWriter.createGenerator(writer);
SyntaxHighlightingJsonGenerator jsonGenerator = SyntaxHighlightingJsonGenerator.create(delegate);

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

// your code here

jsonGenerator.writeEndObject();
// .. etc
```
Supply an instance of `SyntaxHighlighter` using the builder:
jsonGenerator.close();

```java
SyntaxHighlighter highlighter = DefaultSyntaxHighlighter
.newBuilder()
.withNumber(AnsiSyntaxHighlight.BLUE)
.build();

JsonGenerator jsonGenerator = new SyntaxHighlightingJsonGenerator(delegate, highlighter);
// print writer contents.
```

In addition, the JSON structure can be tracked via [JsonStreamContextListener](src/main/java/com/github/skjolber/jackson/jsh/JsonStreamContextListener.java), for stateful coloring of subtrees.
In addition, the JSON structure can be tracked via [TokenStreamContextListener](src/main/java/org/entur/jackson3/jsh/TokenStreamContextListener.java), for stateful coloring of subtrees.

## Highlighting an object
Write a full object using `writeObject`, i.e.

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

## See also
Expand All @@ -109,20 +115,15 @@ jsonGenerator.writeObject(obj);

# 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
- 2.x: Support for Jackson 2.
- 1.1.0: Forked from [jackson-syntax-highlight](https://github.com/skjolber/jackson-syntax-highlight) due to too few maintainers. Support for Jackson 2.x.

[Apache 2.0]: http://www.apache.org/licenses/LICENSE-2.0.html
[issue-tracker]: https://github.com/skjolber/jackson-syntax-highlight/issues
[issue-tracker]: https://github.com/entur/jackson-syntax-highlight/issues
[Maven]: http://maven.apache.org/
[SyntaxHighlighter]: src/main/java/com/github/skjolber/jackson/jsh/SyntaxHighlighter.java
[SyntaxHighlighter]: src/main/java/org/entur/jackson3/jsh/SyntaxHighlighter.java
[Jackson]: https://github.com/FasterXML/jackson
[ANSI]: https://en.wikipedia.org/wiki/ANSI_escape_code
[JSON]: https://no.wikipedia.org/wiki/JSON
[JsonGenerator]: https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
[logback-logstash-syntax-highlighting-decorators]: https://github.com/skjolber/logback-logstash-syntax-highlighting-decorators
[logback-logstash-syntax-highlighting-decorators]: https://github.com/entur/logback-logstash-syntax-highlighting-decorators
Loading