pollita         Mon Dec 17 07:48:21 2007 UTC

  Added files:                 
    /php-src/ext/json/tests     006.phpt 

  Modified files:              
    /php-src/ext/json   json.c 
  Log:
  Add support for encoding options
  
http://cvs.php.net/viewvc.cgi/php-src/ext/json/json.c?r1=1.31&r2=1.32&diff_format=u
Index: php-src/ext/json/json.c
diff -u php-src/ext/json/json.c:1.31 php-src/ext/json/json.c:1.32
--- php-src/ext/json/json.c:1.31        Mon Oct  1 15:25:01 2007
+++ php-src/ext/json/json.c     Mon Dec 17 07:48:21 2007
@@ -16,7 +16,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: json.c,v 1.31 2007/10/01 15:25:01 iliaa Exp $ */
+/* $Id: json.c,v 1.32 2007/12/17 07:48:21 pollita Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -32,6 +32,11 @@
 
 static const char digits[] = "0123456789abcdef";
 
+#define PHP_JSON_HEX_TAG       (1<<0)
+#define PHP_JSON_HEX_AMP       (1<<1)
+#define PHP_JSON_HEX_APOS      (1<<2)
+#define PHP_JSON_HEX_QUOT      (1<<3)
+
 /* {{{ json_functions[]
  *
  * Every user visible function must have an entry in json_functions[].
@@ -43,6 +48,19 @@
 };
 /* }}} */
 
+/* {{{ MINIT */
+static PHP_MINIT_FUNCTION(json)
+{
+       REGISTER_LONG_CONSTANT("JSON_HEX_TAG",  PHP_JSON_HEX_TAG,  CONST_CS | 
CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("JSON_HEX_AMP",  PHP_JSON_HEX_AMP,  CONST_CS | 
CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | 
CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | 
CONST_PERSISTENT);
+
+       return SUCCESS;
+}
+/* }}} */
+
+
 /* {{{ json_module_entry
  */
 zend_module_entry json_module_entry = {
@@ -51,7 +69,7 @@
 #endif
        "json",
        json_functions,
-       NULL,
+       PHP_MINIT(json),
        NULL,
        NULL,
        NULL,
@@ -78,8 +96,8 @@
 }
 /* }}} */
 
-static void json_encode_r(smart_str *buf, zval *val TSRMLS_DC);
-static void json_escape_string(smart_str *buf, zstr s, int len, zend_uchar 
type);
+static void json_encode_r(smart_str *buf, zval *val, int options TSRMLS_DC);
+static void json_escape_string(smart_str *buf, zstr s, int len, zend_uchar 
type, int options);
 
 static int json_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
 {
@@ -115,7 +133,7 @@
 }
 /* }}} */
 
-static void json_encode_array(smart_str *buf, zval **val TSRMLS_DC) /* {{{ */
+static void json_encode_array(smart_str *buf, zval **val, int options 
TSRMLS_DC) /* {{{ */
 {
        int i, r;
        HashTable *myht;
@@ -172,7 +190,7 @@
                                                need_comma = 1;
                                        }
  
-                                       json_encode_r(buf, *data TSRMLS_CC);
+                                       json_encode_r(buf, *data, options 
TSRMLS_CC);
                                } else if (r == 1) {
                                        if (i == HASH_KEY_IS_STRING ||
                                                i == HASH_KEY_IS_UNICODE) {
@@ -187,10 +205,10 @@
                                                        need_comma = 1;
                                                }
 
-                                               json_escape_string(buf, key, 
key_len - 1, (i==HASH_KEY_IS_UNICODE)?IS_UNICODE:IS_STRING);
+                                               json_escape_string(buf, key, 
key_len - 1, (i==HASH_KEY_IS_UNICODE)?IS_UNICODE:IS_STRING, options);
                                                smart_str_appendc(buf, ':');
 
-                                               json_encode_r(buf, *data 
TSRMLS_CC);
+                                               json_encode_r(buf, *data, 
options TSRMLS_CC);
                                        } else {
                                                if (need_comma) {
                                                        smart_str_appendc(buf, 
',');
@@ -203,7 +221,7 @@
                                                smart_str_appendc(buf, '"');
                                                smart_str_appendc(buf, ':');
 
-                                               json_encode_r(buf, *data 
TSRMLS_CC);
+                                               json_encode_r(buf, *data, 
options TSRMLS_CC);
                                        }
                                }
 
@@ -227,7 +245,7 @@
 
 #define REVERSE16(us) (((us & 0xf) << 12) | (((us >> 4) & 0xf) << 8) | (((us 
>> 8) & 0xf) << 4) | ((us >> 12) & 0xf))
 
-static void json_escape_string(smart_str *buf, zstr s, int len, zend_uchar 
type) /* {{{ */
+static void json_escape_string(smart_str *buf, zstr s, int len, zend_uchar 
type, int options) /* {{{ */
 {
        int pos = 0;
        unsigned short us;
@@ -267,7 +285,11 @@
                {
                        case '"':
                                {
-                                       smart_str_appendl(buf, "\\\"", 2);
+                                       if (options & PHP_JSON_HEX_QUOT) {
+                                               smart_str_appendl(buf, 
"\\u0022", 6);
+                                       } else {
+                                               smart_str_appendl(buf, "\\\"", 
2);
+                                       }
                                }
                                break;
                        case '\\':
@@ -305,6 +327,42 @@
                                        smart_str_appendl(buf, "\\t", 2);
                                }
                                break;
+                       case '<':
+                               {
+                                       if (options & PHP_JSON_HEX_TAG) {
+                                               smart_str_appendl(buf, 
"\\u003C", 6);
+                                       } else {
+                                               smart_str_appendc(buf, '<');
+                                       }
+                               }
+                               break;
+                       case '>':
+                               {
+                                       if (options & PHP_JSON_HEX_TAG) {
+                                               smart_str_appendl(buf, 
"\\u003E", 6);
+                                       } else {
+                                               smart_str_appendc(buf, '>');
+                                       }
+                               }
+                               break;
+                       case '&':
+                               {
+                                       if (options & PHP_JSON_HEX_AMP) {
+                                               smart_str_appendl(buf, 
"\\u0026", 6);
+                                       } else {
+                                               smart_str_appendc(buf, '&');
+                                       }
+                               }
+                               break;
+                       case '\'':
+                               {
+                                       if (options & PHP_JSON_HEX_APOS) {
+                                               smart_str_appendl(buf, 
"\\u0027", 6);
+                                       } else {
+                                               smart_str_appendc(buf, '\'');
+                                       }
+                               }
+                               break;
                        default:
                                {
                                        if (us >= ' ' && (us & 127) == us)
@@ -337,7 +395,7 @@
 }
 /* }}} */
 
-static void json_encode_r(smart_str *buf, zval *val TSRMLS_DC) /* {{{ */
+static void json_encode_r(smart_str *buf, zval *val, int options TSRMLS_DC) /* 
{{{ */
 {
        switch (Z_TYPE_P(val)) {
                case IS_NULL:
@@ -374,11 +432,11 @@
                        break;
                case IS_STRING:
                case IS_UNICODE:
-                       json_escape_string(buf, Z_UNIVAL_P(val), 
Z_UNILEN_P(val), Z_TYPE_P(val));
+                       json_escape_string(buf, Z_UNIVAL_P(val), 
Z_UNILEN_P(val), Z_TYPE_P(val), options);
                        break;
                case IS_ARRAY:
                case IS_OBJECT:
-                       json_encode_array(buf, &val TSRMLS_CC);
+                       json_encode_array(buf, &val, options TSRMLS_CC);
                        break;
                default:
                        zend_error(E_WARNING, "[json] (json_encode_r) type is 
unsupported, encoded as null.");
@@ -396,12 +454,13 @@
 {
        zval *parameter;
        smart_str buf = {0};
+       long options = 0;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) 
== FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &parameter, 
&options) == FAILURE) {
                return;
        }
 
-       json_encode_r(&buf, parameter TSRMLS_CC);
+       json_encode_r(&buf, parameter, options TSRMLS_CC);
 
        /*
         * Return as binary string, since the result is 99% likely to be just

http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/006.phpt?view=markup&rev=1.1
Index: php-src/ext/json/tests/006.phpt
+++ php-src/ext/json/tests/006.phpt
--TEST--
json_encode() & extended encoding
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

$a = array('<foo>',"'bar'",'"baz"','&blong&');

echo "Normal: ", json_encode($a), "\n";
echo "Tags: ",   json_encode($a,JSON_HEX_TAG), "\n";
echo "Apos: ",   json_encode($a,JSON_HEX_APOS), "\n";
echo "Quot: ",   json_encode($a,JSON_HEX_QUOT), "\n";
echo "Amp: ",    json_encode($a,JSON_HEX_AMP), "\n";
echo "All: ",    
json_encode($a,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP), "\n";
?>
--EXPECT--
Normal: ["<foo>","'bar'","\"baz\"","&blong&"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026"]

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

Reply via email to