The BSON encoder / decoder in Java is pretty trivial. The following code snippet is from my application, so it is in Scala. I'm sure you could easily implement a Java implementation.
import org.bson.BSON import com.mongodb.{DBObject, DBDecoder, DefaultDBDecoder} def convert(dbo: DBObject): Array[Byte] = BSON.encode(dbo) // NB! this is a stateful object and thus it not thread-safe, so have // to create one per decoding def decoder: DBDecoder = DefaultDBDecoder.FACTORY.create def convert(data: Array[Byte]): DBObject = // NOTE: we do not support Ref in input, that why "null" for DBCollection decoder.decode(data, null) def convert(is: InputStream): DBObject = // NOTE: we do not support Ref in input, that why "null" for DBCollection decoder.decode(is, null)
The only remarkable note: the DBEncoder instance has an internal state that it uses during decoding, so it is not thread safe. This should be fine if you decode objects one by one, but otherwise you would be better off creating an instance for the decoding session.
source share