All Projects → kgn → JSONMagic

kgn / JSONMagic

Licence: MIT license
JSONMagic makes it easy to traverse and parse JSON in Swift.

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language
objective c
16641 projects - #2 most used programming language

Projects that are alternatives of or similar to JSONMagic

Colorizeswift
Terminal string styling for Swift.
Stars: ✭ 253 (+1231.58%)
Mutual labels:  carthage
OrangeLabel
OrangeLabel is extensions of UILabel linkable, available line background and placeholder text
Stars: ✭ 19 (+0%)
Mutual labels:  carthage
Sqlable
Swift library for making storing data in a SQLite database simple and magic-free
Stars: ✭ 83 (+336.84%)
Mutual labels:  carthage
BuckoNetworking
iOS Protocol-Oriented Networking in Swift
Stars: ✭ 18 (-5.26%)
Mutual labels:  carthage
Validated
A rule-based validation framework
Stars: ✭ 31 (+63.16%)
Mutual labels:  carthage
OpenSansSwift
Easily use the OpenSans font in Swift
Stars: ✭ 42 (+121.05%)
Mutual labels:  carthage
Simplecheckbox
A simple Checkbox
Stars: ✭ 253 (+1231.58%)
Mutual labels:  carthage
TVKit
UI components for tvOS
Stars: ✭ 20 (+5.26%)
Mutual labels:  carthage
atinternet-apple-sdk
AT Internet mobile analytics solution for Apple devices
Stars: ✭ 25 (+31.58%)
Mutual labels:  carthage
Dots
Lightweight Concurrent Networking Framework
Stars: ✭ 35 (+84.21%)
Mutual labels:  carthage
Go-Flashcards
Go Flashcards for iOS and WatchOS - Official repository
Stars: ✭ 60 (+215.79%)
Mutual labels:  carthage
DragDropUI
A set of iOS UI components which have drag & drop capability.
Stars: ✭ 30 (+57.89%)
Mutual labels:  carthage
AirPlay
Small framework that lets users track iOS AirPlay availability and extra features.
Stars: ✭ 46 (+142.11%)
Mutual labels:  carthage
iOSProjects
It's project that contains different applications developed with Swift 5.7 👨‍💻👩🏼‍💻🧑🏿‍💻
Stars: ✭ 122 (+542.11%)
Mutual labels:  carthage
Moya-Gloss
Gloss bindings for Moya
Stars: ✭ 37 (+94.74%)
Mutual labels:  carthage
Localize
Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
Stars: ✭ 253 (+1231.58%)
Mutual labels:  carthage
VGSegment
A segment menu with line animation
Stars: ✭ 19 (+0%)
Mutual labels:  carthage
filestack-ios
Official iOS SDK for Filestack - API and content management system that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application.
Stars: ✭ 44 (+131.58%)
Mutual labels:  carthage
Soundable
Soundable allows you to play sounds, single and in sequence, in a very easy way
Stars: ✭ 88 (+363.16%)
Mutual labels:  carthage
Partial
Type-safe wrapper mirroring the wrapped type's properties, making each property optional
Stars: ✭ 61 (+221.05%)
Mutual labels:  carthage

JSONMagic

JSONMagic makes it easy to traverse and parse JSON in Swift.

Swift 3 Release License

Build Status Test Coverage Carthage Compatible CocoaPods Version CocoaPods Platforms

Twitter Follow Star

Installing

Carthage

github "kgn/JSONMagic"

CocoaPods

pod 'JSONMagic'

Examples

Lets say you get a JSON user profile like this from your server:

{
    "user": {
        "name": "David Keegan",
        "age": 30,
        "accounts": [
            {
                "name": "twitter",
                "user": "iamkgn"
            },
            {
                "name": "dribbble",
                "user": "kgn"
            },
            {
                "name": "github",
                "user": "kgn"
            }
        ]
    }
}

Parsing this can take a bunch of nested if statements in Swift to cast things to the right type in order to traverse down the data tree.

Before

let twitterUser: String?
if let data = serverResponse {
    if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: Any] {
        if let user = json?["user"] as? [String: Any] {
            if let accounts = user["accounts"] as? [Any] {
                if let twitter = accounts.first as? [String: Any] {
                    twitterUser = twitter["user"] as? String
                }
            }
        }
    }
}

After

let twitterUser = JSONMagic(data: serverResponse).get("user").get("accounts").first.get("user").string

Or, if you prefer subscripting :)

let twitterUser = JSONMagic(data: serverResponse)["user"]["accounts"][0]["user"].string

It even works with Paw key paths.

let twitterUser = JSONMagic(data: serverResponse).keypath("user.accounts.0.user").string

JSONMagic handles all of this for you with method chaining. So you’re always working with a magical wrapper JSONMagic object that you can chain as long as you want, then just call value at the end to get the ending value and cast that to the final type you want. There are helpers for all the JSON data types too: .bool, .int, .float, .double, .string, .array and .dictionary.

It’s super loosie goosie so doesn’t care about nil values going in, or anywhere in the chain.

Some more examples

let json = JSONMagic(data: serverResponse)

json.get("user").get("name").string // David Keegan
json["user"]["age"].integer // 30

let twitter = json.get("user").get("accounts").first
twitter["name"].value // twitter
twitter["user"].value // iamkgn

let dribbble = json.get("user").get("accounts").get(1)
dribbble.get("name").value // dribbble
dribbble.get("user").value // kgn

let github = json.get("user").get("accounts").last
github.get("name").value // github
github.get("user").value // kgn

let bad = json.get("user").get("accounts").get(5)
bad.get("name").value // nil
bad.get("user").value // nil

Progress

  • Badges
  • Tests
  • Travis
  • Carthage
  • CocoaPods
  • Description
  • Documentation
  • AppleTV
  • AppleWatch
  • Prebuilt Frameworks
  • Travis Test Matrix
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].