https://github.com/nanoxd/bourne
A JSON parser for backends with variant types
https://github.com/nanoxd/bourne
json swift
Last synced: 2 months ago
JSON representation
A JSON parser for backends with variant types
- Host: GitHub
- URL: https://github.com/nanoxd/bourne
- Owner: nanoxd
- License: mit
- Created: 2016-09-13T20:28:51.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-09-06T17:36:33.000Z (almost 9 years ago)
- Last Synced: 2026-01-17T04:34:35.033Z (6 months ago)
- Topics: json, swift
- Language: Swift
- Homepage:
- Size: 131 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bourne
[](https://travis-ci.org/nanoxd/Bourne)
## Installation
### CocoaPods
Add the line `pod "Bourne"` to your `Podfile`
### Carthage
Add the line `github "nanoxd/Bourne"` to your `Cartfile`
### Manual
Clone the repo and drag the file `Bourne.swift` into your Xcode project.
#### Swift Package Manager
Add the line `.Package(url: "https://github.com/nanoxd/Bourne.git", majorVersion: 1)` to your `Package.swift` file.
## Usage
### Conforming to Mappable
```swift
import Bourne
struct Person {
let firstName: String
let lastName: String
let age: Int?
}
extension Person: Mappable {
static func decode(_ j: JSON) throws -> Person {
return Person(
firstName: try j.from("firstName"),
lastName: try j.from("lastName", or: "Medina"),
age: try? j.from("age")
)
}
}
```
### Optional Values
Bourne has two ways of working with Optional values, `try?` and adding a default value to `from`.
First, let's look at how to decode a value that is sometimes included in our JSON:
```json
{
"people": [
{
"firstName": "Jose",
"lastName": null
},
{
"firstName": "Maria",
"lastName": "Sanchez",
"age": 20
}
]
}
```
```swift
try? j.from("age")
```