Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Exceptions

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Utils
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * JSON encoder and decoder.
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Utils
 16:  */
 17: class NJson
 18: {
 19:     const FORCE_ARRAY = 1;
 20: 
 21:     /** @var array */
 22:     private static $messages = array(
 23:         JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
 24:         JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
 25:         JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
 26:         JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
 27:         5 /*JSON_ERROR_UTF8*/ => 'Invalid UTF-8 sequence',
 28:         6 /*PHP_JSON_ERROR_RECURSION*/ => 'Recursion detected',
 29:         7 /*PHP_JSON_ERROR_INF_OR_NAN*/ => 'Inf and NaN cannot be JSON encoded',
 30:         8 /*PHP_JSON_ERROR_UNSUPPORTED_TYPE*/ => 'Type is not supported',
 31:     );
 32: 
 33: 
 34:     /**
 35:      * Static class - cannot be instantiated.
 36:      */
 37:     final public function __construct()
 38:     {
 39:         throw new NStaticClassException;
 40:     }
 41: 
 42: 
 43:     /**
 44:      * Returns the JSON representation of a value.
 45:      * @param  mixed
 46:      * @return string
 47:      */
 48:     public static function encode($value)
 49:     {
 50:         if (function_exists('ini_set')) { // workaround for PHP bugs #52397, #54109, #63004
 51:             $old = ini_set('display_errors', 0); // needed to receive 'Invalid UTF-8 sequence' error
 52:         }
 53:         set_error_handler(create_function('$severity, $message', ' // needed to receive \'recursion detected\' error
 54:             restore_error_handler();
 55:             throw new NJsonException($message);
 56:         '));
 57:         $json = json_encode($value);
 58:         restore_error_handler();
 59:         if (isset($old)) {
 60:             ini_set('display_errors', $old);
 61:         }
 62:         if (PHP_VERSION_ID >= 50300 && ($error = json_last_error())) {
 63:             throw new NJsonException(isset(self::$messages[$error]) ? self::$messages[$error] : 'Unknown error', $error);
 64:         }
 65:         $json = str_replace(array("\xe2\x80\xa8", "\xe2\x80\xa9"), array('\u2028', '\u2029'), $json);
 66:         return $json;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Decodes a JSON string.
 72:      * @param  string
 73:      * @param  int
 74:      * @return mixed
 75:      */
 76:     public static function decode($json, $options = 0)
 77:     {
 78:         $json = (string) $json;
 79:         if (!preg_match('##u', $json)) {
 80:             throw new NJsonException(self::$messages[5], 5); // workaround for PHP < 5.3.3 & PECL JSON-C
 81:         }
 82: 
 83:         $forceArray = (bool) ($options & self::FORCE_ARRAY);
 84:         if (!$forceArray && preg_match('#(?<=[^\\\\]")\\\\u0000(?:[^"\\\\]|\\\\.)*+"\s*+:#', $json)) { // workaround for json_decode fatal error when object key starts with \u0000
 85:             throw new NJsonException(self::$messages[JSON_ERROR_CTRL_CHAR]);
 86:         }
 87: 
 88:         $value = json_decode($json, $forceArray);
 89:         if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' is not clearing json_last_error
 90:             $error = PHP_VERSION_ID >= 50300 ? json_last_error() : 0;
 91:             throw new NJsonException(isset(self::$messages[$error]) ? self::$messages[$error] : 'Unknown error', $error);
 92:         }
 93:         return $value;
 94:     }
 95: 
 96: }
 97: 
 98: 
 99: /**
100:  * The exception that indicates error of JSON encoding/decoding.
101:  * @package Nette\Utils
102:  */
103: class NJsonException extends Exception
104: {
105: }
106: 
Nette Framework 2.0.18 (for PHP 5.2, prefixed) API documentation generated by ApiGen 2.8.0