Skip to content

orlandos-nl/BSON

Folders and files

NameName
Last commit message
Last commit date

Latest commit

507f6cb · Jun 14, 2017
Mar 15, 2017
Jun 14, 2017
Jun 14, 2017
Nov 2, 2016
May 17, 2016
Nov 2, 2016
Mar 29, 2017
Mar 29, 2017
Apr 14, 2016
Mar 20, 2017
Mar 20, 2017
Apr 23, 2017
Feb 6, 2017

Repository files navigation

BSON 5.0

This branch contains working code for BSON 5.0. Nothing here is final and everything may change. BSON 5.0 does not have a release schedule yet, but will be released alongside MongoKitten 4.0.

Goals

  • Better extraction API
  • Move ExtendedJSON to MongoKitten
  • Do not use Foundation RegularExpression
  • Chained subscripts
  • Generally a more simple and logical API
  • Better unit tests

BSON

Swift 3.1 License Build Status

A native, fast BSON library for Swift, written in Swift.

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.

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