All Projects → Molbie → Outlaw

Molbie / Outlaw

Licence: MIT license
JSON mapper for macOS, iOS, tvOS, and watchOS

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Outlaw

ScaledFont
ScaledFont - Using custom fonts with dynamic type
Stars: ✭ 50 (+108.33%)
Mutual labels:  tvos, watchos
Futures
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 59 (+145.83%)
Mutual labels:  tvos, watchos
Wwdc Notes
WWDCNotes.com content ✨
Stars: ✭ 183 (+662.5%)
Mutual labels:  tvos, watchos
Apple Runtime Headers
Objective-C runtime headers for Apple's iOS, macOS, tvOS and watchOS frameworks
Stars: ✭ 174 (+625%)
Mutual labels:  tvos, watchos
Iso8601
ISO8601 date parser and writer
Stars: ✭ 213 (+787.5%)
Mutual labels:  tvos, watchos
Anydate
Swifty Date & Time API inspired from Java 8 DateTime API.
Stars: ✭ 178 (+641.67%)
Mutual labels:  tvos, watchos
Swiftui Sliders
🚀 SwiftUI Sliders with custom styles
Stars: ✭ 241 (+904.17%)
Mutual labels:  tvos, watchos
Mapboxstatic.swift
Static map snapshots with overlays in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
Stars: ✭ 162 (+575%)
Mutual labels:  tvos, watchos
Cache
Swift caching library
Stars: ✭ 210 (+775%)
Mutual labels:  tvos, watchos
Htmlkit
An Objective-C framework for your everyday HTML needs.
Stars: ✭ 206 (+758.33%)
Mutual labels:  tvos, watchos
Cocoalumberjack
A fast & simple, yet powerful & flexible logging framework for Mac and iOS
Stars: ✭ 12,584 (+52333.33%)
Mutual labels:  tvos, watchos
Wwdc
You don't have the time to watch all the WWDC session videos yourself? No problem me and many contributors extracted the gist for you 🥳
Stars: ✭ 2,561 (+10570.83%)
Mutual labels:  tvos, watchos
Xamarin Macios
Bridges the worlds of .NET with the native APIs of macOS, iOS, tvOS, and watchOS.
Stars: ✭ 2,109 (+8687.5%)
Mutual labels:  tvos, watchos
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (+637.5%)
Mutual labels:  tvos, watchos
Mirrordiffkit
Graduation from messy XCTAssertEqual messages.
Stars: ✭ 168 (+600%)
Mutual labels:  tvos, watchos
Objc Sdk
LeanCloud Objective-C SDK
Stars: ✭ 186 (+675%)
Mutual labels:  tvos, watchos
Swifthttpstatuscodes
Swift enum wrapper for easier handling of HTTP status codes.
Stars: ✭ 159 (+562.5%)
Mutual labels:  tvos, watchos
Version
Represent and compare versions via semantic versioning (SemVer) in Swift
Stars: ✭ 160 (+566.67%)
Mutual labels:  tvos, watchos
Ios Crash Dump Analysis Book
iOS Crash Dump Analysis Book
Stars: ✭ 158 (+558.33%)
Mutual labels:  tvos, watchos
Human Interface Guidelines Extras
Community additions to Apple's Human Interface Guidelines
Stars: ✭ 225 (+837.5%)
Mutual labels:  tvos, watchos

License Platforms Build Status codecov

Outlaw

In Swift, we all deal with JSON, plists, and various forms of [String: Any]. Outlaw provides various ways to deal with these in an expressive and type safe way. Outlaw will help you write declarative, performant, error handled code using the power of Protocol Oriented Programming™.

Usage

Extracting values from [String: Any] using Outlaw is as easy as

let name: String = try json.value(for: "name")
let url: URL = try json.value(for: "user.website") // extract from nested objects!

Converting to Models

Often we want to take an extractable object (like [String: Any]) and deserialize it into one of our local models—for example we may want to take some JSON and initialize one of our local models with it:

struct User: Deserializable {
    var id: Int
    var name: String
    var email: String

    init(object: Extractable) throws {
        id = try object.value(for: "id")
        name = try object.value(for: "name")
        email = try object.value(for: "email")
    }
}

Now, just by virtue of supplying a simple initializer you can pull your models directly out of [String: Any]!

let users: [User] = try json.value(for: "users")

That was easy! Thanks, Protocol Oriented Programming™!

Serializing Models

We've looked at going from our [String: Any] into our local models, but what about the other way around?

extension User: Serializable {
	func serialized() -> [String: Any] {
        return [
            "id": "id",
            "name" : name,
            "email": email
        ]
    }
}

Now, you might be thinking "but couldn't I use reflection to do this for me automagically?" You could. And if you're into that, there are some other great frameworks for you to use. But Outlaw believes mirrors can lead down the road to a world of hurt. Outlaw lives in a world where What You See Is What You Get™, and you can easily adapt to APIs that snake case, camel case, or whatever case your backend developers are into. Outlaw code is explicit and declarative. But don't just take Outlaw's word for it—read the good word towards the end here on the official Swift blog.

Error Handling

Are you someone that doesn't care about errors? Use an optional data type.

let users: [User]? = json.value(for: "users")

Otherwise, wrap your code in a do-catch to get all the juicy details when things go wrong.

do {
	let users: [User] = try json.value(for: "users")
}
catch {
	print(error)
}

Add Your Own Values

Out of the box, Outlaw supports extracting native Swift types like String, Int, etc., as well as URL, Date and anything conforming to Deserializable, and arrays or dictionaries of all the aforementioned types.

However, Outlaw doesn't just leave you up the creek without a paddle! Adding your own Outlaw value type is as easy as extending your type with Value.

extension CGPoint: Value {
    public static func value(from object: Any) throws -> CGPoint {
        guard let properties = object as? [String: CGFloat] else {
            throw OutlawError.typeMismatch(expected: [String: CGFloat].self, actual: type(of: object))
        }
        let x: CGFloat = properties["x"] ?? 0
        let y: CGFloat = properties["y"] ?? 0
        
        return CGPoint(x: x, y: y)
    }
}

By simply implementing value(from:), Outlaw allows you to immediately do this:

let point: CGPoint = try json.value(for: "point")

Protocol Oriented Programming™ strikes again!

Intermediate Values

Don't like how Outlaw implements the default value extraction? Have a different date format? No problem! All you need is a transformation function when extracting the values.

let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "GMT")
formatter.dateFormat = "MM/dd/yyyy"

let date: Date? = json.value(for: "date", with: { (dateString: String) -> Date? in
	return formatter.date(from: dateString)
})

We can also use the power of swift and shorten the above code to:

let date: Date? = json.value(for: "date", with: formatter.date)

Performance

Outlaw is based on the same underlying code as Marshal and is just as performant. You should always take benchmarks with a grain of salt, but chew on these benchmarks for a bit anyway. Unfortunately, the JSONShootout project was built in a way so that Outlaw can't be added because of ambiguous method collisions with Marshal.

Goal

Outlaw was created by one of the main contributors to Marshal. However, Marshal was designed to be a bare bones framework that needed to be extended for additional features. Outlaw was designed to be a more feature rich framework that can handle a lot more data extraction scenarios out of the box.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].