简体   繁体   中英

Convert Redis ByteString to BasicDBObject

I need to convert serialized Java Object (get from Redis) to a Java model that extends com.mongodb.BasicDBObject (mongo-java-driver).

class Member extends BasicDBObject {
    public Member(DBObject doc) {
        super(doc.toMap());
    }
    public Member(BasicDBObject doc) {
        super(doc);
    }
    ...
}

What I got from Redis is in ByteString form.

In summary, I need to convert ByteString -> BasicDBObject

You should use an intermediate model to persist your model. MongoDB's BasicDBObject s base on LinkedHashMap , but there is a good chance, that you end up with internal data structures that are not Java-serializable.

Neither DBObject nor BSONObject extend Serializable . I propose that you store JSON within Redis. A shortcut could be storing BSON instead of JSON within Redis (see BasicBSONEncoder#encode and BasicBSONDecoder#readObject ).

@mp911de You are right. The good thing is that DBObject and BSONObject do not extend Serializable. In other words, there is no need to write a deserializer for DBObject/BSONObject. I found a solution:

ByteString from Redis -> byte array -> Object -> cast to my class Member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM