https://github.com/rnystrom/bottlerocket
An experimental CLI tool for generating normalized Swift models from example JSON responses.
https://github.com/rnystrom/bottlerocket
code-generation json-response swift swift-models
Last synced: 10 months ago
JSON representation
An experimental CLI tool for generating normalized Swift models from example JSON responses.
- Host: GitHub
- URL: https://github.com/rnystrom/bottlerocket
- Owner: rnystrom
- Created: 2017-05-04T20:36:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-12T21:27:55.000Z (over 8 years ago)
- Last Synced: 2025-03-21T01:41:33.605Z (10 months ago)
- Topics: code-generation, json-response, swift, swift-models
- Language: Swift
- Homepage:
- Size: 2.69 MB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BottleRocket
An experimental CLI tool for generating normalized Swift models from example JSON responses.
## Goals
- Generate Swift models that don't need any cleanup by hand
- Allow parsing partial JSON responses into the same model
- Properties that don't show up in every model are optional
- Generated Swift JSON parsing code without dependencies
- Experiment with functional programming techniques in Swift
## Installation
- Download project
- Open `BottleRocket.xcodeproj`
- Build
- Should be installed in `/usr/local/bin`
## Example
Suppose you have two different endpoints that return JSON:
`http://myservice.com/api/events`
```json
{
"title": "Birthday party",
"likes": 12,
"is_public": true,
"users": [
{
"name": "Sam",
"id": 1
},
{
"name": "Zoey",
"id": 2
}
]
}
```
`http://myservice.com/api/user/1`
```json
{
"name": "Sam",
"age": 28,
"id": 1,
"horoscope": "gemini"
}
```
You can save both sample responses into two files: `events.json` and `user.json`. Then run:
```shell
$ bottlerocket gen ~/path/to/models
```
Which then generates two different models:
```swift
final class Event: NSCoding {
let title: String
let likes: NSNumber
let is_public: Bool
let users: [User]
init(title: String, likes: NSNumber, is_public: Bool, users: [User]) {
// ...
}
convenience init?(json: [String: Any]) {
// unpack and parse models
// call designated init
}
// encode/decode
}
final class User: NSCoding {
let name: String
let id: NSNumber
// only shows up in api/user/1 response
let age: NSNumber?
let horoscope: String?
// init, json, coding, etc
}
```
## Dependencies
- [InflectorKit](https://github.com/mattt/InflectorKit) to convert plural keys into singular classnames
- [SwiftCLI](https://github.com/jakeheis/SwiftCLI) because this is my first CLI app