BSON 串行器/解串器

发布于 2024-09-24 05:29:34 字数 44 浏览 5 评论 0 原文

是否有适用于 PHP 或 Java 的 BSON 序列化器/反序列化器库?

Is there a BSON serializer/deserializer library out there for PHP or Java?

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

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

发布评论

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

评论(5

星軌x 2024-10-01 05:29:34

另一种可能性是 BSON4Jackson 扩展rel="nofollow">Jackson,增加了对 BSON 读/写的支持。

Another possibility is BSON4Jackson extension for Jackson, which adds support for BSON reading/writing.

云淡月浅 2024-10-01 05:29:34

Java 中的 BSON 编码器/解码器非常简单。以下代码片段来自我的应用程序,因此位于 Scala 中。我相信您可以轻松地从它构建 Java 实现。

import org.bson.BSON
import com.mongodb.{DBObject, DBDecoder, DefaultDBDecoder}

def convert(dbo: DBObject): Array[Byte] =
  BSON.encode(dbo)

// NB! this is a stateful object and thus it's not thread-safe, so have
// to create one per decoding
def decoder: DBDecoder = DefaultDBDecoder.FACTORY.create

def convert(data: Array[Byte]): DBObject =
  // NOTE: we do not support Ref in input, that's why "null" for DBCollection
  decoder.decode(data, null)

def convert(is: InputStream): DBObject =
  // NOTE: we do not support Ref in input, that's why "null" for DBCollection
  decoder.decode(is, null)

唯一值得注意的是,DBEncoder 实例有一个在解码期间(重新)使用的内部状态,因此它不是线程安全的。如果您逐一解码对象应该没问题,但否则您最好为每个解码会话创建一个实例。

BSON encoder/decoder in Java is pretty trivial. The following snippet of code is from my app, so it's in Scala. I am sure you could build a Java implementation from it easily.

import org.bson.BSON
import com.mongodb.{DBObject, DBDecoder, DefaultDBDecoder}

def convert(dbo: DBObject): Array[Byte] =
  BSON.encode(dbo)

// NB! this is a stateful object and thus it's not thread-safe, so have
// to create one per decoding
def decoder: DBDecoder = DefaultDBDecoder.FACTORY.create

def convert(data: Array[Byte]): DBObject =
  // NOTE: we do not support Ref in input, that's why "null" for DBCollection
  decoder.decode(data, null)

def convert(is: InputStream): DBObject =
  // NOTE: we do not support Ref in input, that's why "null" for DBCollection
  decoder.decode(is, null)

The only significant note is that DBEncoder instance has an internal state it (re)uses during decoding, so it's not thread-safe. It should be ok if you decode objects one by one, but otherwise you'd better create an instance per decoding session.

知你几分 2024-10-01 05:29:34

检查此链接
http://php.net/manual/en/ref.mongo.php

bson_decode — 将 BSON 对象反序列化为 PHP 数组

bson_encode — 将 PHP 变量序列化为 BSON 字符串

check this link
http://php.net/manual/en/ref.mongo.php

bson_decode — Deserializes a BSON object into a PHP array

bson_encode — Serializes a PHP variable into a BSON string

我的痛♀有谁懂 2024-10-01 05:29:34

您可以检查这些语言的 MongoDB 驱动程序,因为 MongoDB 使用 BSON。看看他们使用什么,或者窃取他们的实现。

You might check the MongoDB drivers for those languages, since MongoDB uses BSON. See what they use, or steal their implementation.

蓬勃野心 2024-10-01 05:29:34

这是我使用 Rapidjson 制作的 C++11 JSON 编码器和解码器,因为本机 JSON 编码器 (BSONObj::jsonString) 使用长整型的非标准编码:https://gist.github.com/ArtemGr/2c44cb451dc6a0cb46af

另外,与本机 JSON 编码器不同,这个编码器不解码顶级数组时遇到问题。

And here is a C++11 JSON encoder and decoder I've made using Rapidjson, because the native JSON encoder (BSONObj::jsonString) uses a non-standard encoding for longs: https://gist.github.com/ArtemGr/2c44cb451dc6a0cb46af

Also, unlike the native JSON encoder, this one doesn't have a problem decoding top-level arrays.

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