Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrianmteo/magic-mapper-swift
🌟 Super light and easy automatic JSON to model mapper
https://github.com/adrianmteo/magic-mapper-swift
Last synced: 2 months ago
JSON representation
🌟 Super light and easy automatic JSON to model mapper
- Host: GitHub
- URL: https://github.com/adrianmteo/magic-mapper-swift
- Owner: adrianmteo
- License: apache-2.0
- Created: 2016-10-03T17:05:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-09T17:40:50.000Z (about 8 years ago)
- Last Synced: 2024-09-19T13:32:39.057Z (3 months ago)
- Language: Swift
- Homepage:
- Size: 200 KB
- Stars: 26
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# magic-mapper-swift
🌟 Super light and easy automatic JSON to model mapper![](https://img.shields.io/cocoapods/p/MagicMapper.svg)
![](https://img.shields.io/cocoapods/v/MagicMapper.svg)
![](https://img.shields.io/cocoapods/l/MagicMapper.svg)- [ ] Finish writing `README.md`
- [ ] Ability to map `NSManagedObject`
- [x] Ability to convert model back to dictionary## How to setup
### Step 1: Get the JSON model
```json
{
"total_count": 176552,
"incomplete_results": false,
"items": [
{...}
]
}
```### Step 2: Write the Swift models
```swift
class GithubRepository: NSObject, Mappable {
var id : String = ""
var name : String = ""
var body : String = ""
var url : String = ""
var photo : String = ""
var createdAt : Date?
}class GithubSearch: NSObject, Mappable {
var total : Int = 0
var items : [GithubRepository] = []
}
```### Step 3: Link your custom namings with the JSON model
The `mapFromDictionary` property lets you customize the properties mappings. You can also access nested values within dictionaries and arrays (example: `emails.0.address`).
```swift
var mapFromDictionary: [String : String] {
return [
"body" : "description",
"photo" : "owner.avatar_url",
"createdAt" : "created_at"
]
}
```### Step 4: Custom properties
The `mapFromDictionaryTypes` property let's you customize the type of the JSON property (only for optionals). If you have other Swift structures that you need to use, just extend them using the `Mappable` protocol.
```swift
var mapFromDictionaryTypes: [String : Mappable.Type] {
return [
"createdAt" : Date.self
]
}
```For instance `createdAt` is of type `Date` and the JSON property is of type `String`. In order to convert the `String` to a `Date` type, just extend it using the `Mappable` protocol and it will automatically know to convert and set the value to the model.
```swift
extension Date: Mappable {
init?(from: Any) {
if let value = from as? String {
let formatter = DateFormatter()
formatter.dateFormat = "YYYY-MM-dd'T'HH':'mm':'ss'Z'"
if let date = formatter.date(from: value) {
self.init(timeIntervalSince1970: date.timeIntervalSince1970)
return
}
}
self.init()
}
}
```### And that's it!
You can now populate your model from the generic dictionary that comes from your network layer. Happy coding :)
```swift
Alamofire.request(APIURL).responseJSON { (response) in
if let dictionary = response.result.value as? KeyValue {
self.feed = GithubSearch(dictionary)
}
}
```## Contributing
There's still work to do here! Our goal is to speed up development times, making JSON mapping easy as 1-2-3. We would love to see you involved in this project!