Fixed time zone calculation issue
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / util / JSON.class.php
1 <?php
2 namespace wcf\util;
3 use wcf\system\exception\SystemException;
4
5 /**
6 * Provides methods for JSON.
7 *
8 * @author Alexander Ebert
9 * @copyright 2001-2014 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package com.woltlab.wcf
12 * @subpackage util
13 * @category Community Framework
14 */
15 final class JSON {
16 /**
17 * Returns the JSON representation of a value.
18 *
19 * @param mixed $data
20 * @return string
21 */
22 public static function encode($data) {
23 return json_encode($data);
24 }
25
26 /**
27 * Decodes a JSON string.
28 *
29 * @param string $json
30 * @param boolean $asArray
31 * @return array
32 */
33 public static function decode($json, $asArray = true) {
34 // decodes JSON
35 $data = json_decode($json, $asArray);
36
37 if ($data === null && self::getLastError() !== JSON_ERROR_NONE) {
38 throw new SystemException('Could not decode JSON "'.$json.'"');
39 }
40
41 return $data;
42 }
43
44 /**
45 * Returns the last error occurred.
46 *
47 * @return integer
48 */
49 public static function getLastError() {
50 return json_last_error();
51 }
52
53 private function __construct() { }
54 }