Swift 的 BSON 解析器

BSON Parser for Swift

我正在编写一个应用程序(在 swift 5 for iOS 中)读取来自远程 mqtt 代理的一些 mqtt 消息。消息格式为 BSON(二进制 Json)。是否有任何库或代码允许我以这种格式阅读消息?使用 Swift 的标准库,我可以读取和解码普通的 JSON 文件,但不能读取 BSON。有什么建议吗?

找到这个库:https://github.com/OpenKitten/BSON 在 Xcode 中安装包后(使用数据包管理器)我能够读取从 Mqtt broker

收到的 BSON 文件

这里是我使用的代码:

//At the beginning of the file
import BSON

...
...

//I use BSON Document object to read the payload of the message
let info = Document(bytes: message.payload)
//I use the function isValid to check if the format is correct
if info.validate().isValid{
   //If the format is correct I can read a field of the Json (in the example "MessageType")
   let messageType:String = info["MessageType"] as? String ?? ""
   ...
   ...