How can I create a byte array from BSONDocument in 1.0.4

388 views
Skip to first unread message

Valtteri Pirttilä

unread,
Jun 29, 2021, 7:05:45 AM6/29/21
to ReactiveMongo - http://reactivemongo.org
Hi!

I'm in the process of updating a project from 0.20.13 to 1.0.4. Reactivemongo bson library has been so nice to use that we've been using it for quite some time in communication with some other services. We create akka.util.ByteString objects that are passed over the network.

In 0.20 it was really easy to make a ByteString out of a BSONDocument with:

val buffer = new ArrayBSONBuffer()
BSONDocument.write(doc, buffer)
ByteString.fromArray(buffer.array)

And again read an incoming ByteString bytes to a BSONDocument doc:

val buffer = new ArrayBSONBuffer()
val doc = BSONDocument.read(buffer.writeBytes(bytes.toArray).toReadableBuffer())

In 1.0.4 the ArrayBSONBuffer has been removed and that makes it difficult to upgrade. I've been trying to find an alternate way to create the ByteString from a BSONDocument, but so far I'm drawing blank.

While it's not the responsibility of a Mongo driver library to provide this functionality, is anyone aware of an alternative I could use with 1.0.4?

Respectfully,
Valtteri

Cédric Chantepie

unread,
Jun 29, 2021, 4:34:25 PM6/29/21
to ReactiveMongo - http://reactivemongo.org
The binary serialization is no longer part of the public API, and there is no current plan to restore it (as considered internal, and can vary as soon as the BSON API itself stays stable).
If you want to passed BSON over network, rather do as with JSON, and use the text representation (using using BSON/JSON compat).
Reply all
Reply to author
Forward
0 new messages