java的BSON库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3656335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BSON library for java?
提问by Maxim Veksler
We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.htmlbut what about BSON.
我们在 java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html 中对 JSON 有很好的支持,但是 BSON 呢。
What library do you know that provides BSON support for java? It should obviously be efficient in runtime.
您知道哪个库为 Java 提供 BSON 支持?它在运行时显然应该是高效的。
采纳答案by Bozho
回答by Jeroen Rosenberg
In order to get our Model in MongoDB we used google gson to convert our model into JSON first and then we used the JSON util parse method from MongoDBto parse our generated JSON string to a DBObject which you can put in your MongoDB. I don't know about performance to be honest.
为了在 MongoDB 中获取我们的模型,我们首先使用 google gson 将我们的模型转换为 JSON,然后我们使用来自 MongoDB的JSON util parse 方法将我们生成的 JSON 字符串解析为可以放入 MongoDB 的 DBObject。老实说,我不知道性能。
回答by StaxMan
There is also a rather new BSON4Hymansonproject, which allows one to use Hymansonfor handling BSON data. This means full data binding (to/from POJOs), tree model, even streaming (incremental) reading/writing to degree it can be done with BSON format.
还有一个相当新的BSON4Hymanson项目,它允许使用Hymanson处理 BSON 数据。这意味着完整的数据绑定(到/从 POJO)、树模型,甚至流(增量)读/写都可以用 BSON 格式完成。
回答by Matthew Quiros
You can use the MongoDB driver for Java to store a BSON object, then convert that to a String
which you can then wrap with JSONObject
.
您可以使用 Java 的 MongoDB 驱动程序来存储 BSON 对象,然后将其转换为 a String
,然后您可以用JSONObject
.
For example, here's how I'll create a regular document:
例如,下面是我将如何创建一个常规文档:
BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());
Then, to get a String
representation of the object, simply call:
然后,要获得String
对象的表示,只需调用:
String bsonString = obj.toString();
Wrap it with a JSONObject
and get the date attribute, which should return it in a BSON-compliant format.
用 a 包裹它JSONObject
并获取日期属性,它应该以符合 BSON 的格式返回它。
JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));
The resulting output is something like:
结果输出类似于:
{"$date":"2012-08-10T05:22:53.872Z"}