Skip to content

Loading…

[hotfix/ZF-10185] Zend\Json\Json #359

Closed
wants to merge 1 commit into from

2 participants

@adamlundrigan

Update Zend\Json\Json::prettyPrint to properly distinguish between array/object notation in string literals and actual encoded arrays/objects

@adamlundrigan adamlundrigan [hotfix/ZF-10185] Update Zend\Json\Json::prettyPrint to properly dist…
…inguish between array/object notation in string literals and actual encoded arrays/objects
ab0f4f0
@weierophinney
Zend Framework member

Reviewed, merged, and pushed to master - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 28, 2011
  1. @adamlundrigan

    [hotfix/ZF-10185] Update Zend\Json\Json::prettyPrint to properly dist…

    adamlundrigan committed
    …inguish between array/object notation in string literals and actual encoded arrays/objects
Showing with 54 additions and 4 deletions.
  1. +12 −4 library/Zend/Json/Json.php
  2. +42 −0 tests/Zend/Json/JsonTest.php
View
16 library/Zend/Json/Json.php
@@ -366,24 +366,32 @@ public static function prettyPrint($json, $options = array())
$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] == "\n") {
$result .= $prefix;
}
$result .= "$token\n";
- } else if($token == "}" || $token == "]") {
+ } else if(!$inLiteral && ($token == "}" || $token == "]")) {
$indent--;
$prefix = str_repeat($ind, $indent);
$result .= "\n$prefix$token";
- } else if($token == ",") {
+ } else if(!$inLiteral && $token == ",") {
$result .= "$token\n";
} 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;
View
42 tests/Zend/Json/JsonTest.php
@@ -772,6 +772,48 @@ public function testDefaultTypeObject()
$this->assertInstanceOf('stdClass', Json\Decoder::decode('{"var":"value"}'));
}
+ /**
+ * @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
+ )
+ )
+ );
+ $pretty = Json\Json::prettyPrint(Json\Json::encode($test), array("indent" => " "));
+ $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);
+ }
+
}
/**
Something went wrong with that request. Please try again.