PHP code example of bernardosecades / php-json

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

    

bernardosecades / php-json example snippets


use BernardoSecades\Json\Json;

...

Json::decode($value);
Json::encode($value);


use BernardoSecades\Json\Json;
use BernardoSecades\Json\Option;
use BernardoSecades\Json\ArrayOption;

...

$options = new ArrayOption();
$options[] = Option::JSON_UNESCAPED_UNICODE(); // Use enum object
$options[] = Option::JSON_UNESCAPED_SLASHES();
$options[] = Option::JSON_NUMERIC_CHECK();

Json::encode($value, $options);


use BernardoSecades\Json\Json;
use BernardoSecades\Json\DecodeException;
use BernardoSecades\Json\EncodeException;

...

try {
    Json::decode($value);
} catch (DecodeException $exception) {
    // do something
}

// or

if (!Json::isValid($value)) {
   // do something
}

try {
    Json::encode($value);
} catch (EncodeException $exception) {
 // do something
}


use BernardoSecades\Json\Json;
use BernardoSecades\Json\DecodeException;

...

try {
    Json::decode($value);
} catch (DecodeException $exception) {

    $errorMessage = sprintf('%s , json error code %d', 
        $exception->getMessage(), // see http://php.net/manual/en/function.json-last-error-msg.php
        $exception->getCode(),    // see http://php.net/manual/en/json.constants.php
    ); 

    $this->logger->error($errorMessage);
    // do something more
}