Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

HHVM-158: BSON objects should implement JsonSerializable #148

Merged
merged 3 commits into from
Oct 14, 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
89 changes: 80 additions & 9 deletions ext_mongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ public function unserialize(mixed $data) : void
}
}

final class Binary implements Type, \Serializable
final class Binary implements Type, \Serializable, \JsonSerializable
{
static private function checkArray(array $state)
{
Expand All @@ -966,6 +966,14 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
return [
'$binary' => base64_encode( $this->data ),
'$type' => sprintf( "%02x", $this->type )
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down Expand Up @@ -1012,7 +1020,7 @@ function __debugInfo() : array;
}

<<__NativeData("MongoDBBsonDecimal128")>>
final class Decimal128 implements Type, \Serializable
final class Decimal128 implements Type, \Serializable, \JsonSerializable
{
static private function checkArray(array $state)
{
Expand All @@ -1030,6 +1038,13 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
return [
'$numberDecimal' => (string) $this->dec
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand All @@ -1053,7 +1068,7 @@ function __toString() : string;
function __debugInfo() : array;
}

final class Javascript implements Type, \Serializable
final class Javascript implements Type, \Serializable, \JsonSerializable
{
private $code;
private $scope;
Expand Down Expand Up @@ -1084,6 +1099,11 @@ public function serialize() : string
return serialize( $s );
}

public function jsonSerialize() : mixed
{
return $this->code;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that you're being consistent with libmongoc here (code as string and ignoring the scope). There is no corresponding extended JSON representation: https://docs.mongodb.com/v3.2/reference/mongodb-extended-json/#bson-data-types-and-associated-representations

}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down Expand Up @@ -1145,13 +1165,20 @@ public function __toString() : string
}
}

final class MaxKey implements Type, \Serializable
final class MaxKey implements Type, \Serializable, \JsonSerializable
{
public function serialize() : string
{
return '';
}

public function jsonSerialize() : mixed
{
return [
'$maxKey' => 1
];
}

public function unserialize(mixed $serialized) : void
{
}
Expand All @@ -1162,13 +1189,20 @@ static public function __set_state(array $state)
}
}

final class MinKey implements Type, \Serializable
final class MinKey implements Type, \Serializable, \JsonSerializable
{
public function serialize() : string
{
return '';
}

public function jsonSerialize() : mixed
{
return [
'$minKey' => 1
];
}

public function unserialize(mixed $serialized) : void
{
}
Expand All @@ -1180,7 +1214,7 @@ static public function __set_state(array $state)
}

<<__NativeData("MongoDBBsonObjectID")>>
final class ObjectID implements Type, \Serializable
final class ObjectID implements Type, \Serializable, \JsonSerializable
{
static private function checkArray( array $state )
{
Expand All @@ -1198,6 +1232,13 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
return [
'$oid' => $this->__toString()
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down Expand Up @@ -1226,7 +1267,7 @@ public function getTimestamp() : int
}
}

final class Regex implements Type, \Serializable
final class Regex implements Type, \Serializable, \JsonSerializable
{
private $pattern;
private $flags;
Expand All @@ -1250,6 +1291,14 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
return [
'$regex' => $this->pattern,
'$options' => $this->flags,
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down Expand Up @@ -1302,7 +1351,7 @@ public function __debugInfo() : array
}
}

final class Timestamp implements Type, \Serializable
final class Timestamp implements Type, \Serializable, \JsonSerializable
{
static private function checkArray( array $state )
{
Expand Down Expand Up @@ -1338,6 +1387,16 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
return [
'$timestamp' => [
't' => (int) $this->timestamp,
'i' => (int) $this->increment,
]
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down Expand Up @@ -1389,7 +1448,7 @@ public function __debugInfo() : array
}
}

final class UTCDateTime implements Type, \Serializable
final class UTCDateTime implements Type, \Serializable, \JsonSerializable
{
private string $milliseconds;

Expand All @@ -1410,6 +1469,18 @@ public function serialize() : string
] );
}

public function jsonSerialize() : mixed
{
$seconds = (int) ( $this->milliseconds / 1000 );
$millis = (int) ( $this->milliseconds % 1000 );

$d = date_create( "@" . (string) $seconds );

return [
'$date' => sprintf( "%s.%03d+0000", $d->format( 'Y-m-d\TH:i:s' ), $millis )
];
}

public function unserialize(mixed $serialized) : void
{
$unserialized = unserialize( $serialized );
Expand Down
26 changes: 26 additions & 0 deletions tests/json-serialize-binary.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
JsonSerializable: Binary
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\Binary( "gargleblaster", 24 )
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$binary" : "Z2FyZ2xlYmxhc3Rlcg==", "$type" : "18" } }
{"foo":{"$binary":"Z2FyZ2xlYmxhc3Rlcg==","$type":"18"}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that the type is in hexadecimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json_encode version doesn't have spaces for padding formatting, so unfortunately we can't do a straight string-for-string comparision

object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\Binary)#%d (%d) {
["data"]=>
string(13) "gargleblaster"
["type"]=>
int(24)
}
}
24 changes: 24 additions & 0 deletions tests/json-serialize-decimal128.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
JsonSerializable: Decimal128
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\Decimal128( "12389719287312" )
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$numberDecimal" : "12389719287312" } }
{"foo":{"$numberDecimal":"12389719287312"}}
object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\Decimal128)#%d (%d) {
["dec"]=>
string(14) "12389719287312"
}
}
21 changes: 21 additions & 0 deletions tests/json-serialize-javascript.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
JsonSerializable: Javascript
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\Javascript( "function foo(bar) { return bar; }", [ 'foo' => 42 ] )
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : "function foo(bar) { return bar; }" }
{"foo":"function foo(bar) { return bar; }"}
object(stdClass)#%d (%d) {
["foo"]=>
string(33) "function foo(bar) { return bar; }"
}
22 changes: 22 additions & 0 deletions tests/json-serialize-maxkey.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
JsonSerializable: MaxKey
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\MaxKey()
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$maxKey" : 1 } }
{"foo":{"$maxKey":1}}
object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\MaxKey)#%d (%d) {
}
}
22 changes: 22 additions & 0 deletions tests/json-serialize-minkey.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
JsonSerializable: MinKey
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\MinKey()
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$minKey" : 1 } }
{"foo":{"$minKey":1}}
object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\MinKey)#%d (%d) {
}
}
24 changes: 24 additions & 0 deletions tests/json-serialize-objectid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
JsonSerializable: ObjectID
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\ObjectID()
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$oid" : "%s" } }
{"foo":{"$oid":"%s"}}
object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\ObjectID)#%d (%d) {
["oid"]=>
string(24) "%s"
}
}
26 changes: 26 additions & 0 deletions tests/json-serialize-regex-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
JsonSerializable: Regex
--FILE--
<?php
$doc = [
'foo' => new MongoDB\BSON\Regex( "/foo/", "i" )
];

echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
$d = json_encode( $doc );
echo $d, "\n";

var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
?>
--EXPECTF--
{ "foo" : { "$regex" : "/foo/", "$options" : "i" } }
{"foo":{"$regex":"\/foo\/","$options":"i"}}
Copy link
Contributor Author

@derickr derickr Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP's json_encode seems to add \/ for /, unlike extended JSON.

object(stdClass)#%d (%d) {
["foo"]=>
object(MongoDB\BSON\Regex)#%d (%d) {
["pattern"]=>
string(%d) "/foo/"
["flags"]=>
string(%d) "i"
}
}
Loading