Latest 0.1.1
Homepage https://github.com/PKAuth/SwiftAeson
License MIT
Platforms ios 8.0, requires ARC
Authors

![CI Status](http://img.shields.io/travis/James Parker/SwiftAeson.svg?style=flat)
Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

To utilize SwiftAeson in your projects, you need to implement the protocols ToJSON and FromJSON for whichever types you want to serialize and deserialize with JSON.
Here is an example. First we define a class Test:

class Test : Equatable {
    var test : String = "test"
    var booltest : Bool = false
    var booltest2 : Bool = true
    var nul : NSNull = NSNull()
    var doub : Double = 32.0

    init() {

    }

    required init ( test: String, booltest : Bool, booltest2 : Bool, nul : NSNull, doub : Double) {
        self.test = test
        self.booltest = booltest
        self.booltest2 = booltest2
        self.nul = nul
        self.doub = doub
    }
}

func ==( a : Test, b : Test) -> Bool {
    return a.test == b.test
        && a.booltest == b.booltest
        && a.booltest2 == b.booltest2
        && a.nul == b.nul
        && a.doub == b.doub
}

Now we implement ToJSON and FromJSON.
This is shown for a class, but it works for structs and enums as well.

extension Test : ToJSON, FromJSON {
    func toJSON() -> JValue {
        let test = "test" %= self.test
        let booltest = "booltest" %= self.booltest
        let booltest2 = "booltest2" %= self.booltest2
        let nul = "null" %= self.nul
        let doub = "double" %= self.doub

        return Aeson.object( [ test, booltest, booltest2, nul, doub])
    }

    static func fromJSON( value: JValue) -> Self? {
        switch value {
        case .JObject( let o):
            if let test : String = o %! "test" {
                if let booltest : Bool = o %! "booltest" {
                    if let booltest2 : Bool = o %! "booltest2" {
                        if let nul : NSNull = o %! "null" {
                            if let doub : Double = o %! "double" {
                                return self.init(test: test, booltest: booltest, booltest2: booltest2, nul: nul, doub: doub)
                            }
                        }
                    }
                }
            }

            return nil
        default:
            return nil
        }
    }
}

Here we show how to serialize and deserialize our test class.
Then we compare them to test that the original and deserialized versions are equal.

let original = Test()
let serialized = Aeson.encode( original)
let encoded = NSString(data: serialized!, encoding: NSUTF8StringEncoding)
print(encoded!)

let deserialized : Test? = Aeson.decode( serialized!)
print(deserialized!)

original == deserialized!

Requirements

Installation

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

pod "SwiftAeson"

Author

James Parker, [email protected]

License

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

Latest podspec

{
    "name": "SwiftAeson",
    "version": "0.1.1",
    "summary": "A JSON serialization library for Swift that mimics Haskell's Aeson.",
    "description": "SwiftAeson is a JSON serialization library for Swift that mimics Haskell's [Aeson](https://hackage.haskell.org/package/aeson). nIt provides a typesafe wrapper around `NSJSONSerialization` and can be utilized for any data type including classes, enums, and structs.",
    "homepage": "https://github.com/PKAuth/SwiftAeson",
    "license": "MIT",
    "authors": {
        "James Parker": "[email protected]"
    },
    "source": {
        "git": "https://github.com/PKAuth/SwiftAeson.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "SwiftAeson": [
            "Pod/Assets/*.png"
        ]
    }
}

Pin It on Pinterest

Share This