]> granicus.if.org Git - php/commitdiff
Implemented FR #49366 (Make slash escaping optional in json_encode()).
authorAdam Harvey <aharvey@php.net>
Thu, 16 Sep 2010 13:53:27 +0000 (13:53 +0000)
committerAdam Harvey <aharvey@php.net>
Thu, 16 Sep 2010 13:53:27 +0000 (13:53 +0000)
NEWS
UPGRADING
ext/json/json.c
ext/json/php_json.h
ext/json/tests/json_encode_unescaped_slashes.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index c6ce838f69ff23b8289212d89b4f353ea8e258a7..da8139e64c06a70bae01eec52c0f419573ab0496 100644 (file)
--- a/NEWS
+++ b/NEWS
 
 - Implemented FR #52555 (Ability to get HTTP response code). (Paul Dragoonis)
 - Implemented FR #51295 (SQLite3::busyTimeout not existing). (Mark)
+- 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 #42060 (Add paged Results support). (ando@OpenLDAP.org,
index 9a31cbd630f51364803d11ce472bb6c79b61d4cd..11cbaa5a52bef0552ca7ab50fc758e7732bd8ba2 100755 (executable)
--- a/UPGRADING
+++ b/UPGRADING
@@ -235,7 +235,7 @@ UPGRADE NOTES - PHP X.Y
 
      f. New global constants
 
-       - 
+       - JSON_UNESCAPED_SLASHES
 
      g. New classes
 
index c81c05d587d5d63478847835c7ab132f85ce5837..3e893f0e8d653ecfc9ec3431051ffe4b9cce9bfa 100644 (file)
@@ -92,6 +92,7 @@ static PHP_MINIT_FUNCTION(json)
        REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT);
        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_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
@@ -372,7 +373,11 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
                                break;
 
                        case '/':
-                               smart_str_appendl(buf, "\\/", 2);
+                               if (options & PHP_JSON_UNESCAPED_SLASHES) {
+                                       smart_str_appendc(buf, '/');
+                               } else {
+                                       smart_str_appendl(buf, "\\/", 2);
+                               }
                                break;
 
                        case '\b':
index fd5287c0e5f80a42a51e52a60aa36bfe89c0fd61..c321ad292aca3aa86c0c68453b1ed4f480f08b12 100644 (file)
@@ -59,6 +59,7 @@ extern zend_class_entry *php_json_serializable_ce;
 #define PHP_JSON_HEX_QUOT      (1<<3)
 #define PHP_JSON_FORCE_OBJECT  (1<<4)
 #define PHP_JSON_NUMERIC_CHECK (1<<5)
+#define PHP_JSON_UNESCAPED_SLASHES     (1<<6)
 
 /* Internal flags */
 #define PHP_JSON_OUTPUT_ARRAY  0
diff --git a/ext/json/tests/json_encode_unescaped_slashes.phpt b/ext/json/tests/json_encode_unescaped_slashes.phpt
new file mode 100644 (file)
index 0000000..72ebae9
--- /dev/null
@@ -0,0 +1,12 @@
+--TEST--
+json_decode() tests
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+var_dump(json_encode('a/b'));
+var_dump(json_encode('a/b', JSON_UNESCAPED_SLASHES));
+?>
+--EXPECT--
+string(6) ""a\/b""
+string(5) ""a/b""