BSONSerialization

2.2.0

BSON Serialization in Swift
Frizlab/BSONSerialization

What's New

BSONSerialization 2.2

2024-02-01T18:25:30Z

Add support for two new binary subtypes.

BSONSerialization

Installation & Compatibility

The recommended (and only tested) way to install and use BSONSerialization is via SwiftPM, using at least Swift 5.1.

The content of your Package.swift should be something resembling:

import PackageDescription

let package = Package(
	name: "MyAwesomePackage",
	dependencies: [.package(url: "https://github.com/Frizlab/BSONSerialization.git", from: "2.0.0")]
)

Usage

BSONSerialization has the same basic interface than JSONSerialization.

Example of use:

let myFirstBSONDoc = ["key": "value"]
let serializedBSONDoc = try BSONSerialization.data(withBSONObject: myFirstBSONDoc, options: [])
let unserializedBSONDoc = try BSONSerialization.bsonObject(with: serializedBSONDoc, options: [])
try areBSONDocEqual(myFirstBSONDoc, unserializedBSONDoc) /* Returns true */

Serializing/deserializing to/from a stream is also supported. (Note: Due to the specifications of the BSON format, serializing to a Data object is faster than serializing to a stream.)

Finally, a method lets you know if a given dictionary can be serialized as a BSON document.

Alternatives

First, a word about the philosophy of this project. Most BSON frameworks create a whole new type to represent the BSON document. This is useful to add new elements to the documents and have the document serialization ready at all times.

This project takes a different approach. It is merely a converter from the serialized BSON data to actual Foundation or native objects, or the reverse. It is actually just like Foundation’s JSONSerialization which is a converter from Foundation objects to the serialized JSON data.

If you prefer having an actual BSON object to which you can add elements instead of standard Foundation object which are serialized later, I would recommend this project: https://github.com/OpenKitten/BSON

Reference

I used the BSON specification version 1.1 from http://bsonspec.org/spec.html

All types in this specification, including deprecated ones are supported.

To Do

  • ❏ Allow direct serialization of Data object instead of having to use the MongoBinary type

  • ❏ Allow serializing Int64 or Int32 to Int directly, depending on the platform

  • ❏ Xcode target for an iOS Framework

I’ll work seriously on the project if it gains enough attention. Feel free to open issues, I’ll do my best to answer.

Pull requests are welcome 😉

License

MIT (see License.txt file)

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

Last updated: Sat Nov 02 2024 19:43:57 GMT-0900 (Hawaii-Aleutian Daylight Time)