Problem/Motivation

The daily bug bingo in #bugsmash brought up #2983304: Apply code style to all component composer.json which lead to a search for Drupal coding standards for json file. I could not find any. There are coding standards for javaScript but it does not explicitly say if they apply to json as well.

Should there be standards for json? Whether that is agreed to or not, it would be helpful to have the documentation explain the current situation.

Steps to reproduce

Core commit code checks does not check json files.

Proposed resolution

Update coding standards documentation to explicitly state there are no Drupal coding standards for json files.

Remaining tasks

Make sure that there really are no coding standards for json.

User interface changes

API changes

Data model changes

Comments

quietone created an issue. See original summary.

froboy’s picture

#2654894: Use an indent of 4 spaces for composer.json changed core's composer.json to 4 space indents instead of two to follow composer's.

I'd suggest that, for consistency, we encourage contrib modules to follow core's lead on this.

drunken monkey’s picture

I’d also be in favor of specifying standards. Does anyone know of some existing JSON style guide from which we could copy? (I couldn’t find anything. But I also can’t think of much we’d want to specify, tbh, except how to indent.)

If Core specifically changed their composer.json to indent with four spaces, then that should be specified as a standard somewhere, because the reasoning is probably similar for contrib code as well (even though I guess it’s much rarer that someone wants to add a dependency on a module/theme, and not just to their site). Also, it seems this only applies to composer.json specifically, so it might be prudent to also note in the standards that other JSON files should use two spaces, like the rest of Drupal code.

mile23’s picture

Issue tags: +Composer

Most use-cases for JSON within Drupal are related to Composer, so tagging.

This is what Composer does:

    /**
     * Encodes an array into (optionally pretty-printed) JSON
     *
     * @param  mixed  $data    Data to encode into a formatted JSON string
     * @param  int    $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
     * @return string Encoded json
     */
    public static function encode($data, int $options = 448)

So by default we're using 4 spaces for indent, and whatever else JSON_PRETTY_PRINT gives us.

The encode() method itself just goes to json_encode(), which, again, is the path of least resistance for generating human-readable (pretty) JSON.

We're also using a number of scripted tools to generate JSON for use by Composer, which all use json_encode() in this path-of-least-resistance way.

Given this, if we're adopting a coding standard for JSON, then this should be our standard. That way automatic tools are always in compliance without having to deal with a lot of edge cases.