PHP code example of zerifas / json

1. Go to this page and download the library: Download zerifas/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/ */

    

zerifas / json example snippets




erifas\JSON;

$schema = new JSON\Obj([
    'id' => new JSON\Number(),
    'enabled' => new JSON\OptionalBoolean(false),
    'array' => new JSON\Arr(),
    'stringArray' => new JSON\Arr(new JSON\Str()),
    'optionalArray' => new JSON\OptionalArr(),
    'optionalStringArray' => new JSON\OptionalArr(new JSON\Str()),
    'optionalObj' => new JSON\OptionalObj(
        [
            'name' => new JSON\Str(),
        ],
        [
            'name' => 'Alice',
        ]
    ),
]);
$v = new JSON\Validator($schema);

$json = '{"id":1,"array":[],"stringArray":["Hello","World"]}';
if ($v->isValid($json)) {
    $doc = $v->getDocument();
    echo implode(', ', $doc->stringArray), PHP_EOL; // Hello, World
    echo $doc->optionalObj->name, PHP_EOL; // Alice
}

// This is not valid for 2 reasons: `id` is missing, and `array` is a number.
$json = '{"array":15,"stringArray":[]}';
if (!$v->isValid($json)) {
    // Errors will be an array:
    // [
    //     'Key path \'id\' is