mongodb / mongo-php-driver

The Official MongoDB PHP driver
https://pecl.php.net/package/mongodb
Apache License 2.0
886 stars 201 forks source link

PHPC-2350: Implement JSON methods for PackedArray #1645

Closed alcaeus closed 2 weeks ago

alcaeus commented 3 weeks ago

PHPC-2350

We can leverage bson_new_from_json to create a bson_t for an array, and bson_as_json_with_opts lets us export the bson_t to JSON with an array being the root object.

Note that since bson_new_from_json was intended to read documents, the resulting PackedArray instance will retain the original keys in the object, but iterating it or converting it to JSON will reindex the array. However, when using the PackedArray instance in certain ways will cause errors (see bson-packedarray-fromJSON-003.phpt which fails the last operation). The only option I see is to iterate the resulting bson_t and either re-index it (essentially re-writing a bson_t array) or to throw if one of the keys isn't what we expect. Since we have the mantra of "be lenient in what you accept, but strict in what you produce" I'm inclined to go with the first option, despite this potentially causing some overhead. WDYT @jmikola?