Namespaces

  • 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

  • Arrays
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Tokenizer
  • Validators

Exceptions

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