A flexible collection of Swift protocols and helpers to organize and manage translating between JSON and native Swift objects.
Designed to give your model objects a consistent JSON conversion / mapping / caching / offline strategy without getting in your way.
-
Designed to be flexible enough to let you implement your models as your requirements dictate while giving you a general framework to organize your JSON parsing and caching strategy.
-
A collection of composable but distinct features, so you can use the protocols and helpers that work for the use case at hand.
-
Simple, straightforward, and lightweight; Derulo sits right between "I'll just implement it myself without a third party library" and "I don't really want to reinvent the wheel and implement a JSON strategy from scratch"
var jasonJSON = [String : Any]()
jasonJSON["id"] = 93
jasonJSON["name"] = "Jason Derulo"
jasonJSON["fav_song"] = "wiggle"
let jasonPerson = Person(json: jasonJSON)
let jasonPersonJSON = jasonPerson.asJSON
var jaySeanJSON = JSON()
jaySeanJSON["id"] = 57
jaySeanJSON["name"] = "Jay Sean"
let jsonArray: [JSON] = [jasonJSON, jaySeanJSON]
let people: [Person] = JSONMapper<Person>().mapArray(json: jsonArray)
let peopleJSON: [JSON] = JSONConverter<Person>().jsonArray(fromArray: people)
let persistenceKey = "people"
JSONPersistenceManager<Person>().store(array: people, withKey: persistenceKey)
let restoredPeople: [Person] = JSONPersistenceManager<Person>().restoreArray(withKey: persistenceKey)
JSONPersistenceManager<Person>().removeObject(withKey: persistenceKey)
guard let data = try? JSONSerialization.data(withJSONObject: jasonJSON, options: []) else { return }
let jasonPersonFromData = JSONMapper<Person>().map(data: data)
print(jasonPersonFromData ?? "")
struct Person: Identifiable, JSONModel {
let identifier: Identifier
let name: String
let favoriteSong: Song?
}
extension Person: JSONMappable {
init?(json: JSON?) {
guard let json = json else { return nil }
guard let id = IntToStringTransform().transform(fromJSON: json["id"]) else {
print(#function, String(describing: Person.self), "JSON missing required properties")
return nil
}
self.identifier = id
self.name = json["name"] as? String ?? "No Name"
self.favoriteSong = Song(jsonEntry: json["fav_song"])
}
}
extension Person: JSONConvertible {
var asJSON: JSON {
var json = JSON()
json["id"] = IntToStringTransform().transform(toJSON: identifier)
json["name"] = name
if let favoriteSong = favoriteSong {
json["fav_song"] = favoriteSong.asJSONEntry
}
return json
}
}
extension Person: JSONPersistable {
var asPersistenceJSON: JSON { asJSON }
}
enum Song: String {
case wiggle
case talkDirty
}
extension Song: JSONEntryMappable, JSONEntryConvertible {
init?(jsonEntry: JSONEntry?) {
guard let jsonEntry = jsonEntry as? String else { return nil }
self.init(rawValue: jsonEntry)
}
var asJSONEntry: String { rawValue }
}
Swift 5.6
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Derulo as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/dockwa/Derulo", .upToNextMajor(from: "1.7.2"))
]
Derulo is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Derulo'
Christian Hatch, [email protected]
Derulo is available under the MIT license. See the LICENSE file for more info.