Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orlandos-nl/bson
Native Swift library for BSON (http://bsonspec.org)
https://github.com/orlandos-nl/bson
bson hacktoberfest mongodb server-side-swift swift swift-nio
Last synced: 4 days ago
JSON representation
Native Swift library for BSON (http://bsonspec.org)
- Host: GitHub
- URL: https://github.com/orlandos-nl/bson
- Owner: orlandos-nl
- License: mit
- Created: 2016-01-23T10:56:07.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-09-10T12:41:16.000Z (5 months ago)
- Last Synced: 2025-01-20T04:18:14.134Z (13 days ago)
- Topics: bson, hacktoberfest, mongodb, server-side-swift, swift, swift-nio
- Language: Swift
- Homepage: https://orlandos.nl/docs/mongokitten/articles/bson
- Size: 1.4 MB
- Stars: 110
- Watchers: 10
- Forks: 35
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# BSON
A fast BSON library, 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](http://bsonspec.org/spec.html).
Be sure to read our [full documentation](https://orlandos.nl/docs/mongokitten/articles/bson) and [API reference](https://swiftinit.org/reference/bson).
## Installation
BSON uses the Swift Package Manager. Add BSON to your dependencies in your Package.swift file:
```swift
.package(url: "https://github.com/orlandos-nl/BSON.git", from: "8.0.0")
```Also, don't forget to add "BSON" as a dependency for your target.
## Basic Usage
Create Documents using Dictionary Literals:
```swift
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.
```swift
let favouriteNumber = favouriteNumbers[0]
let usernameValue = userDocument["username"]
```Extract types with simplicity:
```swift
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):
```json
{
"users": [
{
"username": "Joannis",
"profile": {
"firstName": "Joannis",
"lastName": "Orlandos"
}
},
{
"username": "Obbut",
"profile": {
"firstName": "Robbert",
"lastName": "Brandsma"
}
}
]
}
``````swift
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:
```swift
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](https://github.com/apple/swift-nio)'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`.