Back to index

php5  5.3.10
Defines | Functions | Variables
php_json.h File Reference
#include "ext/standard/php_smart_str.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define PHP_JSON_VERSION   "1.2.1"
#define phpext_json_ptr   &json_module_entry
#define PHP_JSON_API   PHPAPI
#define JSON_G(v)   (json_globals.v)
#define PHP_JSON_HEX_TAG   (1<<0)
#define PHP_JSON_HEX_AMP   (1<<1)
#define PHP_JSON_HEX_APOS   (1<<2)
#define PHP_JSON_HEX_QUOT   (1<<3)
#define PHP_JSON_FORCE_OBJECT   (1<<4)
#define PHP_JSON_NUMERIC_CHECK   (1<<5)
#define PHP_JSON_OUTPUT_ARRAY   0
#define PHP_JSON_OUTPUT_OBJECT   1

Functions

PHP_JSON_API void php_json_encode (smart_str *buf, zval *val, int options TSRMLS_DC)
PHP_JSON_API void php_json_decode (zval *return_value, char *str, int str_len, zend_bool assoc, long depth TSRMLS_DC)

Variables

zend_module_entry json_module_entry
int error_code

Define Documentation

#define JSON_G (   v)    (json_globals.v)

Definition at line 47 of file php_json.h.

#define PHP_JSON_API   PHPAPI

Definition at line 33 of file php_json.h.

#define PHP_JSON_FORCE_OBJECT   (1<<4)

Definition at line 57 of file php_json.h.

#define PHP_JSON_HEX_AMP   (1<<1)

Definition at line 54 of file php_json.h.

#define PHP_JSON_HEX_APOS   (1<<2)

Definition at line 55 of file php_json.h.

#define PHP_JSON_HEX_QUOT   (1<<3)

Definition at line 56 of file php_json.h.

#define PHP_JSON_HEX_TAG   (1<<0)

Definition at line 53 of file php_json.h.

#define PHP_JSON_NUMERIC_CHECK   (1<<5)

Definition at line 58 of file php_json.h.

#define PHP_JSON_OUTPUT_ARRAY   0

Definition at line 60 of file php_json.h.

#define PHP_JSON_OUTPUT_OBJECT   1

Definition at line 61 of file php_json.h.

#define PHP_JSON_VERSION   "1.2.1"

Definition at line 24 of file php_json.h.

Definition at line 28 of file php_json.h.


Function Documentation

PHP_JSON_API void php_json_decode ( zval *  return_value,
char *  str,
int  str_len,
zend_bool  assoc,
long depth  TSRMLS_DC 
)

Definition at line 488 of file json.c.

{
       int utf16_len;
       zval *z;
       unsigned short *utf16;
       JSON_parser jp;

       utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1);

       utf16_len = utf8_to_utf16(utf16, str, str_len);
       if (utf16_len <= 0) {
              if (utf16) {
                     efree(utf16);
              }
              JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
              RETURN_NULL();
       }

       if (depth <= 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Depth must be greater than zero");
              efree(utf16);
              RETURN_NULL();
       }

       ALLOC_INIT_ZVAL(z);
       jp = new_JSON_parser(depth);
       if (parse_JSON(jp, z, utf16, utf16_len, assoc TSRMLS_CC)) {
              *return_value = *z;
       }
       else
       {
              double d;
              int type;
              long p;

              RETVAL_NULL();
              if (str_len == 4) {
                     if (!strcasecmp(str, "null")) {
                            /* We need to explicitly clear the error because its an actual NULL and not an error */
                            jp->error_code = PHP_JSON_ERROR_NONE;
                            RETVAL_NULL();
                     } else if (!strcasecmp(str, "true")) {
                            RETVAL_BOOL(1);
                     }
              } else if (str_len == 5 && !strcasecmp(str, "false")) {
                     RETVAL_BOOL(0);
              }

              if ((type = is_numeric_string(str, str_len, &p, &d, 0)) != 0) {
                     if (type == IS_LONG) {
                            RETVAL_LONG(p);
                     } else if (type == IS_DOUBLE) {
                            RETVAL_DOUBLE(d);
                     }
              }

              if (Z_TYPE_P(return_value) != IS_NULL) {
                     jp->error_code = PHP_JSON_ERROR_NONE;
              }

              zval_dtor(z);
       }
       FREE_ZVAL(z);
       efree(utf16);
       JSON_G(error_code) = jp->error_code;
       free_JSON_parser(jp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHP_JSON_API void php_json_encode ( smart_str buf,
zval *  val,
int options  TSRMLS_DC 
)

Definition at line 432 of file json.c.

{
       switch (Z_TYPE_P(val))
       {
              case IS_NULL:
                     smart_str_appendl(buf, "null", 4);
                     break;

              case IS_BOOL:
                     if (Z_BVAL_P(val)) {
                            smart_str_appendl(buf, "true", 4);
                     } else {
                            smart_str_appendl(buf, "false", 5);
                     }
                     break;

              case IS_LONG:
                     smart_str_append_long(buf, Z_LVAL_P(val));
                     break;

              case IS_DOUBLE:
                     {
                            char *d = NULL;
                            int len;
                            double dbl = Z_DVAL_P(val);

                            if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
                                   len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl);
                                   smart_str_appendl(buf, d, len);
                                   efree(d);
                            } else {
                                   php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl);
                                   smart_str_appendc(buf, '0');
                            }
                     }
                     break;

              case IS_STRING:
                     json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
                     break;

              case IS_ARRAY:
              case IS_OBJECT:
                     json_encode_array(buf, &val, options TSRMLS_CC);
                     break;

              default:
                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
                     smart_str_appendl(buf, "null", 4);
                     break;
       }

       return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 41 of file php_json.h.

zend_module_entry json_module_entry

Definition at line 99 of file json.c.