Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use CJSON encode when we have json_encode

Tags:

json

php

yii

I am building an API for a website using Yii. I know that there is a utility class called CJson and has a function called encode.

As far as I know there are additional parameters that can be customized in the native json_encode function like the JSON_NUMERIC_CHECK which is really useful. It creates

{     "id": 17 } 

instead of Yii's CJSON encode which makes the '17' a string.

{     "id": "17" } 

So my question is whether there is any reason I should use CJSON encode instead of the built in PHP function json_encode ?

like image 704
ajaybc Avatar asked Jun 28 '13 06:06

ajaybc


People also ask

What is the use of JSON encode?

The json_encode() function is used to encode a value to JSON format.

What is a JSON encoded string?

The method JSON. stringify(student) takes the object and converts it into a string. The resulting json string is called a JSON-encoded or serialized or stringified or marshalled object.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.


2 Answers

Only thing I can think minimum php version support.

Yii support php 5.1 as minimum version See Yii Installation Page . While json_encode/json_decode introduced in php 5.2. So It can be a reason for Yii having a library for CJson.

like image 76
kuldeep.kamboj Avatar answered Sep 21 '22 05:09

kuldeep.kamboj


This question is old. I am working with Yii 1.4, PHP 5.4.

The difference i found was 'json_encode' encodes only class properties, while as 'CJSON::encode' encodes only properties listed at the class documentation using @property annotation... This is true at least for CActiveRecord

like image 42
amit bakle Avatar answered Sep 20 '22 05:09

amit bakle