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

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:10:03 +0000 (7 hours ago)

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