Skip to content

Commit 07cbd47

Browse files
committed
Merge pull request #202
2 parents 2e9b326 + eaf5999 commit 07cbd47

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

src/Model/BSONArray.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MongoDB\BSON\Serializable;
66
use MongoDB\BSON\Unserializable;
77
use ArrayObject;
8+
use JsonSerializable;
89

910
/**
1011
* Model class for a BSON array.
@@ -14,7 +15,7 @@
1415
*
1516
* @api
1617
*/
17-
class BSONArray extends ArrayObject implements Serializable, Unserializable
18+
class BSONArray extends ArrayObject implements JsonSerializable, Serializable, Unserializable
1819
{
1920
/**
2021
* Factory method for var_export().
@@ -56,4 +57,18 @@ public function bsonUnserialize(array $data)
5657
{
5758
self::__construct($data);
5859
}
60+
61+
/**
62+
* Serialize the array to JSON.
63+
*
64+
* The array data will be numerically reindexed to ensure that it is stored
65+
* as a JSON array.
66+
*
67+
* @see http://php.net/jsonserializable.jsonserialize
68+
* @return array
69+
*/
70+
public function jsonSerialize()
71+
{
72+
return array_values($this->getArrayCopy());
73+
}
5974
}

src/Model/BSONDocument.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MongoDB\BSON\Serializable;
66
use MongoDB\BSON\Unserializable;
77
use ArrayObject;
8+
use JsonSerializable;
89

910
/**
1011
* Model class for a BSON document.
@@ -14,7 +15,7 @@
1415
*
1516
* @api
1617
*/
17-
class BSONDocument extends ArrayObject implements Serializable, Unserializable
18+
class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
1819
{
1920
/**
2021
* Constructor.
@@ -66,4 +67,15 @@ public function bsonUnserialize(array $data)
6667
{
6768
parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
6869
}
70+
71+
/**
72+
* Serialize the array to JSON.
73+
*
74+
* @see http://php.net/jsonserializable.jsonserialize
75+
* @return object
76+
*/
77+
public function jsonSerialize()
78+
{
79+
return (object) $this->getArrayCopy();
80+
}
6981
}

tests/Model/BSONArrayTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Tests;
44

55
use MongoDB\Model\BSONArray;
6+
use MongoDB\Model\BSONDocument;
67

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

19+
public function testJsonSerialize()
20+
{
21+
$document = new BSONArray([
22+
'foo',
23+
new BSONArray(['foo' => 1, 'bar' => 2, 'baz' => 3]),
24+
new BSONDocument(['foo' => 1, 'bar' => 2, 'baz' => 3]),
25+
new BSONArray([new BSONArray([new BSONArray])]),
26+
]);
27+
28+
$expectedJson = '["foo",[1,2,3],{"foo":1,"bar":2,"baz":3},[[[]]]]';
29+
30+
$this->assertSame($expectedJson, json_encode($document));
31+
}
32+
33+
public function testJsonSerializeReindexesKeys()
34+
{
35+
$data = [0 => 'foo', 2 => 'bar'];
36+
37+
$array = new BSONArray($data);
38+
$this->assertSame($data, $array->getArrayCopy());
39+
$this->assertSame(['foo', 'bar'], $array->jsonSerialize());
40+
}
41+
1842
public function testSetState()
1943
{
2044
$data = ['foo', 'bar'];

tests/Model/BSONDocumentTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests;
44

5+
use MongoDB\Model\BSONArray;
56
use MongoDB\Model\BSONDocument;
67
use ArrayObject;
78

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

27+
public function testJsonSerialize()
28+
{
29+
$document = new BSONDocument([
30+
'foo' => 'bar',
31+
'array' => new BSONArray([1, 2, 3]),
32+
'object' => new BSONDocument([1, 2, 3]),
33+
'nested' => new BSONDocument([new BSONDocument([new BSONDocument])]),
34+
]);
35+
36+
$expectedJson = '{"foo":"bar","array":[1,2,3],"object":{"0":1,"1":2,"2":3},"nested":{"0":{"0":{}}}}';
37+
38+
$this->assertSame($expectedJson, json_encode($document));
39+
}
40+
41+
public function testJsonSerializeCastsToObject()
42+
{
43+
$data = [0 => 'foo', 2 => 'bar'];
44+
45+
$document = new BSONDocument($data);
46+
$this->assertSame($data, $document->getArrayCopy());
47+
$this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->jsonSerialize());
48+
}
49+
2650
public function testSetState()
2751
{
2852
$data = ['foo' => 'bar'];

0 commit comments

Comments
 (0)