aharvey                                  Thu, 16 Sep 2010 16:21:15 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=303425

Log:
Implement FR #44331 (Formatting option for json_encode). Bikeshedding about the
exact form of the JSON pretty printing and brace handling will only be accepted
in the form of patches. ;)

Bug: http://bugs.php.net/44331 (Open) Formatting option for json_encode
      
Changed paths:
    U   php/php-src/trunk/NEWS
    U   php/php-src/trunk/UPGRADING
    U   php/php-src/trunk/ext/json/json.c
    U   php/php-src/trunk/ext/json/php_json.h
    A   php/php-src/trunk/ext/json/tests/json_encode_pretty_print.phpt

Modified: php/php-src/trunk/NEWS
===================================================================
--- php/php-src/trunk/NEWS      2010-09-16 16:12:32 UTC (rev 303424)
+++ php/php-src/trunk/NEWS      2010-09-16 16:21:15 UTC (rev 303425)
@@ -121,6 +121,7 @@
 - Implemented FR #49366 (Make slash escaping optional in json_encode()). (Adam)
 - Implemented FR #48632 (OpenSSL AES support). (yonas dot y
    at gmail dot com, Pierre)
+- Implemented FR #44331 (Formatting option for json_encode). (Adam)
 - Implemented FR #42060 (Add paged Results support). (a...@openldap.org,
   iaren...@eteo.mondragon.edu, jean...@au-fil-du.net, remy.sai...@gmail.com)
 - Implemented FR #34857 (Change array_combine behaviour when called with empty

Modified: php/php-src/trunk/UPGRADING
===================================================================
--- php/php-src/trunk/UPGRADING 2010-09-16 16:12:32 UTC (rev 303424)
+++ php/php-src/trunk/UPGRADING 2010-09-16 16:21:15 UTC (rev 303425)
@@ -235,6 +235,7 @@

      f. New global constants

+       - JSON_PRETTY_PRINT
        - JSON_UNESCAPED_SLASHES

      g. New classes

Modified: php/php-src/trunk/ext/json/json.c
===================================================================
--- php/php-src/trunk/ext/json/json.c   2010-09-16 16:12:32 UTC (rev 303424)
+++ php/php-src/trunk/ext/json/json.c   2010-09-16 16:21:15 UTC (rev 303425)
@@ -94,6 +94,7 @@
        REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, 
CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, 
CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", 
PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, 
CONST_CS | CONST_PERSISTENT);

        REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS 
| CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, 
CONST_CS | CONST_PERSISTENT);
@@ -113,6 +114,7 @@
 */
 static PHP_GINIT_FUNCTION(json)
 {
+       json_globals->encoder_depth = 0;
        json_globals->error_code = 0;
 }
 /* }}} */
@@ -189,6 +191,30 @@
 }
 /* }}} */

+/* {{{ Pretty printing support functions */
+
+static inline void json_pretty_print_char(smart_str *buf, int options, char c 
TSRMLS_DC) /* {{{ */
+{
+       if (options & PHP_JSON_PRETTY_PRINT) {
+               smart_str_appendc(buf, c);
+       }
+}
+/* }}} */
+
+static inline void json_pretty_print_indent(smart_str *buf, int options 
TSRMLS_DC) /* {{{ */
+{
+       int i;
+
+       if (options & PHP_JSON_PRETTY_PRINT) {
+               for (i = 0; i < JSON_G(encoder_depth); ++i) {
+                       smart_str_appendl(buf, "    ", 4);
+               }
+       }
+}
+/* }}} */
+
+/* }}} */
+
 static void json_encode_array(smart_str *buf, zval **val, int options 
TSRMLS_DC) /* {{{ */
 {
        int i, r;
@@ -214,6 +240,9 @@
                smart_str_appendc(buf, '{');
        }

+       json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
+       ++JSON_G(encoder_depth);
+
        i = myht ? zend_hash_num_elements(myht) : 0;

        if (i > 0)
@@ -241,10 +270,12 @@
                                if (r == PHP_JSON_OUTPUT_ARRAY) {
                                        if (need_comma) {
                                                smart_str_appendc(buf, ',');
+                                               json_pretty_print_char(buf, 
options, '\n' TSRMLS_CC);
                                        } else {
                                                need_comma = 1;
                                        }
-
+
+                                       json_pretty_print_indent(buf, options 
TSRMLS_CC);
                                        php_json_encode(buf, *data, options 
TSRMLS_CC);
                                } else if (r == PHP_JSON_OUTPUT_OBJECT) {
                                        if (i == HASH_KEY_IS_STRING) {
@@ -258,26 +289,36 @@

                                                if (need_comma) {
                                                        smart_str_appendc(buf, 
',');
+                                                       
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
                                                } else {
                                                        need_comma = 1;
                                                }

+                                               json_pretty_print_indent(buf, 
options TSRMLS_CC);
+
                                                json_escape_string(buf, key, 
key_len - 1, options TSRMLS_CC);
                                                smart_str_appendc(buf, ':');

+                                               json_pretty_print_char(buf, 
options, ' ' TSRMLS_CC);
+
                                                php_json_encode(buf, *data, 
options TSRMLS_CC);
                                        } else {
                                                if (need_comma) {
                                                        smart_str_appendc(buf, 
',');
+                                                       
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
                                                } else {
                                                        need_comma = 1;
                                                }

+                                               json_pretty_print_indent(buf, 
options TSRMLS_CC);
+
                                                smart_str_appendc(buf, '"');
                                                smart_str_append_long(buf, 
(long) index);
                                                smart_str_appendc(buf, '"');
                                                smart_str_appendc(buf, ':');

+                                               json_pretty_print_char(buf, 
options, ' ' TSRMLS_CC);
+
                                                php_json_encode(buf, *data, 
options TSRMLS_CC);
                                        }
                                }
@@ -288,6 +329,10 @@
                        }
                }
        }
+
+       --JSON_G(encoder_depth);
+       json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
+       json_pretty_print_indent(buf, options TSRMLS_CC);

        if (r == PHP_JSON_OUTPUT_ARRAY) {
                smart_str_appendc(buf, ']');

Modified: php/php-src/trunk/ext/json/php_json.h
===================================================================
--- php/php-src/trunk/ext/json/php_json.h       2010-09-16 16:12:32 UTC (rev 
303424)
+++ php/php-src/trunk/ext/json/php_json.h       2010-09-16 16:21:15 UTC (rev 
303425)
@@ -38,6 +38,7 @@
 #endif

 ZEND_BEGIN_MODULE_GLOBALS(json)
+       int encoder_depth;
        int error_code;
 ZEND_END_MODULE_GLOBALS(json)

@@ -60,6 +61,7 @@
 #define PHP_JSON_FORCE_OBJECT  (1<<4)
 #define PHP_JSON_NUMERIC_CHECK (1<<5)
 #define PHP_JSON_UNESCAPED_SLASHES     (1<<6)
+#define PHP_JSON_PRETTY_PRINT  (1<<7)

 /* Internal flags */
 #define PHP_JSON_OUTPUT_ARRAY  0

Added: php/php-src/trunk/ext/json/tests/json_encode_pretty_print.phpt
===================================================================
--- php/php-src/trunk/ext/json/tests/json_encode_pretty_print.phpt              
                (rev 0)
+++ php/php-src/trunk/ext/json/tests/json_encode_pretty_print.phpt      
2010-09-16 16:21:15 UTC (rev 303425)
@@ -0,0 +1,40 @@
+--TEST--
+json_encode() with JSON_PRETTY_PRINT
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+function encode_decode($json) {
+       $struct = json_decode($json);
+       $pretty = json_encode($struct, JSON_PRETTY_PRINT);
+       echo "$pretty\n";
+       $pretty = json_decode($pretty);
+       printf("Match: %d\n", $pretty == $struct);
+}
+
+encode_decode('[1,2,3,[1,2,3]]');
+encode_decode('{"a":1,"b":[1,2],"c":{"d":42}}');
+?>
+--EXPECT--
+[
+    1,
+    2,
+    3,
+    [
+        1,
+        2,
+        3
+    ]
+]
+Match: 1
+{
+    "a": 1,
+    "b": [
+        1,
+        2
+    ],
+    "c": {
+        "d": 42
+    }
+}
+Match: 1

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to