Skip to content

analyse()

Diagnostic pass before repair — what failed, where, and which flags to apply.

Signature

php
JsonFast::analyse(string $json, ?int $output = OUTPUT_ARRAY): mixed

Parameters

NameTypeDefaultDescription
$jsonstringJSON text to analyse
$output?intOUTPUT_ARRAYOutput mode

Returns

FieldTypeDescription
validboolWhether the input is already valid JSON
repairableboolWhether repair() can fix it
errorarrayPresent when invalid: message, line, column
repairsarraySuggested repair flags (see below)

Each entry in repairs:

FieldDescription
flagConstant name (e.g. REPAIR_COMMENTS)
bitNumeric bitmask value
descriptionHuman-readable explanation

Example return value

php
[
    'valid' => false,
    'repairable' => true,
    'error' => [
        'message' => '...',
        'line' => 3,
        'column' => 18,
    ],
    'repairs' => [
        [
            'flag' => 'REPAIR_COMMENTS',
            'bit' => 4,
            'description' => 'Removes // and /* */ comments',
        ],
        [
            'flag' => 'REPAIR_UNQUOTED_STRINGS',
            'bit' => 32,
            'description' => 'Quotes unquoted string values',
        ],
    ],
]

Build a targeted repair mask

php
$report = JsonFast::analyse($broken);

if ($report['valid']) {
    $data = JsonFast::minify($broken);
} elseif (!$report['repairable']) {
    throw new InvalidArgumentException($report['error']['message']);
} else {
    $flags = array_reduce(
        $report['repairs'],
        fn ($mask, $r) => $mask | $r['bit'],
        0
    );
    $data = JsonFast::repair($broken, $flags);
}

When to use

  • Ingestion pipelines where you want an audit trail of which repairs ran
  • User-facing uploads — show line/column and whether auto-fix is possible
  • Avoiding REPAIR_ALL when only one or two issues are present

Native tools, weird experiments, and practical performance work.