trejder
trejder

Reputation: 17495

Encode unescaped JSON in Yii 1.x

I need to generate JSON from PHP array, whitout escaping / characters (mostly un URLs).

In pure PHP I can do just json_encode($results, JSON_UNESCAPED_SLASHES); (in PHP 5.4.0+).

Is there any way, I can achieve the same using CJSON::encode(); or any other Yii 1.x class?

Upvotes: 2

Views: 870

Answers (1)

DarkMukke
DarkMukke

Reputation: 2489

If you read the code of CJSON, you see that it uses standard php if available for decode/encode functions. Unfortunately it's not up to speed yet with 5.4 as Yii 1.x has a requirement of PHP 5.1.

I suggest you add the extra functionality by extending CJON yourself, something like this :

class MyJSON extends CJSON {
    public static function encode($var, $options = null, $depth = null)
    {
        if (function_exists('json_encode') && version_compare(PHP_VERSION, '5.5.0') >= 0) {
            return json_encode($var, $options, $depth);
        } elseif (function_exists('json_encode') && version_compare(PHP_VERSION, '5.3.0') >= 0) {
            return json_encode($var, $options);
        } else {
            return parent::encode($var);
        }
    }
}

Upvotes: 2

Related Questions