当我的类扩展了MongoDB类com.mongodb.BasicDBObject时,我一直在使用Gson.toJson()方法生成一个包含所有成员的字段名和值的json字符串。以下是代码段:
import com.mongodb.BasicDBObject;
...
public class TestMongo extends BasicDBObject {
protected String objectId;
protected String userId;
public TestMongo(String oid, String uid) {
this.objectId = oid;
this.userId = uid;
}
/* getters and setters... */
}
主要方法测试输出:
TestMongo test = new TestMongo("sample object id","sample user id");
System.out.println((new Gson()).toJson(test));
控制台输出:
{}
有没有人知道是否有办法从扩展BasicDBObject的对象类中获取json字符串?