Navigation Menu

Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions library/Zend/Json/Json.php
Expand Up @@ -366,24 +366,32 @@ public static function prettyPrint($json, $options = array())
$ind = $options['indent']; $ind = $options['indent'];
} }


$inLiteral = false;
foreach($tokens as $token) { foreach($tokens as $token) {
if($token == "") continue; if($token == "") continue;


$prefix = str_repeat($ind, $indent); $prefix = str_repeat($ind, $indent);
if($token == "{" || $token == "[") { if(!$inLiteral && ($token == "{" || $token == "[")) {
$indent++; $indent++;
if($result != "" && $result[strlen($result)-1] == "\n") { if($result != "" && $result[strlen($result)-1] == "\n") {
$result .= $prefix; $result .= $prefix;
} }
$result .= "$token\n"; $result .= "$token\n";
} else if($token == "}" || $token == "]") { } else if(!$inLiteral && ($token == "}" || $token == "]")) {
$indent--; $indent--;
$prefix = str_repeat($ind, $indent); $prefix = str_repeat($ind, $indent);
$result .= "\n$prefix$token"; $result .= "\n$prefix$token";
} else if($token == ",") { } else if(!$inLiteral && $token == ",") {
$result .= "$token\n"; $result .= "$token\n";
} else { } 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; return $result;
Expand Down
42 changes: 42 additions & 0 deletions tests/Zend/Json/JsonTest.php
Expand Up @@ -772,6 +772,48 @@ public function testDefaultTypeObject()
$this->assertInstanceOf('stdClass', Json\Decoder::decode('{"var":"value"}')); $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);
}

} }


/** /**
Expand Down