🌟 Super light and easy automatic JSON to model mapper

Overview

magic-mapper-swift

🌟 Super light and easy automatic JSON to model mapper

  • Finish writing README.md
  • Ability to map NSManagedObject
  • Ability to convert model back to dictionary

How to setup

Step 1: Get the JSON model

{
  "total_count": 176552,
  "incomplete_results": false,
  "items": [
    {...}
  ]
}

Step 2: Write the Swift models

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).

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.

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.

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 :)

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!

You might also like...
Swift-json - High-performance json parsing in swift

json 0.1.4 swift-json is a pure-Swift JSON parsing library designed for high-per

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend
Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo

SwiftyJSON makes it easy to deal with JSON data in Swift.

SwiftyJSON SwiftyJSON makes it easy to deal with JSON data in Swift. Platform Build Status *OS Linux Why is the typical JSON handling in Swift NOT goo

The easy to use Swift JSON decoder
The easy to use Swift JSON decoder

Unbox is deprecated in favor of Swift’s built-in Codable API and the Codextended project. All current users are highly encouraged to migrate to Codable as soon as possible.

Easy JSON to NSObject mapping using Cocoa's key value coding (KVC)
Easy JSON to NSObject mapping using Cocoa's key value coding (KVC)

#Motis Object Mapping Easy JSON to NSObject mapping using Cocoa's key value coding (KVC) Motis is a user-friendly interface with Key Value Coding that

Model framework for Cocoa and Cocoa Touch

Mantle Mantle makes it easy to write a simple model layer for your Cocoa or Cocoa Touch application. The Typical Model Object What's wrong with the wa

Dogtector: dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized
Dogtector: dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized

Dogtector Project description Dogtector is dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized for u

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Releases(1.0.2)
Owner
Adrian Mateoaea
Adrian Mateoaea
Simple and Elegant Range(A,B) to Range(P,Q) mapper in less then five lines of code.

HSRange Description HSRangeConvertor Simple and Elegant Range[A,B] to Range[P,Q] mapper in less then three lines of code. E.g. Suppose we have Range[1

Hitendra Hckr 10 Sep 29, 2021
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very important part of Pitaya.

John Lui 454 Oct 30, 2022
HandyJSON is a framework written in Swift which to make converting model objects to and from JSON easy on iOS.

HandyJSON To deal with crash on iOS 14 beta4 please try version 5.0.3-beta HandyJSON is a framework written in Swift which to make converting model ob

Alibaba 4.1k Dec 29, 2022
ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects to and from JSON.

ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from J

Tristan Himmelman 9k Jan 2, 2023
JSONExport is a desktop application for Mac OS X which enables you to export JSON objects as model classes with their associated constructors, utility methods, setters and getters in your favorite language.

JSONExport JSONExport is a desktop application for Mac OS X written in Swift. Using JSONExport you will be able to: Convert any valid JSON object to a

Ahmed Ali 4.7k Dec 30, 2022
Mappable - Flexible JSON to Model converter, specially optimized for immutable properties

Mappable is a lightweight, flexible, easy-to-use framework to convert JSON to model, specially optimized for immutable property initializatio

Leave 27 Aug 26, 2022
Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON.

JSONFeed Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON. For more information about this new feed format visit: https:/

Toto Tvalavadze 31 Nov 22, 2021
JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift.

JSEN /ˈdʒeɪsən/ JAY-sən JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift. A JSON, as defined in the EC

Roger Oba 8 Nov 22, 2022
JSON-Practice - JSON Practice With Swift

JSON Practice Vista creada con: Programmatic + AutoLayout Breve explicación de l

Vanesa Giselle Korbenfeld 0 Oct 29, 2021
Ss-json - High-performance json parsing in swift

json 0.1.1 swift-json is a pure-Swift JSON parsing library designed for high-per

kelvin 43 Dec 15, 2022