简体   繁体   English

com.mongodb.DBObject / Java的BSON序列化器

[英]com.mongodb.DBObject / BSON serializer for java

嗨,有人知道Java库可帮助将com.mongodb.DBObject序列化/反序列化为BSON二进制文件,反之亦然?

It's fairly simple, you can use the following helper methods: 这很简单,您可以使用以下辅助方法:

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);
}

When you need binary BSON, ie, byte array in BSON format, you may use the following pair: 当需要二进制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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:org.bson.types.ObjectId无法转换为com.mongodb.DBObject - java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject 为什么会出现此异常 - java.lang.ClassCastException:java.lang.String无法强制转换为com.mongodb.DBObject - Why has this exception started occurring - java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject Morphia java.util.Arrays$ArrayList 在进行 Projection.projection 时无法转换为 com.mongodb.DBObject - Morphia java.util.Arrays$ArrayList cannot be cast to com.mongodb.DBObject when making a Projection.projection bson无法转换为DBObject - bson cannot be converted to DBObject 使用MongoDB Java将DBObject转换为POJO - Converting DBObject to a POJO using MongoDB Java 使用MongoDB Java驱动程序将DBObject转换为POJO - Convert DBObject to a POJO using MongoDB Java Driver 使用 MongoDB 和 Java 时,文档和 dbObject 之间有什么区别? - what are the difference between document and dbObject when using MongoDB and Java? 如何从Java,MongoDB中的dbobject获取属性中的aa属性 - How to get a a property in a property from a dbobject in java, mongodb 从MongoDB检索值时将DBObject转换为Java对象 - Converting DBObject to Java Object while retrieve values from MongoDB MongoDB Java:如何在DBObject中表示$ in查询? - MongoDB Java: How can I represent $in query in DBObject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM