Latest 1.3.0
Homepage https://github.com/dockwa/Derulo
License MIT
Platforms ios 8.0
Authors

Powered by Dockwa

Version
License
Platform

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"

Example

start with JSON

var jasonJSON = [String : Any]()
jasonJSON["id"] = 93
jasonJSON["name"] = "Jason Derulo"
jasonJSON["fav_song"] = "wiggle"

From JSON

parse into model object

let jasonPerson = Person(json: jasonJSON)

To JSON

convert to JSON

let jasonPersonJSON = jasonPerson.asJSON

Arrays

make another json

var jaySeanJSON = JSON()
jaySeanJSON["id"] = 57
jaySeanJSON["name"] = "Jay Sean"

start with array of json

let jsonArray: [JSON] = [jasonJSON, jaySeanJSON]

parse into model object

let people: [Person] = JSONMapper<Person>().mapArray(json: jsonArray)

convert to JSON

let peopleJSON: [JSON] = JSONConverter<Person>().jsonArray(fromArray: people)

Persistence

let persistenceKey = "people"

store to disk

JSONPersistenceManager<Person>().store(array: people, withKey: persistenceKey)

restore from disk

let restoredPeople: [Person] = JSONPersistenceManager<Person>().restoreArray(withKey: persistenceKey)

delete from disk

JSONPersistenceManager<Person>().removeObject(withKey: persistenceKey)

From Data

start with Data

guard let data = try? JSONSerialization.data(withJSONObject: jasonJSON, options: []) else { return }

map to object

let jasonPersonFromData = JSONMapper<Person>().map(data: data)
print(jasonPersonFromData ?? "")

Example Implementation

struct Person: Identifiable {
    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: JSONModel {}

extension Person: JSONPersistable {

    var asPersistenceJSON: JSON {
        return 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 {
        return rawValue
    }
}

Requirements

Swift 3.0

Installation

Derulo is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'Derulo'

Author

Christian Hatch, [email protected]

License

Derulo is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Derulo",
    "version": "1.3.0",
    "summary": "A flexible collection of Swift protocols and helpers to organize and manage translating between JSON and native Swift objects.",
    "description": "Give your model objects a consistent JSON conversion / mapping / caching / offline strategy without getting in your waynnDesigned 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.nnA collection of composable but distinct features, so you can use the mixins that work for the use case at hand.nnSimple, 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"",
    "homepage": "https://github.com/dockwa/Derulo",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Christian Hatch": "[email protected]"
    },
    "source": {
        "git": "https://github.com/dockwa/Derulo.git",
        "tag": "1.3.0"
    },
    "social_media_url": "https://twitter.com/commodoreftp",
    "swift_version": "4.1",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Derulo/*.swift"
}

Pin It on Pinterest

Share This