-
Notifications
You must be signed in to change notification settings - Fork 13
HHVM-158: BSON objects should implement JsonSerializable #148
Conversation
?> | ||
--EXPECT-- | ||
{ "foo" : { "$regex" : "/foo/", "$options" : "i" } } | ||
{"foo":{"$regex":"\/foo\/","$options":"i"}} |
There was a problem hiding this comment.
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.
?> | ||
--EXPECT-- | ||
{ "foo" : { "$binary" : "Z2FyZ2xlYmxhc3Rlcg==", "$type" : "18" } } | ||
{"foo":{"$binary":"Z2FyZ2xlYmxhc3Rlcg==","$type":"18"}} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some requested changes.
public function jsonSerialize() : mixed | ||
{ | ||
return [ | ||
'$date' => (int) $this->milliseconds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In both strict mode and shell mode, the value should be a string. I believe we want to use an ISO-8601 string for strict mode here.
See: https://docs.mongodb.com/v3.2/reference/mongodb-extended-json/#data_date
@@ -1084,6 +1099,11 @@ public function serialize() : string | |||
return serialize( $s ); | |||
} | |||
|
|||
public function jsonSerialize() : mixed | |||
{ | |||
return $this->code; |
There was a problem hiding this comment.
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
return [ | ||
'timestamp' => [ | ||
't' => $this->timestamp, | ||
'i' => $this->increment, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be integers according to https://docs.mongodb.com/v3.2/reference/mongodb-extended-json/#timestamp.
According to http://stackoverflow.com/a/13502497/162228, the JSON spec itself does not have a limit on integer sizes. That said, I'm not sure how we'll support this on 32-bit PHP platforms.
public function jsonSerialize() : mixed | ||
{ | ||
return [ | ||
'timestamp' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$timestamp
.
20949b2
to
49fe7b5
Compare
No description provided.