From aa8ab527f2466ff234a71cfafb1a1d3f5deba0c1 Mon Sep 17 00:00:00 2001 From: Omar Kilani Date: Sat, 18 Mar 2006 04:15:16 +0000 Subject: [PATCH] Fix PECL bug #7147 - rework comma insertion whilst encoding. Add tests to package.xml. --- ext/json/json.c | 26 +++++++++++++++++++------- ext/json/package.xml | 22 +++++++++++++++++++--- ext/json/php_json.h | 2 +- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/ext/json/json.c b/ext/json/json.c index b4f993b239..d19eb2e9b5 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -153,8 +153,7 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) { ulong index; uint key_len; HashPosition pos; - int htlen = i; - int wpos = 0; + int need_comma = 0; zend_hash_internal_pointer_reset_ex(myht, &pos); for (;; zend_hash_move_forward_ex(myht, &pos)) { @@ -164,6 +163,12 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) { if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) { if (r == 0) { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + json_encode_r(buf, *data TSRMLS_CC); } else if (r == 1) { if (i == HASH_KEY_IS_STRING) { @@ -172,11 +177,23 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) { continue; } + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + json_escape_string(buf, key, key_len - 1 TSRMLS_CC); smart_str_appendc(buf, ':'); json_encode_r(buf, *data TSRMLS_CC); } else { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + smart_str_appendc(buf, '"'); smart_str_append_long(buf, (long) index); smart_str_appendc(buf, '"'); @@ -185,11 +202,6 @@ static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) { json_encode_r(buf, *data TSRMLS_CC); } } - - if (htlen > 1 && wpos++ < htlen - 1) - { - smart_str_appendc(buf, ','); - } } } } diff --git a/ext/json/package.xml b/ext/json/package.xml index 3ffeab7bae..0651de736f 100644 --- a/ext/json/package.xml +++ b/ext/json/package.xml @@ -18,10 +18,11 @@ PHP 3.01 stable - 1.2.0 - 2006-03-15 + 1.2.1 + 2006-03-18 - Complete rewrite using JSON_checker as the base for the parser. Implements the JSON specification. 3-8x faster on encodes and 1.2x-4x faster on decodes. + Fix PECL bug #7147 - rework handling of comma insertion while encoding. + Add tests to package.xml @@ -39,6 +40,13 @@ + + + + + + + @@ -129,6 +137,14 @@ Cleanup and TSRM performance fixes by rasmus. + + stable + 1.2.0 + 2006-03-15 + + Complete rewrite using JSON_checker as the base for the parser. Implements the JSON specification. 3-8x faster on encodes and 1.2x-4x faster on decodes. + +