mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-25 16:22:42 +01:00
Improve "PhutilJSON" handling of PHP-object JSON values
Summary: Ref T13635. PHP native JSON functions sometimes represent JSON objects as PHP "stdClass" objects. Accept this representation and emit it correctly in "PhutilJSON". Test Plan: Added a test, made it pass. Maniphest Tasks: T13635 Differential Revision: https://secure.phabricator.com/D21604
This commit is contained in:
parent
2d6452acb5
commit
7570dd0da1
2 changed files with 36 additions and 1 deletions
|
@ -19,7 +19,7 @@ final class PhutilJSON extends Phobject {
|
||||||
* @param dict An object to encode in JSON.
|
* @param dict An object to encode in JSON.
|
||||||
* @return string Pretty-printed object representation.
|
* @return string Pretty-printed object representation.
|
||||||
*/
|
*/
|
||||||
public function encodeFormatted(array $object) {
|
public function encodeFormatted($object) {
|
||||||
return $this->encodeFormattedObject($object, 0)."\n";
|
return $this->encodeFormattedObject($object, 0)."\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,10 @@ final class PhutilJSON extends Phobject {
|
||||||
* @task internal
|
* @task internal
|
||||||
*/
|
*/
|
||||||
private function encodeFormattedObject($object, $depth) {
|
private function encodeFormattedObject($object, $depth) {
|
||||||
|
if ($object instanceof stdClass) {
|
||||||
|
$object = (array)$object;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($object)) {
|
if (empty($object)) {
|
||||||
return '{}';
|
return '{}';
|
||||||
}
|
}
|
||||||
|
@ -123,6 +127,8 @@ final class PhutilJSON extends Phobject {
|
||||||
} else {
|
} else {
|
||||||
return $this->encodeFormattedObject($value, $depth);
|
return $this->encodeFormattedObject($value, $depth);
|
||||||
}
|
}
|
||||||
|
} else if (is_object($value)) {
|
||||||
|
return $this->encodeFormattedObject($value, $depth);
|
||||||
} else {
|
} else {
|
||||||
if (defined('JSON_UNESCAPED_SLASHES')) {
|
if (defined('JSON_UNESCAPED_SLASHES')) {
|
||||||
// If we have a new enough version of PHP, disable escaping of slashes
|
// If we have a new enough version of PHP, disable escaping of slashes
|
||||||
|
|
|
@ -18,4 +18,33 @@ EOJSON;
|
||||||
pht('Empty arrays should serialize as `%s`, not `%s`.', '[]', '{}'));
|
pht('Empty arrays should serialize as `%s`, not `%s`.', '[]', '{}'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNestedObjectEncoding() {
|
||||||
|
$expect = <<<EOJSON
|
||||||
|
{
|
||||||
|
"empty-object": {},
|
||||||
|
"pair-object": {
|
||||||
|
"duck": "quack"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EOJSON;
|
||||||
|
|
||||||
|
$empty_object = new stdClass();
|
||||||
|
|
||||||
|
$pair_object = new stdClass();
|
||||||
|
$pair_object->duck = 'quack';
|
||||||
|
|
||||||
|
$input = (object)array(
|
||||||
|
'empty-object' => $empty_object,
|
||||||
|
'pair-object' => $pair_object,
|
||||||
|
);
|
||||||
|
|
||||||
|
$serializer = new PhutilJSON();
|
||||||
|
|
||||||
|
$this->assertEqual(
|
||||||
|
$expect,
|
||||||
|
$serializer->encodeFormatted($input),
|
||||||
|
pht('Serialization of PHP-object JSON values.'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue