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

Conversation

derickr
Copy link
Contributor

@derickr derickr commented Oct 11, 2016

No description provided.

?>
--EXPECT--
{ "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.

?>
--EXPECT--
{ "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

Copy link
Member

@jmikola jmikola left a 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
Copy link
Member

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;
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

return [
'timestamp' => [
't' => $this->timestamp,
'i' => $this->increment,
Copy link
Member

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' => [
Copy link
Member

Choose a reason for hiding this comment

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

$timestamp.

@derickr derickr force-pushed the HHVM-158-jsonserialize branch from 20949b2 to 49fe7b5 Compare October 11, 2016 14:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants