java 的 BSON 库?

发布于 2024-09-17 19:33:51 字数 1539 浏览 2 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南汐寒笙箫 2024-09-24 19:33:51

您可以使用适用于 Java 的 MongoDB 驱动程序来存储 BSON 对象,然后将其转换为 String,然后您可以用 JSONObject

例如,以下是我创建常规文档的方法:

BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());

然后,要获取对象的 String 表示形式,只需调用:

String bsonString = obj.toString();

JSONObject 包装它并获取日期属性,它应该以 BSON 兼容的格式返回它。

JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));

结果输出类似于:

{"$date":"2012-08-10T05:22:53.872Z"}

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.

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 bsonString = obj.toString();

Wrap it with a JSONObject and get the date attribute, which should return it in a BSON-compliant format.

JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));

The resulting output is something like:

{"$date":"2012-08-10T05:22:53.872Z"}
仅冇旳回忆 2024-09-24 19:33:51

BSON 站点 指向 这个

如果你想从 MongoDB 使用它,请查看 此示例

The BSON site is pointing at this

If you want to use it from MongoDB, take a look at this example

-残月青衣踏尘吟 2024-09-24 19:33:51

为了在 MongoDB 中获取模型,我们首先使用 google gson 将模型转换为 JSON,然后使用 来自 MongoDB 的 JSON util 解析方法 将我们生成的 JSON 字符串解析为 DBObject,您可以将其放入 MongoDB 中。说实话,我不知道性能如何。

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 MongoDB to parse our generated JSON string to a DBObject which you can put in your MongoDB. I don't know about performance to be honest.

暮凉 2024-09-24 19:33:51

还有一个相当新的 BSON4Jackson 项目,它允许使用 Jackson 用于处理 BSON 数据。这意味着完整的数据绑定(到/从 POJO)、树模型,甚至流式(增量)读/写都可以使用 BSON 格式完成。

There is also a rather new BSON4Jackson project, which allows one to use Jackson for 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.

随风而去 2024-09-24 19:33:51

还有 ebson。我没试过...

There is also ebson. I've not tried it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文