diff options
author | Callum Macrae <callum@lynxphp.com> | 2011-08-19 10:45:03 +0100 |
---|---|---|
committer | Igor Wiedler <igor@wiedler.ch> | 2012-03-31 02:09:13 +0200 |
commit | dce38f44de04bd7a1f91f8e57f6d266bd5e1af86 (patch) | |
tree | 8c12244b1c2e052e03c06675848bff5d2297859a /phpBB/includes/json.php | |
parent | 94172b54dd09b28e19b4b12933b3f96e498d264a (diff) | |
download | forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.gz forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.bz2 forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.xz forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.zip |
[ticket/10328] Added a JSON class.
The JSON class adds a consistent way to send JSON to the client, making
it perfect for AJAX (jQuery automatically parses it).
PHPBB3-10328
Diffstat (limited to 'phpBB/includes/json.php')
-rw-r--r-- | phpBB/includes/json.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/phpBB/includes/json.php b/phpBB/includes/json.php new file mode 100644 index 0000000000..04472080d9 --- /dev/null +++ b/phpBB/includes/json.php @@ -0,0 +1,59 @@ +<?php +/** +* +* @package phpBB3 +* @version $Id$ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* JSON class +* @package phpBB3 +*/ +class JSON +{ + private static $data = array(); + + /** + * Send the data to the client and exit the script. + * + * @param array $data Any additional data to send. + * @param bool $exit Will exit the script if true. + */ + public static function send($data = false, $exit = true) + { + if ($data) + { + self::add($data); + } + + header('Content-type: application/json'); + echo json_encode(self::$data); + + if ($exit) + { + garbage_collection(); + exit_handler(); + } + } + + /** + * Saves some data to be written when JSON::send() is called. + * + * @param array $data Data to save to be sent. + */ + public static function add($data) + { + self::$data = array_merge(self::$data, $data); + } +} |