Latest 1.0.0
Homepage https://github.com/oreninit/Unpack
License MIT
Platforms ios 9.3
Frameworks Foundation
Authors

Version
License
Platform

About Unpack

Unpack is a simple library which wraps Codable protocols and allows easy encoding/decoding of you objects, levaraging the power of protocol and protocol extensions.

It takes away the need for the common boilertplate and leaves you with a clean, readable code.

There are many different ways to pack/unpack your object.

All you need to do is conform to Packable (for encoding), Unpackable (for decoding) or UnPacked (for both) and you’re good to go.

In it’s easiest form, it uses a default JSONEncoder / JSONDecoder to perform the conversion. If you’d like to provide your own settings, you could implement the getter in your class or use one of the functions that takes it as an argument.

Some examples

instead of writing this for every conversion:

        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .secondsSince1970
        decoder.keyDecodingStrategy = .convertFromSnakeCase
        let myObject: MyObject? = try? decoder.decode(MyObject.self, from: data)

Wouldn’t it be easier to just do this?

        let myObject = try? MyObject.unpack(data: data)

Or this?

        let myObject: MyObject? = try? data.unpack()

It is for me.

How about the other way?
Convert this:

        let encoder = JSONEncoder()
        encoder.dateEncodingStrategy = .secondsSince1970
        encoder.keyEncodingStrategy = .convertToSnakeCase
        let data: Data? = try? encoder.encode(myObject)

To this:

        let data: Data? = try? myObject.pack()

Simple, right?
It works just the same for arrays.

There are many other ways to do it.
Here’s some examples:

let user: User? = try? unpack(data: data)
let data: Data? = try? pack(user)

let userDictionary = ["name" : "Walter White", "job" : "Chemistry teacher", "hidden_identity" : "Heisenberg"]
var unpacker = Unpacker<Person>(decoder: customDecoder)
let character = try? unpacker.unpack(json: userDictionary)
// The unpacker will get hold of your possible error (if you don't want to do-try-catch)
if let error = unpacker.error {
    print(error)
}

So easy it doesn’t requires explanations.

Go ahead and explore!

Installation

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

pod 'Unpack'

Author

Oren Farhan

License

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

Latest podspec

{
    "name": "Unpack",
    "version": "1.0.0",
    "summary": "A library to simplify the process of parsing Data/JSON to objects and vice versa",
    "description": "Unpack allows easy conversion between raw Data and JSON objects and your own Class and Struct types. It consists of a simple API, but allows further control over the process.",
    "homepage": "https://github.com/oreninit/Unpack",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Oreninit": "[email protected]"
    },
    "source": {
        "git": "https://github.com/oreninit/Unpack.git",
        "tag": "1.0.0"
    },
    "social_media_url": "https://twitter.com/oreninit",
    "platforms": {
        "ios": "9.3"
    },
    "source_files": "Unpack/Classes/**/*",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "4.1"
}

Pin It on Pinterest

Share This