Why use CJSON encode when we have json_encode

Active3 hr before
Viewed126 times

8 Answers


Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

In addition to @kuldeep.kamboj, I should say CJSON::encode will treat that 17 as an integer if you define the data type of the value like this:

// PHP
$toBeConverted = array('id' => (int) 17); // or (int) $myInteger

$jsonString = \CJSON::encode($toBeConverted);

// $jsonString will be:
   "id": 17

Encodes an arbitrary variable into JSON format,decodes a JSON string into appropriate variable,This function returns any UTF-8 encoded text as a list of Unicode values:,This function converts a Unicode array back to its UTF-8 representation

load more v

CJSON::encode will determine which is array and which is hash, and encode to array or object separately,I like CJOSN::encode, but I want it encode my hash & array both into object.,that will force my array translate into object.,sometimes, my array in PHP will have continues index from 0, and be treated as array

Never used CJSON, but would typecasting work?

dan at elearnapp dot com 13 - Jan - 2011 12: 01

If you need to force an object(ex: empty array) you can also do :

      ? php json_encode((object) $arr); ? >

   which acts the same as

   ? php json_encode($arr, JSON_FORCE_OBJECT); ? >
load more v

use the JSON_FORCE_OBJECT option of json_encode:,This mitigates the issue of JSON_FORCE_OBJECT converting a normal array into an object.,By casting the array into an object, json_encode will always use braces instead of brackets for the value (even when empty).,Alternatively you could actually use objects in your PHP code instead of arrays:

I have this code

$status = array(
   "message" => "error",
   "club_id" => $_club_id,
   "status" => "1",
   "membership_info" => array(),
load more v

def encrypt(claims, jwk, adata = '', add_header = None, alg = 'RSA-OAEP',
      enc = 'A128CBC-HS256', rng = get_random_bytes, compression = None):
" Encrypts the given claims and produces a :class:`~jose.JWE`

: param claims: A `dict`
representing the claims
for this
   : class: `~jose.JWE`.: param jwk: A `dict`
representing the JWK to be used
for encryption of
   the CEK.This parameter is algorithm - specific.: param adata: Arbitrary string data to add to the authentication(i.e.HMAC).The same data must be provided during
decryption.: param add_header: Additional items to be added to the header.Additional
headers * will * be authenticated.: param alg: The algorithm to use
for CEK encryption
   : param enc: The algorithm to use
for claims encryption
   : param rng: Random number generator.A string of random bytes is expected
as output.: param compression: The compression algorithm to use.Currently supports `'DEF'`.: rtype:: class: `~jose.JWE`: raises:: class: `~jose.Error`
if there is an error producing the JWE

header = dict((add_header or {}).items() + [
   ('enc', enc), ('alg', alg)

plaintext = json_encode(claims)

# compress(
   if required)
if compression is not None:
   header['zip'] = compression
(compress, _) = COMPRESSION[compression]
except KeyError:
   raise Error(
      'Unsupported compression algorithm: {}'.format(compression))
plaintext = compress(plaintext)

# body encryption / hash((cipher, _), key_size), ((hash_fn, _), hash_mod) = JWA[enc]
iv = rng(AES.block_size)
encryption_key = rng((key_size // 8) + hash_mod.digest_size)

         ciphertext = cipher(plaintext, encryption_key[: -hash_mod.digest_size], iv) hash = hash_fn(_jwe_hash_str(plaintext, iv, adata),
            encryption_key[-hash_mod.digest_size: ], hash_mod)

         # cek encryption(cipher, _), _ = JWA[alg] encryption_key_ciphertext = cipher(encryption_key, jwk)

         return JWE( * map(b64encode_url,
load more v

Convert a Lua table into a JSON encoded string. The special value json.NULL can be used to store a null value in a Lua table since it is not possible to store a Lua null value in a table.,The json.encode function makes no attempt to detect recursive tables, therefore the code shown below will result in a stack overflow error.,See when the English version of this topic was last updated.,See when the translated version of this topic was last updated.

JSON supports an array with integer indexes or an object that has string indexes whereas Lua allows a table to use integer and string values for keys in the same table. To allow any Lua table to be serialized to a string, nzLua by default uses an encoding format that may result in a serialized table that is not compatible with standard JSON. For example, the table below cannot be encoded in the standard JSON format since it has both integer indexes and a string index.

t = {
   foo = "abc"
load more v

Change the default behavior when encoding an empty Lua table.,By default, empty Lua tables are encoded as empty JSON Objects ({}). If this is set to false, empty Lua tables will be encoded as empty JSON Arrays instead ([]).,For example, since encode_empty_table_as_object is true by default:,encode_empty_table_as_object

local cjson = require "cjson"

local json = cjson.encode({
   foo = "bar",
   some_object = {},
   some_array = cjson.empty_array
load more v

decodes a JSON string into appropriate variable,Encodes an arbitrary variable into JSON format,This function converts a Unicode array back to its UTF-8 representation,reduce a string by removing leading and trailing comments and whitespace

public static mixed decode(string $str, boolean $useArray = true)
load more v

Other "undefined-undefined" queries related to "Why use CJSON encode when we have json_encode"