summaryrefslogtreecommitdiff
path: root/ext/json/json_encoder.c
diff options
context:
space:
mode:
authorJakub Zelenka <bukka@php.net>2015-01-18 16:26:25 +0000
committerJakub Zelenka <bukka@php.net>2015-01-18 16:26:25 +0000
commit91386cb2879782c574d1f67b4c472f1f9b40d839 (patch)
treef19250b5b935ddabd75fb6545ac9ff200ddd08c5 /ext/json/json_encoder.c
parentb3823f5cab4e405b767cd8dddebb54b1c29bd2a8 (diff)
parent95cef47afb0b5329915a178d34cf27efcb54607b (diff)
downloadphp-git-91386cb2879782c574d1f67b4c472f1f9b40d839.tar.gz
Merge pull request #1 from jrbasso/json_preserve_fractional_part
Porting implementation of RFC json_preserve_fractional_part
Diffstat (limited to 'ext/json/json_encoder.c')
-rw-r--r--ext/json/json_encoder.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c
index 8657fd2cdb..510163d823 100644
--- a/ext/json/json_encoder.c
+++ b/ext/json/json_encoder.c
@@ -95,13 +95,18 @@ static inline void php_json_pretty_print_indent(smart_str *buf, int options) /*
/* }}} */
-static inline void php_json_encode_double(smart_str *buf, double d) /* {{{ */
+static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
{
if (!zend_isinf(d) && !zend_isnan(d)) {
size_t len;
char num[PHP_JSON_DOUBLE_MAX_LENGTH];
php_gcvt(d, EG(precision), '.', 'e', &num[0]);
len = strlen(num);
+ if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_JSON_DOUBLE_MAX_LENGTH - 2) {
+ num[len++] = '.';
+ num[len++] = '0';
+ num[len] = '\0';
+ }
smart_str_appendl(buf, num, len);
} else {
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
@@ -290,7 +295,7 @@ static void php_json_escape_string(smart_str *buf, char *s, size_t len, int opti
if (type == IS_LONG) {
smart_str_append_long(buf, p);
} else if (type == IS_DOUBLE) {
- php_json_encode_double(buf, d);
+ php_json_encode_double(buf, d, options);
}
return;
}
@@ -502,7 +507,7 @@ again:
break;
case IS_DOUBLE:
- php_json_encode_double(buf, Z_DVAL_P(val));
+ php_json_encode_double(buf, Z_DVAL_P(val), options);
break;
case IS_STRING: