PHP code example of josantonius / json

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

    

josantonius / json example snippets


/**
 * @param string $filepath The path to the JSON file to be handled.
 */
public function __construct(public readonly string $filepath)
{
}

public readonly string $filepath;

/**
 * @return bool True if the file exists at the specified filepath, false otherwise.
 */
public function exists(): bool;

/**
 * @param bool $asObject If true and the value is an array, it is returned as an object.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 *
 * @return mixed the contents of the JSON file.
 */
public function get(bool $asObject = false): mixed;

/**
 * @param mixed  $content The data that will be written to the file or a key within the file.
 * @param string $dot     The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws CreateFileException
 * @throws CreateDirectoryException
 * @throws NoIterableElementException
 *
 * @return mixed the content of the JSON file after the set operation.
 */
public function set(mixed $content = [], string $dot = null): array|bool|int|null|string;

/**
 * @param mixed  $content The data that will be written to the file or a key within the file.
 * @param string $dot     The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 * @throws NoIterableElementException
 *
 * @return mixed the content of the JSON file after the merge operation.
 */
public function merge(array|object $content, string $dot = null): array;

/**
 * @param string $dot The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 * @throws NoIterableElementException
 *
 * @return mixed|null the last value of JSON file, or null if array is empty.
 */
public function pop(string $dot = null): mixed;

/**
 * @param mixed  $content The data that will be written to the file or a key within the file.
 * @param string $dot     The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 * @throws NoIterableElementException
 *
 * @return mixed the content of the JSON file after the push operation.
 */
public function push(mixed $content, string $dot = null): array;

/**
 * @param string $dot The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 * @throws NoIterableElementException
 *
 * @return mixed|null the shifted value, or null if array is empty.
 */
public function shift(string $dot = null): mixed(mixed $content, string $dot = null): array;

/**
 * @param string $dot       The dot notation representing the key to be modified within the file.
 * @param bool   $reindexed If true, the array will be re-indexed.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 *
 * @return array the content of the JSON file after the unset operation.
 */
public function unset(string $dot, bool $reindexed = false): array;

/**
 * @param mixed  $content The data that will be written to the file or a key within the file.
 * @param string $dot     The dot notation representing the key to be modified within the file.
 *
 * @throws GetFileException
 * @throws JsonErrorException
 * @throws NoIterableFileException
 * @throws NoIterableElementException
 *
 * @return mixed the content of the JSON file after the unshift operation.
 */
public function unshift(mixed $content, string $dot = null): mixed;

use Josantonius\Json\Exceptions\GetFileException;           // if file reading failed
use Josantonius\Json\Exceptions\CreateFileException;        // if file creation failed
use Josantonius\Json\Exceptions\JsonErrorException;         // if the file contains invalid JSON
use Josantonius\Json\Exceptions\NoIterableFileException;    // if the file isn't a JSON array
use Josantonius\Json\Exceptions\CreateDirectoryException;   // if directory creation failed
use Josantonius\Json\Exceptions\NoIterableElementException; // if $dot isn't an array location

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->filepath; // 'file.json'

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->exists(); // bool

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->get(); // ['foo' => 'bar']

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->get(asObject: true); // object(stdClass) { ["foo"] => string(3) "bar" }

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->set();

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->set(['foo' => 'bar']);

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->set('baz', 'foo.bar.0');

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->merge(['bar' => 'foo']);

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->merge(['baz' => 'bar'], 'foo.0');

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->pop(); // 3

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->pop(); // 3

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->push(['name'  => 'bar']);

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->push('baz', 'foo.bar.0');

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->shift(); // 1

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->shift('foo.bar.0'); // 1

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->unset('foo.bar');

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->unset('1');

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->unset('1', reindexed: true);

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->unshift(0);

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->unshift(0, 'foo.bar.0');