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

Classes

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

Exceptions

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