PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/json - json.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 88 91 96.7 %
Date: 2015-06-27 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2015 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: Omar Kilani <omar@php.net>                                   |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : 
      25             : #include "php.h"
      26             : #include "php_ini.h"
      27             : #include "ext/standard/info.h"
      28             : #include "ext/standard/html.h"
      29             : #include "zend_smart_str.h"
      30             : #include "php_json.h"
      31             : #include "php_json_encoder.h"
      32             : #include "php_json_parser.h"
      33             : #include <zend_exceptions.h>
      34             : 
      35             : #include <float.h>
      36             : #if defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP)
      37             : #define NUM_BUF_SIZE (3 + DBL_MANT_DIG - DBL_MIN_EXP)
      38             : #else
      39             : #define NUM_BUF_SIZE 1080
      40             : #endif
      41             : 
      42             : 
      43             : static PHP_MINFO_FUNCTION(json);
      44             : static PHP_FUNCTION(json_encode);
      45             : static PHP_FUNCTION(json_decode);
      46             : static PHP_FUNCTION(json_last_error);
      47             : static PHP_FUNCTION(json_last_error_msg);
      48             : 
      49             : PHP_JSON_API zend_class_entry *php_json_serializable_ce;
      50             : 
      51             : ZEND_DECLARE_MODULE_GLOBALS(json)
      52             : 
      53             : /* {{{ arginfo */
      54             : ZEND_BEGIN_ARG_INFO_EX(arginfo_json_encode, 0, 0, 1)
      55             :         ZEND_ARG_INFO(0, value)
      56             :         ZEND_ARG_INFO(0, options)
      57             :         ZEND_ARG_INFO(0, depth)
      58             : ZEND_END_ARG_INFO()
      59             : 
      60             : ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
      61             :         ZEND_ARG_INFO(0, json)
      62             :         ZEND_ARG_INFO(0, assoc)
      63             :         ZEND_ARG_INFO(0, depth)
      64             :         ZEND_ARG_INFO(0, options)
      65             : ZEND_END_ARG_INFO()
      66             : 
      67             : ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
      68             : ZEND_END_ARG_INFO()
      69             : 
      70             : ZEND_BEGIN_ARG_INFO(arginfo_json_last_error_msg, 0)
      71             : ZEND_END_ARG_INFO()
      72             : /* }}} */
      73             : 
      74             : /* {{{ json_functions[] */
      75             : static const zend_function_entry json_functions[] = {
      76             :         PHP_FE(json_encode, arginfo_json_encode)
      77             :         PHP_FE(json_decode, arginfo_json_decode)
      78             :         PHP_FE(json_last_error, arginfo_json_last_error)
      79             :         PHP_FE(json_last_error_msg, arginfo_json_last_error_msg)
      80             :         PHP_FE_END
      81             : };
      82             : /* }}} */
      83             : 
      84             : /* {{{ JsonSerializable methods */
      85             : ZEND_BEGIN_ARG_INFO(json_serialize_arginfo, 0)
      86             :         /* No arguments */
      87             : ZEND_END_ARG_INFO();
      88             : 
      89             : static const zend_function_entry json_serializable_interface[] = {
      90             :         PHP_ABSTRACT_ME(JsonSerializable, jsonSerialize, json_serialize_arginfo)
      91             :         PHP_FE_END
      92             : };
      93             : /* }}} */
      94             : 
      95             : /* {{{ MINIT */
      96       21049 : static PHP_MINIT_FUNCTION(json)
      97             : {
      98             :         zend_class_entry ce;
      99             : 
     100       21049 :         INIT_CLASS_ENTRY(ce, "JsonSerializable", json_serializable_interface);
     101       21049 :         php_json_serializable_ce = zend_register_internal_interface(&ce);
     102             : 
     103       21049 :         REGISTER_LONG_CONSTANT("JSON_HEX_TAG",  PHP_JSON_HEX_TAG,  CONST_CS | CONST_PERSISTENT);
     104       21049 :         REGISTER_LONG_CONSTANT("JSON_HEX_AMP",  PHP_JSON_HEX_AMP,  CONST_CS | CONST_PERSISTENT);
     105       21049 :         REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | CONST_PERSISTENT);
     106       21049 :         REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT);
     107       21049 :         REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT);
     108       21049 :         REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT);
     109       21049 :         REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT);
     110       21049 :         REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT);
     111       21049 :         REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
     112       21049 :         REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);
     113       21049 :         REGISTER_LONG_CONSTANT("JSON_PRESERVE_ZERO_FRACTION", PHP_JSON_PRESERVE_ZERO_FRACTION, CONST_CS | CONST_PERSISTENT);
     114             : 
     115       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
     116       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
     117       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_STATE_MISMATCH", PHP_JSON_ERROR_STATE_MISMATCH, CONST_CS | CONST_PERSISTENT);
     118       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT);
     119       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT);
     120       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT);
     121       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION, CONST_CS | CONST_PERSISTENT);
     122       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN, CONST_CS | CONST_PERSISTENT);
     123       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE, CONST_CS | CONST_PERSISTENT);
     124       21049 :         REGISTER_LONG_CONSTANT("JSON_ERROR_INVALID_PROPERTY_NAME", PHP_JSON_ERROR_INVALID_PROPERTY_NAME, CONST_CS | CONST_PERSISTENT);
     125             : 
     126       21049 :         REGISTER_LONG_CONSTANT("JSON_OBJECT_AS_ARRAY",                PHP_JSON_OBJECT_AS_ARRAY,               CONST_CS | CONST_PERSISTENT);
     127       21049 :         REGISTER_LONG_CONSTANT("JSON_BIGINT_AS_STRING",               PHP_JSON_BIGINT_AS_STRING,              CONST_CS | CONST_PERSISTENT);
     128             : 
     129       21049 :         return SUCCESS;
     130             : }
     131             : /* }}} */
     132             : 
     133             : /* {{{ PHP_GINIT_FUNCTION
     134             : */
     135       21049 : static PHP_GINIT_FUNCTION(json)
     136             : {
     137             : #if defined(COMPILE_DL_JSON) && defined(ZTS)
     138             :         ZEND_TSRMLS_CACHE_UPDATE();
     139             : #endif
     140       21049 :         json_globals->encoder_depth = 0;
     141       21049 :         json_globals->error_code = 0;
     142       21049 :         json_globals->encode_max_depth = 0;
     143       21049 : }
     144             : /* }}} */
     145             : 
     146             : 
     147             : /* {{{ json_module_entry
     148             :  */
     149             : zend_module_entry json_module_entry = {
     150             :         STANDARD_MODULE_HEADER,
     151             :         "json",
     152             :         json_functions,
     153             :         PHP_MINIT(json),
     154             :         NULL,
     155             :         NULL,
     156             :         NULL,
     157             :         PHP_MINFO(json),
     158             :         PHP_JSON_VERSION,
     159             :         PHP_MODULE_GLOBALS(json),
     160             :         PHP_GINIT(json),
     161             :         NULL,
     162             :         NULL,
     163             :         STANDARD_MODULE_PROPERTIES_EX
     164             : };
     165             : /* }}} */
     166             : 
     167             : #ifdef COMPILE_DL_JSON
     168             : #ifdef ZTS
     169             : ZEND_TSRMLS_CACHE_DEFINE();
     170             : #endif
     171             : ZEND_GET_MODULE(json)
     172             : #endif
     173             : 
     174             : /* {{{ PHP_MINFO_FUNCTION
     175             :  */
     176         142 : static PHP_MINFO_FUNCTION(json)
     177             : {
     178         142 :         php_info_print_table_start();
     179         142 :         php_info_print_table_row(2, "json support", "enabled");
     180         142 :         php_info_print_table_row(2, "json version", PHP_JSON_VERSION);
     181         142 :         php_info_print_table_end();
     182         142 : }
     183             : /* }}} */
     184             : 
     185        1849 : PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
     186             : {
     187        1849 :         php_json_encode_zval(buf, val, options);
     188        1849 : }
     189             : /* }}} */
     190             : 
     191         209 : PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
     192             : {
     193             :         php_json_parser parser;
     194             : 
     195         209 :         php_json_parser_init(&parser, return_value, str, str_len, (int)options, (int)depth);
     196             : 
     197         209 :         if (php_json_yyparse(&parser)) {
     198          71 :                 JSON_G(error_code) = php_json_parser_error_code(&parser);
     199          71 :                 RETURN_NULL();
     200             :         }
     201             : }
     202             : /* }}} */
     203             : 
     204             : /* {{{ proto string json_encode(mixed data [, int options[, int depth]])
     205             :    Returns the JSON representation of a value */
     206         155 : static PHP_FUNCTION(json_encode)
     207             : {
     208             :         zval *parameter;
     209         155 :         smart_str buf = {0};
     210         155 :         zend_long options = 0;
     211         155 :         zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
     212             : 
     213         155 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", &parameter, &options, &depth) == FAILURE) {
     214           1 :                 return;
     215             :         }
     216             : 
     217         154 :         JSON_G(error_code) = PHP_JSON_ERROR_NONE;
     218             : 
     219         154 :         JSON_G(encode_max_depth) = (int)depth;
     220             : 
     221         154 :         php_json_encode(&buf, parameter, (int)options);
     222             : 
     223         170 :         if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
     224             :                 smart_str_free(&buf);
     225          16 :                 ZVAL_FALSE(return_value);
     226             :         } else {
     227             :                 smart_str_0(&buf); /* copy? */
     228         138 :                 ZVAL_NEW_STR(return_value, buf.s);
     229             :         }
     230             : }
     231             : /* }}} */
     232             : 
     233             : /* {{{ proto mixed json_decode(string json [, bool assoc [, long depth]])
     234             :    Decodes the JSON representation into a PHP value */
     235         222 : static PHP_FUNCTION(json_decode)
     236             : {
     237             :         char *str;
     238             :         size_t str_len;
     239         222 :         zend_bool assoc = 0; /* return JS objects as PHP objects by default */
     240         222 :         zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
     241         222 :         zend_long options = 0;
     242             : 
     243         222 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) {
     244           3 :                 return;
     245             :         }
     246             : 
     247         219 :         JSON_G(error_code) = 0;
     248             : 
     249         219 :         if (!str_len) {
     250          10 :                 JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
     251          10 :                 RETURN_NULL();
     252             :         }
     253             : 
     254             :         /* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */
     255         209 :         if (assoc) {
     256          59 :                 options |=  PHP_JSON_OBJECT_AS_ARRAY;
     257             :         } else {
     258         150 :                 options &= ~PHP_JSON_OBJECT_AS_ARRAY;
     259             :         }
     260             : 
     261         209 :         php_json_decode_ex(return_value, str, str_len, options, depth);
     262             : }
     263             : /* }}} */
     264             : 
     265             : /* {{{ proto int json_last_error()
     266             :    Returns the error code of the last json_encode() or json_decode() call. */
     267          75 : static PHP_FUNCTION(json_last_error)
     268             : {
     269          75 :         if (zend_parse_parameters_none() == FAILURE) {
     270           0 :                 return;
     271             :         }
     272             : 
     273          75 :         RETURN_LONG(JSON_G(error_code));
     274             : }
     275             : /* }}} */
     276             : 
     277             : /* {{{ proto string json_last_error_msg()
     278             :    Returns the error string of the last json_encode() or json_decode() call. */
     279          24 : static PHP_FUNCTION(json_last_error_msg)
     280             : {
     281          24 :         if (zend_parse_parameters_none() == FAILURE) {
     282           0 :                 return;
     283             :         }
     284             : 
     285          24 :         switch(JSON_G(error_code)) {
     286             :                 case PHP_JSON_ERROR_NONE:
     287           2 :                         RETURN_STRING("No error");
     288             :                 case PHP_JSON_ERROR_DEPTH:
     289           2 :                         RETURN_STRING("Maximum stack depth exceeded");
     290             :                 case PHP_JSON_ERROR_STATE_MISMATCH:
     291           2 :                         RETURN_STRING("State mismatch (invalid or malformed JSON)");
     292             :                 case PHP_JSON_ERROR_CTRL_CHAR:
     293           2 :                         RETURN_STRING("Control character error, possibly incorrectly encoded");
     294             :                 case PHP_JSON_ERROR_SYNTAX:
     295           2 :                         RETURN_STRING("Syntax error");
     296             :                 case PHP_JSON_ERROR_UTF8:
     297          16 :                         RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded");
     298             :                 case PHP_JSON_ERROR_RECURSION:
     299           8 :                         RETURN_STRING("Recursion detected");
     300             :                 case PHP_JSON_ERROR_INF_OR_NAN:
     301           8 :                         RETURN_STRING("Inf and NaN cannot be JSON encoded");
     302             :                 case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
     303           4 :                         RETURN_STRING("Type is not supported");
     304             :                 case PHP_JSON_ERROR_INVALID_PROPERTY_NAME:
     305           2 :                         RETURN_STRING("The decoded property name is invalid");
     306             :                 default:
     307           0 :                         RETURN_STRING("Unknown error");
     308             :         }
     309             : 
     310             : }
     311             : /* }}} */
     312             : 
     313             : /*
     314             :  * Local variables:
     315             :  * tab-width: 4
     316             :  * c-basic-offset: 4
     317             :  * End:
     318             :  * vim600: noet sw=4 ts=4 fdm=marker
     319             :  * vim<600: noet sw=4 ts=4
     320             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:11 +0000 (5 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.