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

Classes

Exceptions

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