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

Breadcrumb

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

function JsonFile::validateSyntax

Validates the syntax of a JSON string

Parameters

string $file:

Return value

bool true on success

Throws

\UnexpectedValueException

ParsingException

2 calls to JsonFile::validateSyntax()
JsonFile::parseJson in vendor/composer/composer/src/Composer/Json/JsonFile.php
Parses json string and returns hash.
JsonFile::validateSchema in vendor/composer/composer/src/Composer/Json/JsonFile.php
Validates the schema of the current json file according to composer-schema.json rules

File

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

Class

JsonFile
Reads/writes json files.

Namespace

Composer\Json

Code

protected static function validateSyntax(string $json, ?string $file = null) : bool {
    $parser = new JsonParser();
    $result = $parser->lint($json);
    if (null === $result) {
        if (defined('JSON_ERROR_UTF8') && JSON_ERROR_UTF8 === json_last_error()) {
            if ($file === null) {
                throw new \UnexpectedValueException('The input is not UTF-8, could not parse as JSON');
            }
            else {
                throw new \UnexpectedValueException('"' . $file . '" is not UTF-8, could not parse as JSON');
            }
        }
        return true;
    }
    if ($file === null) {
        throw new ParsingException('The input does not contain valid JSON' . "\n" . $result->getMessage(), $result->getDetails());
    }
    else {
        throw new ParsingException('"' . $file . '" does not contain valid JSON' . "\n" . $result->getMessage(), $result->getDetails());
    }
}
RSS feed
Powered by Drupal