Commit 07cbd47b authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #202

parents 2e9b3268 eaf59995
......@@ -5,6 +5,7 @@ namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;
/**
* Model class for a BSON array.
......@@ -14,7 +15,7 @@ use ArrayObject;
*
* @api
*/
class BSONArray extends ArrayObject implements Serializable, Unserializable
class BSONArray extends ArrayObject implements JsonSerializable, Serializable, Unserializable
{
/**
* Factory method for var_export().
......@@ -56,4 +57,18 @@ class BSONArray extends ArrayObject implements Serializable, Unserializable
{
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());
}
}
......@@ -5,6 +5,7 @@ namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;
/**
* Model class for a BSON document.
......@@ -14,7 +15,7 @@ use ArrayObject;
*
* @api
*/
class BSONDocument extends ArrayObject implements Serializable, Unserializable
class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
{
/**
* Constructor.
......@@ -66,4 +67,15 @@ class BSONDocument extends ArrayObject implements Serializable, Unserializable
{
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();
}
}
......@@ -3,6 +3,7 @@
namespace MongoDB\Tests;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
class BSONArrayTest extends TestCase
{
......@@ -15,6 +16,29 @@ class BSONArrayTest extends TestCase
$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'];
......
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use ArrayObject;
......@@ -23,6 +24,29 @@ class BSONDocumentTest extends TestCase
$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'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment