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

Classes

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

Exceptions

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