Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

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

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://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:     const PRETTY = 2;
 22: 
 23:     /** @var array */
 24:     private static $messages = array(
 25:         JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
 26:         JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
 27:         JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
 28:         JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
 29:         5 /*JSON_ERROR_UTF8*/ => 'Invalid UTF-8 sequence',
 30:         6 /*JSON_ERROR_RECURSION*/ => 'Recursion detected',
 31:         7 /*JSON_ERROR_INF_OR_NAN*/ => 'Inf and NaN cannot be JSON encoded',
 32:         8 /*JSON_ERROR_UNSUPPORTED_TYPE*/ => 'Type is not supported',
 33:     );
 34: 
 35: 
 36:     /**
 37:      * Static class - cannot be instantiated.
 38:      */
 39:     final public function __construct()
 40:     {
 41:         throw new Nette\StaticClassException;
 42:     }
 43: 
 44: 
 45:     /**
 46:      * Returns the JSON representation of a value.
 47:      * @param  mixed
 48:      * @param  int  accepts Json::PRETTY
 49:      * @return string
 50:      */
 51:     public static function encode($value, $options = 0)
 52:     {
 53:         if (function_exists('ini_set')) { // workaround for PHP bugs #52397, #54109, #63004
 54:             $old = ini_set('display_errors', 0); // needed to receive 'Invalid UTF-8 sequence' error
 55:         }
 56:         set_error_handler(function($severity, $message) { // needed to receive 'recursion detected' error
 57:             restore_error_handler();
 58:             throw new JsonException($message);
 59:         });
 60:         $json = json_encode(
 61:             $value,
 62:             PHP_VERSION_ID >= 50400 ? (JSON_UNESCAPED_UNICODE | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)) : 0
 63:         );
 64:         restore_error_handler();
 65:         if (isset($old)) {
 66:             ini_set('display_errors', $old);
 67:         }
 68:         if ($error = json_last_error()) {
 69:             throw new JsonException(isset(static::$messages[$error]) ? static::$messages[$error] : 'Unknown error', $error);
 70:         }
 71:         $json = str_replace(array("\xe2\x80\xa8", "\xe2\x80\xa9"), array('\u2028', '\u2029'), $json);
 72:         return $json;
 73:     }
 74: 
 75: 
 76:     /**
 77:      * Decodes a JSON string.
 78:      * @param  string
 79:      * @param  int  accepts Json::FORCE_ARRAY
 80:      * @return mixed
 81:      */
 82:     public static function decode($json, $options = 0)
 83:     {
 84:         $json = (string) $json;
 85:         if (!preg_match('##u', $json)) {
 86:             throw new JsonException('Invalid UTF-8 sequence', 5); // workaround for PHP < 5.3.3 & PECL JSON-C
 87:         }
 88: 
 89:         $args = array($json, (bool) ($options & self::FORCE_ARRAY));
 90:         $args[] = 512;
 91:         if (PHP_VERSION_ID >= 50400 && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) { // not implemented in PECL JSON-C 1.3.2 for 64bit systems
 92:             $args[] = JSON_BIGINT_AS_STRING;
 93:         }
 94:         $value = call_user_func_array('json_decode', $args);
 95: 
 96:         if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' do not clean json_last_error
 97:             $error = json_last_error();
 98:             throw new JsonException(isset(static::$messages[$error]) ? static::$messages[$error] : 'Unknown error', $error);
 99:         }
100:         return $value;
101:     }
102: 
103: }
104: 
105: 
106: /**
107:  * The exception that indicates error of JSON encoding/decoding.
108:  */
109: class JsonException extends \Exception
110: {
111: }
112: 
API documentation generated by ApiGen 2.8.0