Add JVM-based JSON formatter by jamietanna · Pull Request #853 · diffplug/spotless

jamietanna

jamietanna

jamietanna

@jamietanna jamietanna changed the title WIP: JSON formatter Add JVM-based JSON formatter

Apr 25, 2021

nedtwigg

nedtwigg

nedtwigg

nedtwigg

@jamietanna

Currenly, if consumers of Spotless want to format their JSON files, they
need to do this by adding `prettier`, which increases the dependencies a
project needs.

To simplify things for consumers, as documented in diffplug#850, we can provide
a JVM-based JSON formatter, using `org.json`'s JSON formatting.

To follow how we've done this with other formatters, we can retrieve
`org.json`'s JAR at runtime, and retrieve the classes via Reflection,
instead of adding this as a `lib-extra` project, with an explicit
dependency.

To validate this fully, we want a few straightforward JSON files to
validate before/after, but we can also use [a sample Cucumber JSON
report] as a much more complex example of what happens, which we've
removed any `data` objects from its source, so the files are smaller.

We also want our consumers to be able to configure the indentation size.

We're calling this a `simple` formatter as it doesn't allow much to be
configured other than the indentation size in spaces.

[a sample Cucumber JSON report]: https://github.com/damianszczepanik/cucumber-reporting/raw/master/src/test/resources/json/sample.json

nedtwigg

nedtwigg

As we've created a JVM JSON formatter as part of diffplug#850, we should make it
possible to use it natively in Gradle, which requires we add it as a new
supported type in `SpotlessExtension`.

When configured, we'll default to including any JSON files under the
`src` directory, while allowing it to be overriden if requested.

@jamietanna