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?
PHPC-2350
We can leverage
bson_new_from_json
to create abson_t
for an array, andbson_as_json_with_opts
lets us export thebson_t
to JSON with an array being the root object.Note that since
bson_new_from_json
was intended to read documents, the resultingPackedArray
instance will retain the original keys in the object, but iterating it or converting it to JSON will reindex the array. However, when using thePackedArray
instance in certain ways will cause errors (seebson-packedarray-fromJSON-003.phpt
which fails the last operation). The only option I see is to iterate the resultingbson_t
and either re-index it (essentially re-writing abson_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?