Namespaces

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

Classes

  • Arrays
  • Callback
  • FileSystem
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • 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 (https://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', // exists since 5.3.3, but is returned since 5.3.1
 30:     );
 31: 
 32: 
 33:     /**
 34:      * Static class - cannot be instantiated.
 35:      */
 36:     final public function __construct()
 37:     {
 38:         throw new Nette\StaticClassException;
 39:     }
 40: 
 41: 
 42:     /**
 43:      * Returns the JSON representation of a value.
 44:      * @param  mixed
 45:      * @param  int  accepts Json::PRETTY
 46:      * @return string
 47:      */
 48:     public static function encode($value, $options = 0)
 49:     {
 50:         $flags = PHP_VERSION_ID >= 50400 ? (JSON_UNESCAPED_UNICODE | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)) : 0;
 51: 
 52:         if (PHP_VERSION_ID < 50500) {
 53:             $json = Callback::invokeSafe('json_encode', array($value, $flags), function ($message) { // needed to receive 'recursion detected' error
 54:                 throw new JsonException($message);
 55:             });
 56:         } else {
 57:             $json = json_encode($value, $flags);
 58:         }
 59: 
 60:         if ($error = json_last_error()) {
 61:             $message = isset(static::$messages[$error]) ? static::$messages[$error]
 62:                 : (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error');
 63:             throw new JsonException($message, $error);
 64:         }
 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  accepts Json::FORCE_ARRAY
 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('Invalid UTF-8 sequence', 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:         $args = array($json, $forceArray, 512);
 89:         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
 90:             $args[] = JSON_BIGINT_AS_STRING;
 91:         }
 92:         $value = call_user_func_array('json_decode', $args);
 93: 
 94:         if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' is not clearing json_last_error
 95:             $error = json_last_error();
 96:             throw new JsonException(isset(static::$messages[$error]) ? static::$messages[$error] : 'Unknown error', $error);
 97:         }
 98:         return $value;
 99:     }
100: 
101: }
102: 
103: 
104: /**
105:  * The exception that indicates error of JSON encoding/decoding.
106:  */
107: class JsonException extends \Exception
108: {
109: }
110: 
Nette 2.1 API documentation generated by ApiGen 2.8.0