PHP code example of aternos / almost-json

1. Go to this page and download the library: Download aternos/almost-json library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aternos / almost-json example snippets


$parser = new \Aternos\AlmostJson\AlmostJsonParser();
$parsed = $parser->parseString('{key: "value"}', assoc: true);

// => ['key' => 'value']

$parser = new \Aternos\AlmostJson\AlmostJsonParser();

$input = new \Aternos\AlmostJson\Input('{key: "value"}', encoding: "ISO-8859-1");
$parsed = $parser->parse($input, assoc: true);


$parser = new \Aternos\AlmostJson\AlmostJsonParser();
$parser->setMaxDepth(512) // Maximum depth of the JSON tree
    ->setZeroPrefixOctal(true) // Parse numbers with leading zero as octal
    ->setTopLevelUnquotedStringAllowed(true); // Allow the root of the JSON tree to be an unquoted string



try {
    $parsed = json_decode($string, true, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
    $parser = new \Aternos\AlmostJson\AlmostJsonParser();
    $parsed = $parser->parseString($string, assoc: true);
}