Skip to content

PHPLIB-185: BSONArray and BSONDocument implement JsonSerializable #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Model/BSONArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;

/**
* Model class for a BSON array.
Expand All @@ -14,7 +15,7 @@
*
* @api
*/
class BSONArray extends ArrayObject implements Serializable, Unserializable
class BSONArray extends ArrayObject implements JsonSerializable, Serializable, Unserializable
{
/**
* Factory method for var_export().
Expand Down Expand Up @@ -56,4 +57,18 @@ public function bsonUnserialize(array $data)
{
self::__construct($data);
}

/**
* Serialize the array to JSON.
*
* The array data will be numerically reindexed to ensure that it is stored
* as a JSON array.
*
* @see http://php.net/jsonserializable.jsonserialize
* @return array
*/
public function jsonSerialize()
{
return array_values($this->getArrayCopy());
}
}
14 changes: 13 additions & 1 deletion src/Model/BSONDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;

/**
* Model class for a BSON document.
Expand All @@ -14,7 +15,7 @@
*
* @api
*/
class BSONDocument extends ArrayObject implements Serializable, Unserializable
class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
{
/**
* Constructor.
Expand Down Expand Up @@ -66,4 +67,15 @@ public function bsonUnserialize(array $data)
{
parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
}

/**
* Serialize the array to JSON.
*
* @see http://php.net/jsonserializable.jsonserialize
* @return object
*/
public function jsonSerialize()
{
return (object) $this->getArrayCopy();
}
}
24 changes: 24 additions & 0 deletions tests/Model/BSONArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Tests;

use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;

class BSONArrayTest extends TestCase
{
Expand All @@ -15,6 +16,29 @@ public function testBsonSerializeReindexesKeys()
$this->assertSame(['foo', 'bar'], $array->bsonSerialize());
}

public function testJsonSerialize()
{
$document = new BSONArray([
'foo',
new BSONArray(['foo' => 1, 'bar' => 2, 'baz' => 3]),
new BSONDocument(['foo' => 1, 'bar' => 2, 'baz' => 3]),
new BSONArray([new BSONArray([new BSONArray])]),
]);

$expectedJson = '["foo",[1,2,3],{"foo":1,"bar":2,"baz":3},[[[]]]]';

$this->assertSame($expectedJson, json_encode($document));
}

public function testJsonSerializeReindexesKeys()
{
$data = [0 => 'foo', 2 => 'bar'];

$array = new BSONArray($data);
$this->assertSame($data, $array->getArrayCopy());
$this->assertSame(['foo', 'bar'], $array->jsonSerialize());
}

public function testSetState()
{
$data = ['foo', 'bar'];
Expand Down
24 changes: 24 additions & 0 deletions tests/Model/BSONDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MongoDB\Tests;

use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use ArrayObject;

Expand All @@ -23,6 +24,29 @@ public function testBsonSerializeCastsToObject()
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize());
}

public function testJsonSerialize()
{
$document = new BSONDocument([
'foo' => 'bar',
'array' => new BSONArray([1, 2, 3]),
'object' => new BSONDocument([1, 2, 3]),
'nested' => new BSONDocument([new BSONDocument([new BSONDocument])]),
]);

$expectedJson = '{"foo":"bar","array":[1,2,3],"object":{"0":1,"1":2,"2":3},"nested":{"0":{"0":{}}}}';

$this->assertSame($expectedJson, json_encode($document));
}

public function testJsonSerializeCastsToObject()
{
$data = [0 => 'foo', 2 => 'bar'];

$document = new BSONDocument($data);
$this->assertSame($data, $document->getArrayCopy());
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->jsonSerialize());
}

public function testSetState()
{
$data = ['foo' => 'bar'];
Expand Down