Latest 0.1.0
Homepage https://github.com/akaashdev/SwiftJSONi
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Why SwiftJSONi?

The traditional way of handling JSON in Swift is so cumbersome and requires many lines of code. For example, consider a JSON response from a API for bakery items,

{
    "items": {
        "item": [
            {
                "id": "0001",
                "type": "donut",
                "name": "Cake",
                "ppu": 0.55,
                "batters": {
                "batter": [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" },
                        { "id": "1003", "type": "Blueberry" },
                        { "id": "1004", "type": "Devil's Food" }
                    ]
                },
                "topping": [
                    { "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5007", "type": "Powdered Sugar" },
                    { "id": "5006", "type": "Chocolate with Sprinkles" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" }
                ]
            }
        ]
    }
}

To get the type of 4th topping in first item, the safest Swift code looks like,

if let json = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String: Any],
    let items = object["items"] as? [String: Any],
    let item = items["item"] as? [[String: Any]],
    let firstItem = item.first,
    let toppings = firstItem["topping"] as? [[String: Any]],
    toppings.count >= 3,
    let type = toppings[3]["type"] as? String
{
    print(type)
}

It involves lots of typecasting and safe index checks due to Swift’s strict typecast and array index policies.
To achieve the same with SwiftJSONi, the code looks like,

if let json = JSON(data: jsonData),
    let type = json["items"]["item"][0]["topping"][3]["type"].string
{
    print(type)
}

That’s it!! It is so safe and readable. No need to worry about type casting and array index checks. It is all done automatically.

Installation

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

pod 'SwiftJSONi'

Author

Akaash Dev, [email protected]

License

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

Latest podspec

{
    "name": "SwiftJSONi",
    "version": "0.1.0",
    "summary": "A Light weight Swift JSON Model, that allows to use JSON the way it has to be",
    "swift_version": "4.2",
    "description": "A light weight Swift JSON Model, completely written in Swift, that makes handling JSON data in Swift and Objective-C very easy and safe. No more type casting of data and safe unwraping. No more crashing because of force unwraping, Index Out of bound, missing key etc.. Its totally nil safe as all the optional handling is done. The model is basically of struct type making it even thread safe and very light. It supports chaining. This lib makes your code a lot neater, simpler and more readable. SwiftJSONi follows the rules of JSON described under RC-7129 and www.json.org. It allows parsing fragments.",
    "homepage": "https://github.com/akaashdev/SwiftJSONi",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Akaash Dev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/akaashdev/SwiftJSONi.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/akaash_dev",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "SwiftJSONi/Classes/**/*"
}

Pin It on Pinterest

Share This