Sfoglia il codice sorgente

ZF-10185
Zend_Json
Update prettyPrint to properly distinguish between arrays/object notation in string literals and actual encoded arrays/objects


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

adamlundrigan 14 anni fa
parent
commit
f2aba080b8
2 ha cambiato i file con 55 aggiunte e 4 eliminazioni
  1. 12 4
      library/Zend/Json.php
  2. 43 0
      tests/Zend/JsonTest.php

+ 12 - 4
library/Zend/Json.php

@@ -403,26 +403,34 @@ class Zend_Json
             $ind = $options['indent'];
         }
 
+        $inLiteral = false;
         foreach($tokens as $token) {
             if($token == '') {
                 continue;
             }
 
             $prefix = str_repeat($ind, $indent);
-            if ($token == '{' || $token == '[') {
+            if (!$inLiteral && ($token == '{' || $token == '[')) {
                 $indent++;
                 if (($result != '') && ($result[(strlen($result)-1)] == $lineBreak)) {
                     $result .= $prefix;
                 }
                 $result .= $token . $lineBreak;
-            } elseif ($token == '}' || $token == ']') {
+            } elseif (!$inLiteral && ($token == '}' || $token == ']')) {
                 $indent--;
                 $prefix = str_repeat($ind, $indent);
                 $result .= $lineBreak . $prefix . $token;
-            } elseif ($token == ',') {
+            } elseif (!$inLiteral && $token == ',') {
                 $result .= $token . $lineBreak;
             } else {
-                $result .= $prefix . $token;
+                $result .= ( $inLiteral ? '' : $prefix ) . $token;
+                
+                // Count # of unescaped double-quotes in token, subtract # of
+                // escaped double-quotes and if the result is odd then we are 
+                // inside a string literal
+                if ((substr_count($token, "\"")-substr_count($token, "\\\"")) % 2 != 0) {
+                    $inLiteral = !$inLiteral;
+                }
             }
         }
         return $result;

+ 43 - 0
tests/Zend/JsonTest.php

@@ -118,6 +118,49 @@ class Zend_JsonTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(0, $zero, 'Failed 0 integer test. Encoded: ' . serialize(Zend_Json_Encoder::encode(0)));
     }
 
+    /**
+     * @group ZF-10185
+     */
+    public function testJsonPrettyPrintWorksWithArrayNotationInStringLiteral()
+    {
+        $o = new stdClass();
+        $o->test = 1;
+        $o->faz = 'fubar';
+        
+        // The escaped double-quote in item 'stringwithjsonchars' ensures that 
+        // escaped double-quotes don't throw off prettyPrint's string literal detection
+        $test = array(
+            'simple'=>'simple test string',
+            'stringwithjsonchars'=>'\"[1,2]',
+            'complex'=>array(
+                'foo'=>'bar',
+                'far'=>'boo',
+                'faz'=>array(
+                    'obj'=>$o
+                )
+            )
+        );
+        print_r($test);
+        $pretty = Zend_Json::prettyPrint(Zend_Json::encode($test), array("indent"  => " "));
+        print_r($pretty);
+        $expected = <<<EOB
+{
+ "simple":"simple test string",
+ "stringwithjsonchars":"\\\\\\"[1,2]",
+ "complex":{
+  "foo":"bar",
+  "far":"boo",
+  "faz":{
+   "obj":{
+    "test":1,
+    "faz":"fubar"
+   }
+  }
+ }
+}
+EOB;
+        $this->assertSame($expected, $pretty);
+    }
 
     /**
      * test float encoding/decoding