From 430d05b058ac3df435600a678cda365dba3eb1b7 Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Mon, 15 Nov 2010 00:38:54 +0100 Subject: Use native PHP JSON functions when available --- inc/JSON.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'inc/JSON.php') diff --git a/inc/JSON.php b/inc/JSON.php index 332827f4c..d1fbd404a 100644 --- a/inc/JSON.php +++ b/inc/JSON.php @@ -130,6 +130,7 @@ class JSON { /** * encodes an arbitrary variable into JSON format + * If available the native PHP JSON implementation is used. * * @param mixed $var any number, boolean, string, array, or object to be encoded. * see argument 1 to JSON() above for array-parsing behavior. @@ -140,6 +141,7 @@ class JSON { * @access public */ function encode($var) { + if (function_exists('json_encode')) return json_encode($var); switch (gettype($var)) { case 'boolean': return $var ? 'true' : 'false'; @@ -352,6 +354,7 @@ class JSON { /** * decodes a JSON string into appropriate variable + * If available the native PHP JSON implementation is used. * * @param string $str JSON-formatted string * @@ -363,6 +366,7 @@ class JSON { * @access public */ function decode($str) { + if (function_exists('json_decode')) return json_decode($str); $str = $this->reduce_string($str); switch (strtolower($str)) { -- cgit v1.2.3 From dc9bdeadf4b4eeacd7b6c7a51269a4400e4d25d8 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 18 Nov 2010 22:42:42 +0100 Subject: Improve native JSON usage This patch does two things: It makes sure the native json_decode() will honor the JSON_LOOSETYPE option of the class and it also adds way to skip the use of the native function completely. The latter is necessary for slightly non-standard JSON data. --- inc/JSON.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'inc/JSON.php') diff --git a/inc/JSON.php b/inc/JSON.php index d1fbd404a..2dea44003 100644 --- a/inc/JSON.php +++ b/inc/JSON.php @@ -112,6 +112,16 @@ define('JSON_STRICT_TYPE', 11); * @deprecated */ class JSON { + + /** + * Disables the use of PHP5's native json_decode() + * + * You shouldn't change this usually because the native function is much + * faster. However, this non-native will also parse slightly broken JSON + * which might be handy when talking to a non-conform endpoint + */ + public $skipnative = false; + /** * constructs a new JSON instance * @@ -366,7 +376,10 @@ class JSON { * @access public */ function decode($str) { - if (function_exists('json_decode')) return json_decode($str); + if (!$this->skipnative && function_exists('json_decode')){ + return json_decode($str,($this->use == JSON_LOOSE_TYPE)); + } + $str = $this->reduce_string($str); switch (strtolower($str)) { -- cgit v1.2.3