0

com.mongodb.DBObjectこんにちは、BSONバイナリへのシリアル化/逆シリアル化、およびその逆を支援する Java ライブラリを知っている人はいますか?

4

2 に答える 2

2

それは非常に簡単です。次のヘルパー メソッドを使用できます。

public static byte[] encode(BSONObject bsonObject) {
    BSONEncoder encoder = new BasicBSONEncoder();
    return encoder.encode(bsonObject);
}

public static BSONObject readObject(InputStream is) throws IOException {
    BasicBSONDecoder encoder = new BasicBSONDecoder();
    return encoder.readObject(is);
}

public static BSONObject readObject(byte[] bsonObject) {
    BasicBSONDecoder encoder = new BasicBSONDecoder();
    return encoder.readObject(bsonObject);
}
于 2014-10-06T09:12:49.337 に答える
1

バイナリ BSON、つまり BSON 形式のバイト配列が必要な場合は、次のペアを使用できます。

public byte[] DBObjectToBSON(DBObject dbObject) {
    BasicBSONEncoder encoder = new BasicBSONEncoder();
    return encoder.encode(dbObject);
}

public DBObject BSONToDBObject(byte[] bson) {
    BasicBSONDecoder decoder = new BasicBSONDecoder();
    JSONCallback callback = new JSONCallback();
    decoder.decode(bson, callback);
    return (DBObject) callback.get();
}
于 2015-06-23T10:01:36.980 に答える