2009-09-10 16:56:58 +00:00
|
|
|
<?php
|
2010-08-11 12:45:59 +00:00
|
|
|
/**
|
2012-05-04 06:29:11 +00:00
|
|
|
* Simple wrapper for json_econde and json_decode that falls back on Services_JSON class.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-22 14:31:05 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2010-08-11 12:45:59 +00:00
|
|
|
*/
|
2010-08-22 14:31:05 +00:00
|
|
|
|
2012-08-27 19:03:15 +00:00
|
|
|
require_once __DIR__ . '/Services_JSON.php';
|
2011-02-19 16:59:34 +00:00
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* JSON formatter wrapper class
|
|
|
|
|
*/
|
2010-05-04 15:29:17 +00:00
|
|
|
class FormatJson {
|
2011-10-26 04:15:09 +00:00
|
|
|
|
2010-08-12 07:27:48 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the JSON representation of a value.
|
2011-10-26 04:15:09 +00:00
|
|
|
*
|
2010-08-12 07:27:48 +00:00
|
|
|
* @param $value Mixed: the value being encoded. Can be any type except a resource.
|
|
|
|
|
* @param $isHtml Boolean
|
2011-02-05 21:06:44 +00:00
|
|
|
*
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: "$isHtml" parameter's purpose is not documented. It appears to
|
2011-02-05 21:06:44 +00:00
|
|
|
* map to a parameter labeled "pretty-print output with indents and
|
|
|
|
|
* newlines" in Services_JSON::encode(), which has no string relation
|
|
|
|
|
* to HTML output.
|
2011-10-26 04:15:09 +00:00
|
|
|
*
|
2010-08-12 07:27:48 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-05-15 10:39:41 +00:00
|
|
|
public static function encode( $value, $isHtml = false ) {
|
2012-08-12 19:31:01 +00:00
|
|
|
if ( !function_exists( 'json_encode' ) || ( $isHtml && version_compare( PHP_VERSION, '5.4.0', '<' ) ) ) {
|
2009-09-10 14:33:25 +00:00
|
|
|
$json = new Services_JSON();
|
2010-05-15 10:39:41 +00:00
|
|
|
return $json->encode( $value, $isHtml );
|
2009-09-10 14:33:25 +00:00
|
|
|
} else {
|
2012-08-12 19:31:01 +00:00
|
|
|
return json_encode( $value, $isHtml ? JSON_PRETTY_PRINT : 0 );
|
2009-09-10 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-15 10:39:41 +00:00
|
|
|
|
2010-08-12 07:27:48 +00:00
|
|
|
/**
|
|
|
|
|
* Decodes a JSON string.
|
2011-10-26 04:15:09 +00:00
|
|
|
*
|
2010-08-12 07:27:48 +00:00
|
|
|
* @param $value String: the json string being decoded.
|
|
|
|
|
* @param $assoc Boolean: when true, returned objects will be converted into associative arrays.
|
2011-10-26 04:15:09 +00:00
|
|
|
*
|
2010-08-12 07:27:48 +00:00
|
|
|
* @return Mixed: the value encoded in json in appropriate PHP type.
|
|
|
|
|
* Values true, false and null (case-insensitive) are returned as true, false
|
2012-07-10 12:48:06 +00:00
|
|
|
* and "&null;" respectively. "&null;" is returned if the json cannot be
|
2010-08-12 07:27:48 +00:00
|
|
|
* decoded or if the encoded data is deeper than the recursion limit.
|
|
|
|
|
*/
|
2010-08-11 13:03:45 +00:00
|
|
|
public static function decode( $value, $assoc = false ) {
|
2010-05-15 10:39:41 +00:00
|
|
|
if ( !function_exists( 'json_decode' ) ) {
|
2011-12-20 20:15:42 +00:00
|
|
|
$json = $assoc ? new Services_JSON( SERVICES_JSON_LOOSE_TYPE ) :
|
|
|
|
|
new Services_JSON();
|
2009-09-17 01:19:02 +00:00
|
|
|
$jsonDec = $json->decode( $value );
|
2009-09-10 14:33:25 +00:00
|
|
|
return $jsonDec;
|
|
|
|
|
} else {
|
2011-03-10 12:04:38 +00:00
|
|
|
return json_decode( $value, $assoc );
|
2009-09-10 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-10-26 04:15:09 +00:00
|
|
|
|
2011-02-19 16:59:34 +00:00
|
|
|
}
|