Swiftpack.co -  OpenKitten/BSON as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
OpenKitten/BSON
Native Swift library for BSON (http://bsonspec.org)
.package(url: "https://github.com/OpenKitten/BSON.git", from: "7.0.27")

BSON

Swift 4.2 Swift 5.3 License Build Status

BSON 7 is a fast BSON library. It's compliant to the whole BSON specification test suite. The library parses the binary data on-demand, delaying copies until the last second.

BSON is parsed and generated as specified for version 1.1 of the BSON specification.

Be sure to read our full documentation.

Installation

BSON uses the Swift Package Manager. Add BSON to your dependencies in your Package.swift file:

.package(url: "https://github.com/OpenKitten/BSON.git", from: "7.0.0")

Also, don't forget to add "BSON" as a dependency for your target.

Basic Usage

Create Documents using Dictionary Literals:

var userDocument: Document = [
	"username": "Joannis",
	"online": true,
	"age": 20,
	"pi_constant": 3.14,
	"profile": [
		"firstName": "Joannis",
		"lastName": "Orlandos"
	]
]

let favouriteNumbers: Document = [1, 3, 7, 14, 21, 24, 34]

userDocument["favouriteNumbers"] = favouriteNumbers

Access values in an array like you would in Swift Arrays and values in an object like a Dictionary.

let favouriteNumber = favouriteNumbers[0]
let usernameValue = userDocument["username"]

Extract types with simplicity:

let username = String(userDocument["username"]) // "Joannis"
let isOnline = Bool(userDocument["online"]) // true
let age = Int(userDocument["age"]) // 20
let pi = Double(userDocument["pi_constant"]) // 3.14

Chain subscripts easily to find results without a hassle as shown underneath using this JSON structure (assuming this is represented in BSON):

{
  "users": [
  	{
  		"username": "Joannis",
  		"profile": {
  		  "firstName": "Joannis",
  		  "lastName": "Orlandos"
  		}
  	},
  	{
  		"username": "Obbut",
  		"profile": {
  		  "firstName": "Robbert",
  		  "lastName": "Brandsma"
  		}
  	}
  ]
}
let obbutLastName = String(object["users"][1]["profile"]["lastName"]) // "Brandsma"

Nested Documents

Complex array and dictionary literals may confuse the Swift type system. If this happens to you, make the literal explicitly a Document type:

var userDocument: Document = [
	"username": "Joannis",
	"online": true,
	"age": 20,
	"pi_constant": 3.14,
	"profile": [
		"firstName": "Joannis",
		"lastName": "Orlandos",
		"pets": [
			[
				"name": "Noodles",
				"type": "Parrot"
			] as Document,
			[
				"name": "Witje",
				"type": "Rabbit"
			]
		] as Document
	] as Document
]

Codable

Document can be instantiated from SwiftNIO's ByteBuffer or Foundation.Data. You can validate the formatting of this document manually using the .validate() function. This will also specify where the data was found corrupt.

If you pass a Document or Primitive into the BSONDecoder you can decode any Decodable type if the formats match. Likewise, BSONEncoder can encode your Swift types into a Document.

Supported Types

All non-deprecated BSON 1.1 types are supported.

  • Double
  • String
  • Document
  • Array
  • ObjectId
  • Bool
  • DateTime
  • 32-bit integer
  • 64-bit integer
  • Null value
  • Binary
  • Regular Expression
  • Min Key
  • Max Key
  • Timestamp
  • Javascript Code
  • Javascript Code with Scope
  • Decimal128

GitHub

link
Stars: 88
Last commit: 1 hour ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

7.0.25
47 weeks ago

Fixed a bug that would incorrectly set the writerIndex of the NIO.ByteBuffer when inine-editing an array typed subdocument. This does not affect code based on Codable.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API