BSON Serial Converter / Deserializer

Is there a BSON serializer / deserializer library for PHP or Java?

+4
source share
6 answers

You can check MongoDB drivers for these languages ​​since MongoDB uses BSON. See what they use, or steal their implementation.

+1
source

Another feature is the BSON4Jackson extension for Jackson , which adds BSON read / write support.

+3
source

check out this link http://php.net/manual/en/ref.mongo.php

bson_decode - Deserializes a BSON object into a PHP array

bson_encode - Serialize a PHP variable to a BSON string

+1
source

Not for Java, but here for Obj-C: https://github.com/martinkou/bson-objc/

0
source

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.

0
source

And here is the C ++ 11 JSON encoder and decoder that I made using Rapidjson, because the native JSON encoder ( BSONObj::jsonString ) uses non-standard encoding for longs: https://gist.github.com/ArtemGr/2c44cb451dc6a0cb46af

In addition, unlike its own JSON encoder, it has no problems decoding top-level arrays.

0
source

All Articles