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: /**
19:  * JSON encoder and decoder.
20:  *
21:  * @author     David Grudl
22:  */
23: final class Json
24: {
25:     const FORCE_ARRAY = 1;
26: 
27:     /** @var array */
28:     private static $messages = array(
29:         JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
30:         JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
31:         JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
32:         JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
33:     );
34: 
35: 
36: 
37:     /**
38:      * Static class - cannot be instantiated.
39:      */
40:     final public function __construct()
41:     {
42:         throw new Nette\StaticClassException;
43:     }
44: 
45: 
46: 
47:     /**
48:      * Returns the JSON representation of a value.
49:      * @param  mixed
50:      * @return string
51:      */
52:     public static function encode($value)
53:     {
54:         if (function_exists('ini_set')) { // workaround for PHP bugs #52397, #54109, #63004
55:             $old = ini_set('display_errors', 0); // needed to receive 'Invalid UTF-8 sequence' error
56:         }
57:         set_error_handler(function($severity, $message) { // needed to receive 'recursion detected' error
58:             restore_error_handler();
59:             throw new JsonException($message);
60:         });
61:         $json = json_encode($value);
62:         restore_error_handler();
63:         if (isset($old)) {
64:             ini_set('display_errors', $old);
65:         }
66:         return $json;
67:     }
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:         $value = json_decode($json, (bool) ($options & self::FORCE_ARRAY));
81:         if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' do not clean json_last_error
82:             $error = PHP_VERSION_ID >= 50300 ? json_last_error() : 0;
83:             throw new JsonException(isset(static::$messages[$error]) ? static::$messages[$error] : 'Unknown error', $error);
84:         }
85:         return $value;
86:     }
87: 
88: }
89: 
90: 
91: 
92: /**
93:  * The exception that indicates error of JSON encoding/decoding.
94:  */
95: class JsonException extends \Exception
96: {
97: }
98: 
Nette Framework 2.0.10 API API documentation generated by ApiGen 2.8.0