Nette\Utils\Json::decode PHP Method

decode() public static method

Decodes a JSON string.
public static decode ( $json, $options ) : mixed
return mixed
    public static function decode($json, $options = 0)
    {
        $forceArray = (bool) ($options & self::FORCE_ARRAY);
        $flags = JSON_BIGINT_AS_STRING;
        if (PHP_VERSION_ID < 70000) {
            $json = (string) $json;
            if ($json === '') {
                throw new JsonException('Syntax error');
            } elseif (!$forceArray && preg_match('#(?<=[^\\\\]")\\\\u0000(?:[^"\\\\]|\\\\.)*+"\\s*+:#', $json)) {
                throw new JsonException('The decoded property name is invalid');
                // fatal error when object key starts with \u0000
            } elseif (defined('JSON_C_VERSION') && !preg_match('##u', $json)) {
                throw new JsonException('Invalid UTF-8 sequence', 5);
            } elseif (defined('JSON_C_VERSION') && PHP_INT_SIZE === 8) {
                $flags &= ~JSON_BIGINT_AS_STRING;
                // not implemented in PECL JSON-C 1.3.2 for 64bit systems
            }
        }
        $value = json_decode($json, $forceArray, 512, $flags);
        if ($error = json_last_error()) {
            throw new JsonException(json_last_error_msg(), $error);
        }
        return $value;
    }

Usage Example

Example #1
0
 /**
  * @secured
  * @param $usersID
  */
 public function handleDelete($usersID)
 {
     if (is_string($usersID)) {
         try {
             $usersID = (array) Json::decode($usersID);
             $usersID = array_values($usersID);
         } catch (JsonException $e) {
             $this['notification']->addError($e->getMessage());
             if ($this->isAjax()) {
                 $this['notification']->redrawControl('error');
             }
         }
     }
     $result = $this->userRepository->deactivate($usersID);
     if ($result === TRUE) {
         $this['notification']->addSuccess("Úspěšně deaktivováno...");
     } else {
         if (strpos("Integrity constraint violation", $result) != -1) {
             $this['notification']->addError("Uživatele se nepovedlo deaktivovat.");
         } else {
             $this['notification']->addError($result);
         }
     }
     if ($this->isAjax()) {
         $this['notification']->redrawControl('error');
         $this['notification']->redrawControl('success');
         $this['grid']->redrawControl();
     }
 }
All Usage Examples Of Nette\Utils\Json::decode