Swiftpack.co - Package - OpenKitten/BSON

BSON

Swift 3.1 License Build Status

BSON 5 is the fastest BSON library speeding past all libraries including C. It's compliant to the whole C BSON specification test suite, and even passes official libraries in compliance slightly.

It's not only fast, it's also got an extremely easy and intuitive API for extraction.

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

Usage

The supported method for using this library is trough the Swift Package manager, like this:

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 5)]
)

Create Documents naturally:

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"

Check the documentation for more information.

Performance

The performance in this BSON library is thanks to specialized algorithms per-operation with a centralized cache for indexed information. BSON is 100% lazy, so if you don't read data, it doesn't cost any performance.

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: 58
Help us keep the lights on

Dependencies

Used By

Total:

Releases

5.2.4 - Jul 3, 2018

  • Fixed UInt decoding

5.2.3 - Oct 18, 2017

  • Improved caching the count

5.2.2 - Oct 17, 2017

  • Fixed performance issues for count operations

BSON ended up re-indexing documents when the count was being read

  • Fixed BSONEncoder refusing the max and min value for an integer

5.2.1 - Oct 4, 2017

  • Fixed a very obscure bug that would sometimes cause misinterpreting "primitive" BSON values (like Double, Int) when the backing storage of the array containing the bytes of the Document is not contiguous
  • Added a range check in the Int initializer, to prevent crashes when converting a Double whose value falls outside the Int.min-Int.max range

5.2.0 - Sep 24, 2017

Swift 4 codable support. Swift 3.0 and 3.1 are still supported, too, but without Codable.