Skip to content

BSON Encoding Discrepancy Compared with Old Driver #256

@mtdavidson

Description

@mtdavidson

Hi

I am currently in progress of migration our code base from using the old driver to using the new driver and have stumbled across an issue with the BSON encoding between the two. This is in regards to the length of the keys within an array I think.

Here is the simplest example I've been able to produce to recreate the issue.

Using the old implementation

$data = [
    '9781449410247' => 'a',
    'X9781449410247' => 'b',
    9781449410248 => 'c',
];

echo json_encode(
        bson_decode(
            bson_encode($data)
        )
    );

this results in

{
    "9781449410247":"a",
    "X9781449410247":"b",
    "9781449410248":"c"
}

which is correct and how I would expect it to look.

Now with the new method

$bson = MongoDB\BSON\fromPHP($data);
$json = MongoDB\BSON\toJSON($bson);

echo $json;

this results in

{ 
   "1808877255" : "a", 
   "X9781449410247" : "b", 
   "1808877256" : "c"
}

as you can see the first and third values seem aren't coming out as expected. This is consistent in its behaviour I can only assume its something to do with key length or casting but am really not sure.

If its not a bug I'd really appreciate if someone could explain the reason why.

Thanks

Mark

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions