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
  • PHP

Classes

  • NArrays
  • NFinder
  • NHtml
  • NJson
  • NLimitedScope
  • NMimeTypeDetector
  • NNeon
  • NNeonEntity
  • NPaginator
  • NStrings
  • NTokenizer
  • NValidators

Exceptions

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