Skip to content

Commit 3aa2aad

Browse files
yohgakibukka
authored andcommitted
Add JSON_G(precision)
1 parent f943daf commit 3aa2aad

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

ext/json/json.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,31 @@ static const zend_function_entry json_serializable_interface[] = {
9393
};
9494
/* }}} */
9595

96+
97+
/* {{{ PHP_INI_MH
98+
*/
99+
static PHP_INI_MH(OnSetJsonPrecision)
100+
{
101+
zend_long i;
102+
103+
ZEND_ATOL(i, ZSTR_VAL(new_value));
104+
if (i >= -1) {
105+
JSON_G(precision) = i;
106+
return SUCCESS;
107+
} else {
108+
return FAILURE;
109+
}
110+
}
111+
/* }}} */
112+
113+
114+
/* {{{ PHP_INI
115+
*/
116+
PHP_INI_BEGIN()
117+
STD_PHP_INI_ENTRY("json.precision", "-1", PHP_INI_ALL, OnSetJsonPrecision, precision, zend_json_globals, json_globals)
118+
PHP_INI_END()
119+
/* }}} */
120+
96121
/* Register constant for options and errors */
97122
#define PHP_JSON_REGISTER_CONSTANT(_name, _value) \
98123
REGISTER_LONG_CONSTANT(_name, _value, CONST_CS | CONST_PERSISTENT);
@@ -102,6 +127,8 @@ static PHP_MINIT_FUNCTION(json)
102127
{
103128
zend_class_entry ce;
104129

130+
REGISTER_INI_ENTRIES();
131+
105132
INIT_CLASS_ENTRY(ce, "JsonSerializable", json_serializable_interface);
106133
php_json_serializable_ce = zend_register_internal_interface(&ce);
107134

@@ -153,6 +180,16 @@ static PHP_GINIT_FUNCTION(json)
153180
}
154181
/* }}} */
155182

183+
/* {{{ PHP_MSHUTDOWN_FUNCTION
184+
*/
185+
static PHP_MSHUTDOWN_FUNCTION(json)
186+
{
187+
UNREGISTER_INI_ENTRIES();
188+
189+
return SUCCESS;
190+
}
191+
/* }}} */
192+
156193

157194
/* {{{ json_module_entry
158195
*/
@@ -161,7 +198,7 @@ zend_module_entry json_module_entry = {
161198
"json",
162199
json_functions,
163200
PHP_MINIT(json),
164-
NULL,
201+
PHP_MSHUTDOWN(json),
165202
NULL,
166203
NULL,
167204
PHP_MINFO(json),

ext/json/json_encoder.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
104104
{
105105
size_t len;
106106
char num[PHP_JSON_DOUBLE_MAX_LENGTH];
107-
if (PG(serialize_precision) == -1) {
107+
if (JSON_G(precision) == -1) {
108108
php_0cvt(d, 17, '.', 'e', num);
109109
} else {
110-
php_gcvt(d, PG(serialize_precision), '.', 'e', num);
110+
php_gcvt(d, JSON_G(precision), '.', 'e', num);
111111
}
112112
len = strlen(num);
113113
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_JSON_DOUBLE_MAX_LENGTH - 2) {

ext/json/php_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef enum {
8181
#define PHP_JSON_PARSER_DEFAULT_DEPTH 512
8282

8383
ZEND_BEGIN_MODULE_GLOBALS(json)
84+
zend_long precision;
8485
int encoder_depth;
8586
int encode_max_depth;
8687
php_json_error_code error_code;

0 commit comments

Comments
 (0)