Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. JsonFile.php

function JsonFile::encode

Encodes an array into (optionally pretty-printed) JSON

Parameters

mixed $data Data to encode into a formatted JSON string:

int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE):

string $indent Indentation string:

Return value

string Encoded json

23 calls to JsonFile::encode()
Auditor::audit in vendor/composer/composer/src/Composer/Advisory/Auditor.php
CheckPlatformReqsCommand::printTable in vendor/composer/composer/src/Composer/Command/CheckPlatformReqsCommand.php
ComposerRepository::asyncFetchFile in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php
@phpstan-return PromiseInterface<array<mixed>|true> true if the response was a 304 and the cache is fresh, otherwise it returns the decoded json
ComposerRepository::fetchFile in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php
ComposerRepository::fetchFileIfLastModified in vendor/composer/composer/src/Composer/Repository/ComposerRepository.php

... See full list

File

vendor/composer/composer/src/Composer/Json/JsonFile.php, line 280

Class

JsonFile
Reads/writes json files.

Namespace

Composer\Json

Code

public static function encode($data, int $options = 448, string $indent = self::INDENT_DEFAULT) : string {
    $json = json_encode($data, $options);
    if (false === $json) {
        self::throwEncodeError(json_last_error());
    }
    if (($options & JSON_PRETTY_PRINT) > 0 && $indent !== self::INDENT_DEFAULT) {
        // Pretty printing and not using default indentation
        return Preg::replaceCallback('#^ {4,}#m', static function ($match) use ($indent) : string {
            return str_repeat($indent, (int) (strlen($match[0]) / 4));
        }, $json);
    }
    return $json;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal