]> granicus.if.org Git - php/commitdiff
Implement FR #44331 (Formatting option for json_encode). Bikeshedding about the
authorAdam Harvey <aharvey@php.net>
Thu, 16 Sep 2010 16:21:15 +0000 (16:21 +0000)
committerAdam Harvey <aharvey@php.net>
Thu, 16 Sep 2010 16:21:15 +0000 (16:21 +0000)
exact form of the JSON pretty printing and brace handling will only be accepted
in the form of patches. ;)

NEWS
UPGRADING
ext/json/json.c
ext/json/php_json.h
ext/json/tests/json_encode_pretty_print.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index da8139e64c06a70bae01eec52c0f419573ab0496..19094f5c4fcc932ea89da2293f9da36b165ca4a3 100644 (file)
--- a/NEWS
+++ b/NEWS
 - 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). (ando@OpenLDAP.org,
   iarenuno@eteo.mondragon.edu, jeanseb@au-fil-du.net, remy.saissy@gmail.com)
 - Implemented FR #34857 (Change array_combine behaviour when called with empty
index 11cbaa5a52bef0552ca7ab50fc758e7732bd8ba2..8b3befbe849142050f35eca8583459353f750077 100755 (executable)
--- a/UPGRADING
+++ b/UPGRADING
@@ -235,6 +235,7 @@ UPGRADE NOTES - PHP X.Y
 
      f. New global constants
 
+       - JSON_PRETTY_PRINT
        - JSON_UNESCAPED_SLASHES
 
      g. New classes
index 1836c7f7e2a27d651b080027f26000f94c413f66..95766106df7524ebc6adf2a77090ce9ad7a3561f 100644 (file)
@@ -94,6 +94,7 @@ static PHP_MINIT_FUNCTION(json)
        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_MINIT_FUNCTION(json)
 */
 static PHP_GINIT_FUNCTION(json)
 {
+       json_globals->encoder_depth = 0;
        json_globals->error_code = 0;
 }
 /* }}} */
@@ -189,6 +191,30 @@ static int json_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
 }
 /* }}} */
 
+/* {{{ 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 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
                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 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
                                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 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
 
                                                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 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
                        }
                }
        }
+       
+       --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, ']');
index c321ad292aca3aa86c0c68453b1ed4f480f08b12..7b1c7b72ff4971e9b19350139f5bafb5b3db2387 100644 (file)
@@ -38,6 +38,7 @@ extern zend_module_entry json_module_entry;
 #endif
 
 ZEND_BEGIN_MODULE_GLOBALS(json)
+       int encoder_depth;
        int error_code;
 ZEND_END_MODULE_GLOBALS(json)
 
@@ -60,6 +61,7 @@ extern zend_class_entry *php_json_serializable_ce;
 #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
diff --git a/ext/json/tests/json_encode_pretty_print.phpt b/ext/json/tests/json_encode_pretty_print.phpt
new file mode 100644 (file)
index 0000000..43b93aa
--- /dev/null
@@ -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