瀏覽代碼

ZF-11356
Zend_Json
Modify Zend_Json_Encoder to properly encode namespaced class names


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24151 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan 14 年之前
父節點
當前提交
30949e8928
共有 3 個文件被更改,包括 55 次插入1 次删除
  1. 2 1
      library/Zend/Json/Encoder.php
  2. 34 0
      tests/Zend/Json/_files/ZF11356-NamespacedClass.php
  3. 19 0
      tests/Zend/JsonTest.php

+ 2 - 1
library/Zend/Json/Encoder.php

@@ -151,7 +151,8 @@ class Zend_Json_Encoder
             }
         }
 
-        return '{"__className":"' . get_class($value) . '"'
+        $className = get_class($value);
+        return '{"__className":' . $this->_encodeString($className)
                 . $props . '}';
     }
 

+ 34 - 0
tests/Zend/Json/_files/ZF11356-NamespacedClass.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Json
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+namespace Zend\JsonTest\ZF11356;
+
+/**
+ * Namespaced Class for use in Zend_JsonTest::testEncoderEscapesNamespacedClassNamesProperly
+ *
+ * @category   Zend
+ * @package    Zend_Json
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class NamespacedClass extends \ArrayIterator
+{
+}

+ 19 - 0
tests/Zend/JsonTest.php

@@ -762,6 +762,25 @@ class Zend_JsonTest extends PHPUnit_Framework_TestCase
         $expectedDecoding = '{"__className":"ArrayIterator","0":"foo"}';
         $this->assertEquals($encoded, $expectedDecoding);
     }
+    
+    /**
+     * @group ZF-11356
+     */
+    public function testEncoderEscapesNamespacedClassNamesProperly()
+    {
+        if (version_compare(PHP_VERSION, '5.3.0') === -1) {
+            $this->markTestSkipped('Namespaces not available in PHP < 5.3.0');
+        }
+        
+        require_once dirname(__FILE__ ) . "/Json/_files/ZF11356-NamespacedClass.php";        
+        $inputValue = new \Zend\JsonTest\ZF11356\NamespacedClass(array('foo'));
+        
+        $encoded = Zend_Json_Encoder::encode($inputValue);
+        $this->assertEquals(
+            '{"__className":"Zend\\\\JsonTest\\\\ZF11356\\\\NamespacedClass","0":"foo"}',
+            $encoded
+        );
+    }
 }
 
 /**