Saturday, June 8, 2013

Here’s an easy forward-compatible way to use JSON (json_encode() and json_decode()) in versions of PHP earlier than 5.2.
And then add the following to your custom functions:
JSON DECODE

if (!function_exists('json_decode'))
  {
     
       function json_decode($content, $assoc=false)
                   {
                       require_once 'classes/JSON.php';
                       if ($assoc)
                         {
                            $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
                         }
                     else
                        {
                           $json = new Services_JSON;
                        }
                     return $json->decode($content);
                 }
}

JSON ENCODE

if (!function_exists('json_encode'))
{
    function json_encode($content)
       {
         require_once 'classes/JSON.php';
        $json = new Services_JSON;
        return $json->encode($content);
       }
}
That’s it! Your code will continue to work, even when you eventually upgrade to PHP 5.2

No comments: