Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-974

Converting JSON to BSON to PHP introduces gaps in array indexes

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.3.0-beta1, 1.3.0
    • Affects Version/s: 1.2.9
    • Component/s: None
    • Labels:
      None

      As reported in mongodb/mongo-php-driver#603:

      <?php
      
      var_dump(\MongoDB\BSON\toPHP(\MongoDB\BSON\fromJson('{"myArray": [{"$oid": "2017-06-13T11:21:26.906Z"}]}')));
      
      echo "\n";
      
      var_dump(\MongoDB\BSON\toPHP(\MongoDB\BSON\fromJson('[1,2,3,{"$date": "2017-06-13T11:21:26.906Z"}]')));
      
      echo "\n";
      
      var_dump(\MongoDB\BSON\toPHP(\MongoDB\BSON\fromJson('[{"$oid": "55f2b3f1f657b3fa97c9c0a2"},{"$date": "2017-06-13T11:21:26.906Z"}]')));
      

      This produces the following output:

      object(stdClass)#2 (1) {
        ["myArray"]=>
        array(1) {
          [0]=>
          object(MongoDB\BSON\ObjectID)#1 (1) {
            ["oid"]=>
            string(24) "201700601301102102609060"
          }
        }
      }
      
      object(stdClass)#1 (4) {
        [0]=>
        int(1)
        [1]=>
        int(2)
        [2]=>
        int(3)
        [4]=>
        object(MongoDB\BSON\UTCDateTime)#2 (1) {
          ["milliseconds"]=>
          string(13) "1497352886906"
        }
      }
      
      object(stdClass)#3 (2) {
        [1]=>
        object(MongoDB\BSON\ObjectID)#1 (1) {
          ["oid"]=>
          string(24) "55f2b3f1f657b3fa97c9c0a2"
        }
        [3]=>
        object(MongoDB\BSON\UTCDateTime)#2 (1) {
          ["milliseconds"]=>
          string(13) "1497352886906"
        }
      }
      

      In the second and third examples, conversion introduces gaps in the array indexes. While unrelated to PHPC, the HHVM driver also demonstrates a gap in the first example (see GitHub issue).

            Assignee:
            jmikola@mongodb.com Jeremy Mikola
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: