Skip to content

jacksonJson: toggle JsonParser.Feature, e.g. ALLOW_COMMENTS for JSON with comments - #3224

Open
Inf1mum wants to merge 1 commit into
diffplug:mainfrom
Inf1mum:json-parser-features-2271
Open

Inf1mum wants to merge 1 commit into
diffplug:mainfrom
Inf1mum:json-parser-features-2271

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Dec 4, 2026

Copy link
Copy Markdown

The jacksonJson step could toggle SerializationFeature (feature) and JsonGenerator.Feature (jsonFeature), but not JsonParser.Feature. A JSON file containing comments therefore always failed with Unable to format, and there was no way to accept them. Fixes #2271.

What changed

  • JacksonJsonConfig: new jsonParserFeatureToToggle map (getter / setter / append), mirroring the existing jsonFeatureToToggle.
  • JacksonJsonFormatterFunc: applies the parser features to the JsonFactory right after the generator features.
  • Gradle: new jsonParserFeature(String feature, boolean toggle) on the jacksonJson config.
  • Maven: new <jsonParserFeatures> map on <jackson>.
  • Docs and changelogs (both plugins) updated.

Behavior note

Jackson parses the comments and then drops them, so formatting no longer fails but the comments themselves are not preserved. The new test documents exactly that (per the discussion in #2272), so the behavior is explicit rather than surprising.

What must NOT change

  • The existing feature / jsonFeature / spaceBeforeSeparator options and their defaults are untouched.
  • With no jsonParserFeature configured, output is byte-for-byte identical to before (the new map is empty by default); all existing JacksonJsonStepTest, JsonSimpleStepTest and GsonStepTest cases pass unchanged.

Verification

  • Before: formatting commented JSON throws IllegalArgumentException: Unable to format (reproduced on main).
  • After: canSetJsonParserFeature passes; the whole com.diffplug.spotless.json package passes (20 tests, 0 failures).
  • spotlessApply run, and :plugin-gradle:compileJava / :plugin-maven:compileJava are clean.

This revives the intent of #2272 with the test, changelog and docs entries requested there.

…with comments

The jacksonJson step could only toggle SerializationFeature and
JsonGenerator.Feature, so a JSON file with comments always failed with
"Unable to format" and there was no way to accept them (issues/2271).

Adds jsonParserFeatureToToggle to JacksonJsonConfig, applied to the
JsonFactory like the existing generator features; exposed as
jsonParserFeature(feature, toggle) in the Gradle extension and as
<jsonParserFeatures> in the Maven plugin. Note that Jackson parses
comments and then drops them, so formatting no longer fails but the
comments themselves are not preserved - the new test documents that.

Fixes diffplug#2271

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support configuring jackson JsonParser. Feature ALLOW_COMMENTS

1 participant