@@ -40,15 +40,6 @@ static const char digits[] = "0123456789abcdef";
40
40
41
41
zend_class_entry * php_json_serializable_ce ;
42
42
43
- #define PHP_JSON_HEX_TAG (1<<0)
44
- #define PHP_JSON_HEX_AMP (1<<1)
45
- #define PHP_JSON_HEX_APOS (1<<2)
46
- #define PHP_JSON_HEX_QUOT (1<<3)
47
- #define PHP_JSON_FORCE_OBJECT (1<<4)
48
-
49
- #define PHP_JSON_OUTPUT_ARRAY 0
50
- #define PHP_JSON_OUTPUT_OBJECT 1
51
-
52
43
ZEND_DECLARE_MODULE_GLOBALS (json )
53
44
54
45
/* {{{ arginfo */
@@ -99,6 +90,7 @@ static PHP_MINIT_FUNCTION(json)
99
90
REGISTER_LONG_CONSTANT ("JSON_HEX_APOS" , PHP_JSON_HEX_APOS , CONST_CS | CONST_PERSISTENT );
100
91
REGISTER_LONG_CONSTANT ("JSON_HEX_QUOT" , PHP_JSON_HEX_QUOT , CONST_CS | CONST_PERSISTENT );
101
92
REGISTER_LONG_CONSTANT ("JSON_FORCE_OBJECT" , PHP_JSON_FORCE_OBJECT , CONST_CS | CONST_PERSISTENT );
93
+ REGISTER_LONG_CONSTANT ("JSON_NUMERIC_CHECK" , PHP_JSON_NUMERIC_CHECK , CONST_CS | CONST_PERSISTENT );
102
94
103
95
REGISTER_LONG_CONSTANT ("JSON_ERROR_NONE" , PHP_JSON_ERROR_NONE , CONST_CS | CONST_PERSISTENT );
104
96
REGISTER_LONG_CONSTANT ("JSON_ERROR_DEPTH" , PHP_JSON_ERROR_DEPTH , CONST_CS | CONST_PERSISTENT );
@@ -312,6 +304,30 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
312
304
return ;
313
305
}
314
306
307
+ if (options & PHP_JSON_NUMERIC_CHECK ) {
308
+ double d ;
309
+ int type ;
310
+ long p ;
311
+
312
+ if ((type = is_numeric_string (s , len , & p , & d , 0 )) != 0 ) {
313
+ if (type == IS_LONG ) {
314
+ smart_str_append_long (buf , p );
315
+ } else if (type == IS_DOUBLE ) {
316
+ if (!zend_isinf (d ) && !zend_isnan (d )) {
317
+ char * tmp ;
318
+ int l = spprintf (& tmp , 0 , "%.*k" , (int ) EG (precision ), d );
319
+ smart_str_appendl (buf , tmp , l );
320
+ efree (tmp );
321
+ } else {
322
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "double %.9g does not conform to the JSON spec, encoded as 0" , d );
323
+ smart_str_appendc (buf , '0' );
324
+ }
325
+ }
326
+ return ;
327
+ }
328
+
329
+ }
330
+
315
331
utf16 = (unsigned short * ) safe_emalloc (len , sizeof (unsigned short ), 0 );
316
332
317
333
len = utf8_to_utf16 (utf16 , s , len );
@@ -496,7 +512,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
496
512
smart_str_appendl (buf , d , len );
497
513
efree (d );
498
514
} else {
499
- zend_error ( E_WARNING , "[json] (php_json_encode) double %.9g does not conform to the JSON spec, encoded as 0" , dbl );
515
+ php_error_docref ( NULL TSRMLS_CC , E_WARNING , "double %.9g does not conform to the JSON spec, encoded as 0" , dbl );
500
516
smart_str_appendc (buf , '0' );
501
517
}
502
518
}
@@ -517,7 +533,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
517
533
break ;
518
534
519
535
default :
520
- zend_error ( E_WARNING , "[json] (php_json_encode) type is unsupported, encoded as null" );
536
+ php_error_docref ( NULL TSRMLS_CC , E_WARNING , "type is unsupported, encoded as null" );
521
537
smart_str_appendl (buf , "null" , 4 );
522
538
break ;
523
539
}
0 commit comments