Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • LimitedScope
  • MimeTypeDetector
  • ObjectMixin
  • Paginator
  • Random
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 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: class Json
17: {
18:     use Nette\StaticClass;
19: 
20:     const FORCE_ARRAY = 0b0001;
21:     const PRETTY = 0b0010;
22: 
23: 
24:     /**
25:      * Returns the JSON representation of a value.
26:      * @param  mixed
27:      * @param  int  accepts Json::PRETTY
28:      * @return string
29:      */
30:     public static function encode($value, $options = 0)
31:     {
32:         $flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
33:             | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)
34:             | (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0); // since PHP 5.6.6 & PECL JSON-C 1.3.7
35: 
36:         $json = json_encode($value, $flags);
37:         if ($error = json_last_error()) {
38:             throw new JsonException(json_last_error_msg(), $error);
39:         }
40: 
41:         $json = str_replace(["\xe2\x80\xa8", "\xe2\x80\xa9"], ['\u2028', '\u2029'], $json);
42:         return $json;
43:     }
44: 
45: 
46:     /**
47:      * Decodes a JSON string.
48:      * @param  string
49:      * @param  int  accepts Json::FORCE_ARRAY
50:      * @return mixed
51:      */
52:     public static function decode($json, $options = 0)
53:     {
54:         $json = (string) $json;
55:         if (defined('JSON_C_VERSION') && !preg_match('##u', $json)) {
56:             throw new JsonException('Invalid UTF-8 sequence', 5);
57:         } elseif ($json === '') { // for PHP < 7
58:             throw new JsonException('Syntax error');
59:         }
60: 
61:         $forceArray = (bool) ($options & self::FORCE_ARRAY);
62:         if (PHP_VERSION_ID < 70000 && !$forceArray && preg_match('#(?<=[^\\\\]")\\\\u0000(?:[^"\\\\]|\\\\.)*+"\s*+:#', $json)) {
63:             throw new JsonException('The decoded property name is invalid'); // workaround for json_decode fatal error when object key starts with \u0000
64:         }
65:         $flags = !defined('JSON_C_VERSION') || PHP_INT_SIZE === 4 ? JSON_BIGINT_AS_STRING : 0; // not implemented in PECL JSON-C 1.3.2 for 64bit systems
66: 
67:         $value = json_decode($json, $forceArray, 512, $flags);
68:         if ($error = json_last_error()) {
69:             throw new JsonException(json_last_error_msg(), $error);
70:         }
71:         return $value;
72:     }
73: 
74: }
75: 
Nette 2.4-20160930 API API documentation generated by ApiGen 2.8.0